Archived

This topic is now archived and is closed to further replies.

What?

Recommended Posts

Wachar    127
This code generates 4 errors. Why?

#include <iostream>
using namespace std;
//------------------------------------Function Declarations-----------------------------------------

int humanSetData();
int alienSetData();
//------------------------------------------Classes-------------------------------------------------

/////////////////////////////////////////////HUMANS/////////////////////////////////////////////////

struct Humans
{
public:
int health = 80;
Humans()
{}
void main_human_story();
}human1;
/////////////////////////////////////////////ALIENS/////////////////////////////////////////////////

struct Aliens
{
public:
int health = 80;
Aliens()
{}
void main_alien_story();
}alien1;
////////////////////////////////////////GLOBAL VARIABLES////////////////////////////////////////////

//--------------------------------------------MAIN--------------------------------------------------

int main()
{
//Any temporary variables

int choice;

cout << "\"Come on!\"  You''re fellow beings are in a frenzy!  Help them survive!\n\n";
cout << "Choose you''re race now; 1 - Humans OR 2 - Aliens: ";
cin >> choice;

switch(choice)
{
case 1:
{
humanSetData();
human1.main_human_story();
break;
};
case 2:
{
alienSetData();
alien1.main_alien_story();
break;
}

default:break;
}
return(0);
}

int humanSetData()
{
char temp_name[30];

cout << "\n\n\"Alrighty then!  Let''s get youse registered!\nName: ";

cin >> temp_name;

cout << "\n\n\"All right, welcome aboard " << temp_name
<< "!.  Let''s get down to business quick like!\n"
<< "You''ll be given an armor which will shield you equal to the amount of 80 health.\n"
<< "Remember, this will drain quickly if you don''t take care of it.\n"
<< "You know what''s happening out there. So get out and help us win this battle!\"\n\n";

return(0);

}

void main_human_story()
{
cout << "main_human_story() called";
}

int alienSetData()
{
return(0);
}

void main_alien_story()
{
cout << "main_alien_story() called";
}


Take it to the Xtreme!

Share on other sites
krikkit    792
It would help to show us what errors you''re getting.

Share on other sites
Wachar    127
Compiling...
Main.cpp
C:\Program Files\Microsoft Visual Studio\My Games\GeneralGame\Main.cpp(12) : error C2258: illegal pure syntax, must be ''= 0''
C:\Program Files\Microsoft Visual Studio\My Games\GeneralGame\Main.cpp(12) : error C2252: ''health'' : pure specifier can only be specified for functions
C:\Program Files\Microsoft Visual Studio\My Games\GeneralGame\Main.cpp(22) : error C2258: illegal pure syntax, must be ''= 0''
C:\Program Files\Microsoft Visual Studio\My Games\GeneralGame\Main.cpp(22) : error C2252: ''health'' : pure specifier can only be specified for functions
Error executing cl.exe.

Main.obj - 4 error(s), 0 warning(s)

Take it to the Xtreme!

Share on other sites
clabinsky    122
I dont really like your syntax. You write that it is classes that you define so then why don''t you?

- structs are not classes.
- all mambers in a struct are public by default
- you cannot assign a value to a variable in the declaration of a class, unless...
- to define a pure virtual function you use: virtual = 0
- Each function must have a return type. void, int, double etc
- I dont think you can declare a virtual function in a struct.

Good luck!

Why make it simple when you can make it sooo nice and complicated?

Share on other sites
Oluseyi    2116
quote:
Original post by clabinsky
- structs are not classes.

Structs are classes. We''ve been over this several times on this site, so search for the threads. (Unions are also special cases of classes).

quote:
- you cannot assign a value to a variable in the declaration of a class, unless...

...it is static. Defining a pure virtual doesn''t declare/define a variable.

quote:
- Each function must have a return type. void, int, double etc

C/C++ default to int.

quote:
- I dont think you can declare a virtual function in a struct.

You can.

As to Wachar: Get a good book and learn the language before trying to make a game.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
Thanks to Kylotan for the idea!

Share on other sites
Wachar    127
I know c++ fairly well!

