C++ Error Message that makes no sense

Started by
13 comments, last by ArcticTiger 20 years, 6 months ago
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!
Advertisement
You forgot your return types. MSVC defaults to return types of int, hence the error.
You forgot to specify the return types of functions in your source file.

"Sneftel is correct, if rather vulgar." --Flarelocke
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





Oops!
Thanx guys!
You''ll want to put code guards up in your head file too.

eg.
#ifndef _PLAYER_H#define _PLAYER_Hclass Player{    ...}#endif





--{You fight like a dairy farmer!}

--{You fight like a dairy farmer!}

#pragma once does the same thing as guards.
____________________________________________________________AAAAA: American Association Against Adobe AcrobatYou know you hate PDFs...
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)
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!
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

This topic is closed to new replies.

Advertisement