Ask coding questions

← Back to all posts
Please help! It says undeclared Identifier and IDk how to fix it.
baolinchang (5)

Hello! I am fairly new to using classes in C++ and I was trying to do me.printsummary but it comes out as an error because it says there is an undeclared identifier, which is "me" (line 20). However, I did declare it in my main function. How can I fix this problem? Can I only use it in my main or something? Any help is appreciated. Thank you all in advance :)

Answered by Coder100 (6999) [earned 5 cycles]
View Answer
Commentshotnewtop
Coder100 (6999)

Hello, it appears that me is supposed to be a class of Character right?

baolinchang (5)

@Coder100 Yes! Do you know how to fix it?

Coder100 (6999)

ahh so like right now me is an undefined variable (you are just using it, but you haven't actually declared it) so if I recall correctly, the way to do this is before line 20, add

Character me = new Character();

and inside, put it any arguments.
@baolinchang

baolinchang (5)

Hello! Thank you for the response. I tried it and it says unknown type name 'Character'. Why is [email protected]

baolinchang (5)

I tried it and it says there was an unknown type named Character :( also i may have sent this twice bc idk if it sent the first time [email protected]

Coder100 (6999)

oh yes, probably want to declare that first? @baolinchang

Coder100 (6999)

Here you go! Fixed!
https://repl.it/@Coder100/rpg-game-solution-for-baolinchang
Turns out I was thinking of declaring classes in C# :P
but basically you have to declare the class first before you can access it. Anyways, so declaring a class is

ClassName me = ClassName();

while in C# it is

ClassName me = new ClassName();

Sorry about that!
@baolinchang

baolinchang (5)

No need to be sorry! Thank you so much for the solution, I understand now :) However, theres another problem because I declared Character me in my main function with set values, but when I print it in my void function, it doesn't use the values that I put from before. For example, when I print me.printsummary(), it doesn't print out the name or any of the information that I put in the main. Do you know how to fix this? Again, thank you so nuch! @Coder100

baolinchang (5)

Nevermind! I fixed it. I just declared Character me(myname,"Unpopular",-500,"Shy, close-minded, judgemental"); in the actual function instead of in the main. Thank you so much again for your [email protected]

Coder100 (6999)

:) No problem! Happy to help! @baolinchang

xxpertHacker (387)

Just like anything else, can you use a function before it's declared? No!
This doesn't work:

f();

void f() {...}

The same applies to classes! Also, new has meaning, don't use it unless you're allocating dynamic data.

F myVariable;

class F {...};

Do you see it now?

class F {...};

F myVariable;
baolinchang (5)

Thank you so much! I understand now, but I'm pretty sure I did declare it in my main function, however It doesn't work for some reason. @StudentFires

xxpertHacker (387)

@baolinchang You cannot declare classes inside functions at all; I know with 100% certainty you didn't declare it in main.

baolinchang (5)

Yes, I figured it out and changed it. Thank [email protected]