Take it to the Xtreme!

Share on other sites
Oluseyi    2116
quote:
Original post by Wachar
I know c++ fairly well!

Actually, you apparently don''t. You don''t know what your errors mean or why they occur, and they''re fundamental errors. You aren''t familiar with the documentation of your tools and can''t track down common causes for your errors.

This isn''t a put-down, but admitting ignorance is the first step to gaining knowledge. We all began knowing nothing, and we''re all still learning.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
Thanks to Kylotan for the idea!

Share on other sites
romer    323
Despite knowing very little about C++, I do a spot a couple of errors. First of all, you can't explicitly assign a value to your data members when you define the class [i.e, the int Health = 80;]. That's the job of the constructor to initialize the values of your data members to default values. Second thing is when you define your main_human_story() and main_alien_story() member functions, you need to tell what class its part of. This is down at the bottom of your file. Right now you have this:

void main_human_story()
{
cout << "main_human_story() called";
}

void main_alien_story()
{
cout << "main_alien_story() called";
}

What you want is this:

void Humans::main_human_story()
{
cout << "main_human_story() called";
}

void Aliens::main_alien_story()
{
cout << "main_alien_story() called";
}

Hope this helps out and good luck!

edit: smiley code messed up one of my closing parenthesis, just fixed that a bit

[edited by - MRom on April 28, 2002 11:30:29 PM]

Share on other sites
Guest Anonymous Poster
MRom is right

  #include <iostream>using namespace std;//------------------------------------Function Declarations-----------------------------------------int humanSetData();int alienSetData();//------------------------------------------Classes-------------------------------------------------/////////////////////////////////////////////HUMANS/////////////////////////////////////////////////struct Humans{public:	char * userName;	int health;;	Humans()	{      health = 80;   }	void main_human_story();}human1;/////////////////////////////////////////////ALIENS/////////////////////////////////////////////////struct Aliens{public:	char * userName;	int health;	Aliens()	{      health = 80;   }	void main_alien_story();}alien1;////////////////////////////////////////GLOBAL VARIABLES//////////////////////////////////////////////--------------------------------------------MAIN--------------------------------------------------int main(){	//Any temporary variables	int choice;	//Choose your race	cout << "\"Come on!\"  You''re fellow beings are in a frenzy!  Help them survive!\n\n";	cout << "Choose you''re race now; 1 - Humans OR 2 - Aliens: ";	cin >> choice;	switch(choice)	{	case 1:		{			humanSetData();			human1.main_human_story();			break;		};	case 2:		{			alienSetData();			alien1.main_alien_story();			break;		}	default:break;	}return(0);}int humanSetData(){	char temp_name[30];		cout << "\n\n\"Alrighty then!  Let''s get youse registered!\nName: ";	cin >> temp_name;	human1.userName = temp_name;	cout << "\n\n\"All right, welcome aboard " << temp_name 		 << "!.  Let''s get down to business quick like!\n"		 << "You''ll be given an armor which will shield you equal to the amount of 80 health.\n"		 << "Remember, this will drain quickly if you don''t take care of it.\n"		 << "You know what''s happening out there. So get out and help us win this battle!\"\n\n";		return(0);}void Humans::main_human_story(){	cout << "main_human_story() called";}int alienSetData(){	return(0);}void Aliens::main_alien_story(){	cout << "main_alien_story() called";}

Share on other sites
granat    122
quote:
Original post by Oluseyi

Structs are classes. We've been over this several times on this site, so search for the threads. (Unions are also special cases of classes).

I suppose a signed int is just a special case of an unsigned int but still it is NOT the same thing although similar.

We might have been over this several times but did everyone agree on the subject ?

[edited by - granat on April 29, 2002 1:54:46 AM]

Share on other sites
Oluseyi    2116
quote:
Original post by granat
I suppose a signed int is just a special case of an unsigned int but still it is NOT the same thing although similar.

Nope, an unsigned int is a signed int constrained to the natural numbers (ie, the set of natural numbers is a subset of the set of integers, which is a subset of the set of real numbers). Theoretically speaking, of course.

