Archived

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

C++ Error Message that makes no sense

This topic is 5149 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 currently trying to write an application that will set up a database for Game Players to register with, to start off I have tried to set up a Class to take the player details and return them when requested. The only problem is I get a load of error messages that make no sense to me. Here is the code Player.h
#pragma once
#include <STRING>
using namespace std;


class Player
{
private:
	int score;
	string nickname, firstname, surname, email, password;
	int played;
	float ratio;
public:
	Player(string nick, string fname, string sname, string mail, string pass);
	~Player(void);
	int addscore(int x);
	int getscore();
	int addplayed(int y);
	int getplayed();
	int clacratio();
	float getratio();
	string getnick();
	string getfirstname();
	string getsurname();
	string getemail();
	string getpassword();
};
Player.cpp
#include "player.h"

Player::Player(string nick, string fname, string sname, string mail, string pass)
{
	nickname = nick;
	firstname = fname;
	surname = sname;
	email = mail;
	password = pass;
	score = 0;
	played = 0;
	ratio = 0;
}

Player::~Player(void)
{
}

Player::getnick()
{
	return nickname;
}

Player::getfirstname()
{
	return firstname;
}

Player::getsurname()
{
	return surname;
}

Player::getemail()
{
	return email;
}

Player::getpassword()
{
	return password;
}

Player::getplayed()
{
	return played;
}

Player::addplayed(int y)
{
	played+=y;
}

Player::getratio()
{
	return ratio;
}

Player::clacratio()
{
	ratio = (score/played);
	return 0;
}

Player::addscore(int x)
{
	score+=x;
	return 0;
}

Player::getscore()
{
	return score;
}
Errors
e:\Visual Studio Projects\FileIOTest\Player.cpp(25): error C2371: ''Player::getfirstname'' : redefinition; different basic types
e:\Visual Studio Projects\FileIOTest\Player.cpp(35): error C2371: ''Player::getemail'' : redefinition; different basic types
e:\Visual Studio Projects\FileIOTest\Player.cpp(20): error C2371: ''Player::getnick'' : redefinition; different basic types
e:\Visual Studio Projects\FileIOTest\Player.cpp(40): error C2371: ''Player::getpassword'' : redefinition; different basic types
e:\Visual Studio Projects\FileIOTest\Player.cpp(55): error C2371: ''Player::getratio'' : redefinition; different basic types
e:\Visual Studio Projects\FileIOTest\Player.cpp(30): error C2371: ''Player::getsurname'' : redefinition; different basic types
e:\Visual Studio Projects\FileIOTest\Player.cpp(35): error C2556: ''int Player::getemail(void)'' : overloaded function differs only by return type from ''std::string Player::getemail(void)''
e:\Visual Studio Projects\FileIOTest\Player.cpp(25): error C2556: ''int Player::getfirstname(void)'' : overloaded function differs only by return type from ''std::string Player::getfirstname(void)''
e:\Visual Studio Projects\FileIOTest\Player.cpp(20): error C2556: ''int Player::getnick(void)'' : overloaded function differs only by return type from ''std::string Player::getnick(void)''
e:\Visual Studio Projects\FileIOTest\Player.cpp(40): error C2556: ''int Player::getpassword(void)'' : overloaded function differs only by return type from ''std::string Player::getpassword(void)''
e:\Visual Studio Projects\FileIOTest\Player.cpp(55): error C2556: ''int Player::getratio(void)'' : overloaded function differs only by return type from ''float Player::getratio(void)''
e:\Visual Studio Projects\FileIOTest\Player.cpp(30): error C2556: ''int Player::getsurname(void)'' : overloaded function differs only by return type from ''std::string Player::getsurname(void)''
e:\Visual Studio Projects\FileIOTest\Player.cpp(61): warning C4244: ''='' : conversion from ''int'' to ''float'', possible loss of data
 
Please help!

Share this post


Link to post
Share on other sites
You forgot to specify the return types of functions in your source file.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You must suply the return type both in the declaration and in the definition of a function;

Player::getfirstname()

When you leave it out, the compiler think´s that the function will return an int.

Do

string Player::getfirstname()

and that goes for the rest of your function that returns a string





Share this post


Link to post
Share on other sites
except that:

#pragma once is not standard C++ code ... so why would you use it (most people I know always start from a basic template file anyway, and the guards are in there)

Share this post


Link to post
Share on other sites
I am using Visual Studio .NET and it set up the class like that. As long as it runs I aren''t too bothered as this for a University project. If it runs, it passes!

Thanks for the advice guys!

Share this post


Link to post
Share on other sites
Always beware when visualstudio does something like that, it may work in your compiler, but when you go and try to compil it on someone elses system, it may blow up

Share this post


Link to post
Share on other sites
I agree that you can use any proprietary extensions that work for you ... and I won''t hold that against you ...

but being INFORMED that they are proprietary is very valuable information ... so now you know, when you take that code elsewhere someday, you will need to run a conversion (I have one in PERL) to change files with #pragma once to #ifndef ...

if this never comes up, it never comes up ... but KNOWING is the key to not wasting hours confused about simple issues ...

just likely the decision about more complicated stuff, such as using POSIX, or pthread, or Win32 threading APIs, the key is not that you pick the one some poster suggests, but that the posters help you know enough to make an informed decision yourself.

I like #ifndef specifically because I use multiple compilers and platforms, if I used only MS, I wouldn''t care so much ...

Share this post


Link to post
Share on other sites