What's wrong with this class?
| zachlr (17) | |||
| I'm currently learning about classes, and I'm trying to make a simple program to create, and show information about a member of a club, guild, etc. Here is part of my code so far:
Here is the compiler/debugger output: [ln 7]error C4430: missing type specifier - int assumed. Note: C++ does not support default-int [ln 7]error C2059: syntax error : '{' [ln 7]error C2334: unexpected token(s) preceding '{'; skipping apparent function body [ln 11]error C4430: missing type specifier - int assumed. Note: C++ does not support default-int [ln 11]error C2059: syntax error : '{' [ln 11]error C2334: unexpected token(s) preceding '{'; skipping apparent function body ------------- I am using Visual studio 2008. Can someone please tell me what's wrong so I can better understand classes? Thanks, Zach | |||
| enixi0s (33) | |||||
Your constructor is all wrong, It must have the same name as the class. And why is it static?
You only need to use the 'Member::' when implementing the class' functions outside of the declaration. | |||||
| jsmith (158) | |||
Not sure you can do the assignment that way since the parameters shadow the data members. But initializing the members in an initializer list would work (C++ standard explicitly allows for it):
| |||
| Zaita (729) | |||
| You should also be using string instead of char* for the name. It will handle memory allocation and de-allocation for you. That way you don't end up with memory leaks. | |||
| zachlr (17) | |||
| Okay, I see now. static Mem was my mistake. I didn't realize it had to be the same, and non-static. The tutorial told me to have the parameters shadow the data members. Is that incorrect? So what you are saying is 'Class::Function' is only used for functions, not variables, and only used outside if the class? Clearly, the tutorial I watched was incorrect. I should be using string instead of char *? The tutorial was incorrect in that aspect as well. This my current code:
My compiler says: [ln 10] error C2059: syntax error : '{' [ln 10] error C2334: unexpected token(s) preceding '{'; skipping apparent function body [ln 22] 'Member::ShowMember' : illegal call of non-static member function --- see declaration of 'Member::ShowMember' | |||
| enixi0s (33) | ||||
jsmith:
Indeed, my bad. I'm pretty sure this works though :
zachlr : Your error on line 10 is beacause you put a semi-colon (; just in case ^^) after "void ShowMember()". This tells the compiler that the function will be implemented later and so it doesn't expect your code block after it. You seem to be mixing up two ways of declaring classes, maybe you should read the tutorial : http://www.cplusplus.com/doc/tutorial/classes.html The last example shows a mix of the two ways. | ||||
| zachlr (17) | |||||
| Oh I see. The semi colon set the code block out of place. That was a stupid mistake, I should know better than that. I read the tutorial, but it's a little confusing. I'm trying to learn C++ just from tutorials and forums, because my school doesn't offer C++ courses for my grade. Let me take a guess, should I be doing Member.Showmember ? And using Member::ShowMember to declare the function and move it out of the class declaration or not? Should I be using
or
Sorry for asking so many questions by the way. Thanks for your help, Zach | |||||
| enixi0s (33) | |||||
| Fist of all you can use either constuctor; jsmith's is much cleaner and initialises the variables rather than crating them, then setting the value (I think, anyone who can verify??) When you create a class, you can then use it like any pre-defined C++ class. A string for example. So your code could look like this :
or, if you want to use pointers :
Notice the '.' changes to '->' when using pointers. Asking questions is good ^^ | |||||
| zachlr (17) | |||
| Ok, I took your first method (nether would compile). I'm not really sure where the 'my' in 'myMember' came from, but if you say so. My code looks like this:
Compiler: [ln 10] error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) --- some big long error thing that makes no sense --- [ln 22] error C2143: syntax error : missing ';' before '.' [ln 22] error C2143: syntax error : missing ';' before '.' | |||
| enixi0s (33) | |||
| Member is the type, myMember is just the name of a vairable, you could call it 'x' if you so chose to. I am indeed an idiot. Its not "Member.ShowMember();", but "myMember.ShowMember();" | |||
| zachlr (17) | |||
| So it goes: classname func_var("FunctionParams", 34, 9) func_var.ShowMember(); func_var is used to tell ShowMember() which person to display. I'm not used to having a variable in the function name spot. In my world, it would go var = FuncName("params", 35, 5), as in myMember = Member("BillyBob", 27, 6). But what are you gonna do? Okay, that got rid of the one error. I'm still getting "error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)" (Then it has a huge thing I can't understand but it's too big for a post) which is talking about
void ShowMember(){cout << "\nName: " << name << "\nAge: " << age << "\nRank: " << rank;}And I'm not sure that is even class related. I never got it before, and I can't remember what I changed when I got it the first time. Leave it to me to be able to mess up something as simple as "<<". You're not at all an idiot, we all make mistakes. | |||
| enixi0s (33) | |||
| Is this problem not something to do with the namespace? Maybe you should try: using namespace std; or using std::string; | |||
| zachlr (17) | |||
I did have
using namespace std; in the original code. You just couldn't see it because I only showed part of the code. My fault.
using namespace std; 1 error, binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
using std::string;1 error, 'cout' : undeclared identifier
using std::string; using namespace std; // (both) 1 error, binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) I have had << working many times before, now all of the sudden it won't work. I wouldn't imagine the addition of a class would cause this. Would it? It doesn't make any sense! :S | |||
| zachlr (17) | |||
| Ok, never mind. I used using std::string, but didn't realize I also needed to use #include <string>. Finally! My program works! Thanks for all of your help everyone. | |||
This topic is archived - New replies not allowed.
