Sign in to follow this  

Help! strange Class errors

This topic is 4092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a class "character" for my game. As usual after writing the class's header and source, I try to declare an instance of my character object by linking the header file of the class to the main. I could declare the object but dont know why I couldnt call any of its functions. I am not new to classes, and I didnt have problem with it in my previous experience. The compiler I am using is Microsoft Visual Studio C++ 2005. I will show some of the codes from each of these files:
//character.h

#ifndef CHARACTER_H
#define CHARACTER_H

#include <string>
using std::string;

const short TOTAL_LANDS = 23;

class character
{
private:
        int gold;
        /* some private members */

public:                          
	character();
	character(string newname); 
	~character();

        void setGold(int totalGold);
        /* some public members */
};
#endif

//character.cpp

#include <string>
using std::string;

#include "character.h"

character::character()
{	//constructor
        ...
}

character::character(string newname)
{	//second constructor
        ...
}

character::~character()
{	//destructor
        ...
}   

void character::setGold(int totalGold)
{	//set the total gold
	gold = totalGold;
}

/* more member functions */

here is how I link it to the main file
//main.cpp

#include <windows.h>		// Header File For Windows
#include <stdio.h>			// Header File For Standard Input/Output
#include <gl\gl.h>			// Header File For The OpenGL32 Library
#include <gl\glu.h>			// Header File For The GLu32 Library
#include <gl\glaux.h>		// Header File For The Glaux Library

#include <string>
using std::string;

#include "character.h"		//include the player object

...

character test;
test.setGold(10);               // <-- this line fail

...

The compiler gives me these errors:
Compiling...
main.cpp
.\main.cpp(30) : error C2143: syntax error : missing ';' before '.'
.\main.cpp(30) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
.\main.cpp(30) : error C2365: 'lit' : redefinition; previous definition was 'function'
        .\main.cpp(29) : see declaration of 'lit'
sanguo - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I had compared it to my other projects which use classes but still couldnt find anything wrong with my codes. I already spent more than 3 hours just trying to make it to work. Anyone has any ideas of what I did wrong? Thanks!

Share this post


Link to post
Share on other sites
In C++ it is not legal to have any code outside of functions that is not a declaration.

You can have
character test;
outside of a function because it is a declaration.

test.setGold(10);
cannot be outside of a function however. Try putting it in main.

Share this post


Link to post
Share on other sites

This topic is 4092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this