What?

Started by
18 comments, last by Wachar 22 years ago
This code generates 4 errors. Why?
  
#include <iostream>
using namespace std;
//------------------------------------Function Declarations-----------------------------------------

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

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

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

struct Aliens
{
public:
	char * userName;
	int health = 80;
	Aliens()
	{}
	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 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!
Wachar's Eternity <-<-<-<-<- Me own site!
Advertisement
It would help to show us what errors you''re getting.
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!

Wachar's Eternity <-<-<-<-<- Me own site!
I dont really like your syntax. You write that it is classes that you define so then why don''t you?

Here are some advice:

- 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.

Read some about structs and classes, their similariries and their differences.

Good luck!


Why make it simple when you can make it sooo nice and complicated?
Why make it simple when you can make it sooo nice and complicated?
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 ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
I know c++ fairly well!

Take it to the Xtreme!

Wachar's Eternity <-<-<-<-<- Me own site!
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 ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
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]
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";}  


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]
-------------Ban KalvinB !

This topic is closed to new replies.

Advertisement