What's wrong with this class?

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Member {
private:
	char *name;
	int age;
	int rank;
public:
	static Mem(char *name, int age, int rank);{
	Member::name = name;
	Member::age = age;
	Member::rank = rank;}
	static Member::ShowMember();{cout << "\nName: " << name << "\nAge: " << age << "\nRank: " << rank;}
};


int main ()
{
	HANDLE hOut; hOut = GetStdHandle(STD_OUTPUT_HANDLE); 
	SetConsoleTextAttribute(hOut, BRIGHT_YELLOW);
	char *name;
	int age;
	int rank;
	Member::Mem("BillyBob", 27, 6);
	Member::ShowMember();
	wait(40); //user-defined function
	return 0;
}



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
1
2
3
4
5
6
7
8
9
10
11
12
class Member {
private:
	char *name;
	int age;
	int rank;
public:
	static Mem(char *name, int age, int rank);{
	Member::name = name;
	Member::age = age;
	Member::rank = rank;}
	static Member::ShowMember();{cout << "\nName: " << name << "\nAge: " << age << "\nRank: " << rank;}
};


Your constructor is all wrong, It must have the same name as the class. And why is it static?

1
2
3
4
5
6
7
8
9
10
public:
	Member(char *name, int age, int rank) {
		name = name;
		age = age;
		rank = rank;
	}
	
	ShowMember() {
		cout << "\nName: " << name << "\nAge: " << age << "\nRank: " << rank;
	}


You only need to use the 'Member::' when implementing the class' functions outside of the declaration.
Last edited on
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):

1
2
Member( const char* name, int age, int rank ) :
    name( name ), age( age ), rank( rank ) {}

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.
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Member {
private:
	string name;
	int age;
	int rank;
public:
	Member(string name, int age, int rank ) :
	name( name ), age( age ), rank( rank ) {}

	void ShowMember();{cout << "\nName: " << name << "\nAge: " << age << "\nRank: " << rank;}
};


int main ()
{
	HANDLE hOut; hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hOut, BRIGHT_YELLOW);
	string name;
	int age;
	int rank;
	Member::Member("BillyBob", 27, 6);
	Member::ShowMember();
	wait(40);
	return 0;
}


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'
Last edited on
jsmith:
Not sure you can do the assignment that way since the parameters shadow the data members.


Indeed, my bad. I'm pretty sure this works though :
1
2
3
4
5
Member(char *name, int age, int rank) {
		this->name = name;
		this->age = age;
		this->rank = rank;
	}


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.
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
1
2
	Member(string name, int age, int rank ) :
	name( name ), age( age ), rank( rank ) {}

or
1
2
3
4
5
Member(char *name, int age, int rank) {
		this->name = name;
		this->age = age;
		this->rank = rank;
	}


Sorry for asking so many questions by the way.

Thanks for your help,
Zach
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 :

1
2
3
4
5
6
7
8
9
10
11
12
int main ()
{
	HANDLE hOut; hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hOut, BRIGHT_YELLOW);
	string name;
	int age;
	int rank;
	Member myMember("BillyBob", 27, 6);
	Member.ShowMember();
	wait(40);
	return 0;
}


or, if you want to use pointers :

1
2
3
4
5
6
7
8
9
10
11
12
int main ()
{
	HANDLE hOut; hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hOut, BRIGHT_YELLOW);
	string name;
	int age;
	int rank;
	Member * myMember = new Member("BillyBob", 27, 6);
	Member->ShowMember();
	wait(40);
	return 0;
}


Notice the '.' changes to '->' when using pointers.

Asking questions is good ^^
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Member {
private:
	string name;
	int age;
	int rank;
public:
	Member(string name, int age, int rank ) :
	name( name ), age( age ), rank( rank ) {}

	void ShowMember(){cout << "\nName: " << name << "\nAge: " << age << "\nRank: " << rank;}
};


int main ()
{
	HANDLE hOut; hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hOut, BRIGHT_YELLOW);
	string name;
	int age;
	int rank;
	Member myMember("BillyBob", 27, 6);
	Member.ShowMember();
	wait(40);
	return 0;
}


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 '.'
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();"
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.
Is this problem not something to do with the namespace?

Maybe you should try:
using namespace std;
or
using std::string;
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
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.
Topic archived. No new replies allowed.