However, a struct is a class in C++. The differences are public rather than private default access specification and public rather than private default inheritance. You can have virtual functions, inherit from the suckers and so on and so forth.

quote:

We might have been over this several times but did everyone agree on the subject ?

Loser. Loo-hoo-hoo-hoo-ser-eeerr!

Seriously, though, this isn''t a question of opinion; it''s a statement of fact. Feel free to disagree. At least the next time you get a roasting on this issue I can smirk and say "I told you so".

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
Thanks to Kylotan for the idea!

Share on other sites
granat    122
quote:
Original post by Oluseyi
Howewer, a struct is a class in C++. The differences are public rather than private default access specification and public rather than private default inheritance.

Hmm..Sounds like they are SIMILAR

Share on other sites
granat    122
Consider this.

A panther is just a black leopard (I've heard that's the only difference). If I'm wrong about this I'm sure I can think of another example.

Would you say that a panther IS a leopard ? I would not. I would say that they are very similar. But there is a reason they have been given two seperate words.
If you say that a panther IS a leopard then we probably agree in reality but just not on definition.
I say if there is the slightest difference then it is not the same.

End of anal

[edited by - granat on April 29, 2002 4:24:25 AM]

Share on other sites
SabreMan    504
quote:
ISO/IEC 14882: Programming Languages - C++
A structure is a class defined with the class-key struct; its members and base classes are public by default. A union is a class defined with the class-key union; its members are public by default and it holds only one data member at a time.

Please feel free to argue with the Standard committee. If it helps, you may draw up whatever inappropriate analogies help you to feel better about being wrong.

[C++ FAQ Lite | ACCU | Boost | Python]

Share on other sites
granat    122
I wouldn''t want to disagree with the Emperial Standard Committee.

I still say it''s a matter of definition but it''s not like it matters....

Share on other sites
clabinsky    122
Thanks for the lecture Oluseyi!
I appreciate you being very active, it helps a lot.

However, I agree and don''t agree on what you are saying. Ok, structs are kind of classes with all public members.

However, there is a difference between classes and structs, hence, structs are not classes. Closely related, but not identical. Knitty gritty details, but I wont consider them the same

Didn''t know that C/C++ defaults return types to int which by the way explains why the compiler did not complain above. So, I''ll change my advice: functions should have a return type - don''t depend on the default values, it might be implemented differently between compilers.

The rest is just something I add to my small pile of knowledge.

Share on other sites
SilentCoder    122
quote:
Original post by clabinsky

So, I''ll change my advice: functions should have a return type - don''t depend on the default values, it might be implemented differently between compilers.

Those were constructors, they don''t have a return type.

Share on other sites
SabreMan    504
quote:
Original post by clabinsky
However, there is a difference between classes and structs, hence, structs are not classes.

Let''s try again. A struct *is a* class. Just because it has different default access does not make it a different thing. You can consider a struct as a specialisation of a class if it makes you happier, but it is still a class.
quote:

Didn''t know that C/C++ defaults return types to int which by the way explains why the compiler did not complain above.

Yes, but this is strictly illegal. Explicit return types should be returned, but most compilers will default to int if omitted.

[C++ FAQ Lite | ACCU | Boost | Python]

Share on other sites
Oluseyi    2116
quote:
Original post by clabinsky
However, I agree and don''t agree on what you are saying. Ok, structs are kind of classes with all public members.

However, there is a difference between classes and structs, hence, structs are not classes. Closely related, but not identical. Knitty gritty details, but I wont consider them the same

You would so fail a test on inheritance.
class Base{  // yatta};class Derived : public Base{  // yatta};

Is Derived a Base? Same with class and struct, and class and union.

Nuff said.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
Thanks to Kylotan for the idea!

Share on other sites
Wachar    127
Silly me! I can''t believe I forgot about the : in the construcors(to set variables)! Also about the :: for the scope. Sorry Oluseyi and everyone else who had to put up with this! I promise I won''t post anything of this sort unless I''m in desperate need of it! Thanks again!

Take it to the Xtreme!

• 9
• 11
• 9
• 11
• 14