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.

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 getscore();
int getplayed();
int clacratio();
float getratio();
string getnick();
string getfirstname();
string getsurname();
string getemail();
};

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;
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::getplayed()
{
return played;
}

{
played+=y;
}

Player::getratio()
{
return ratio;
}

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

{
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


Share on other sites
You forgot your return types. MSVC defaults to return types of int, hence the error.

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 on other sites
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!

Share on other sites
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!}

Share on other sites
#pragma once does the same thing as guards.

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

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 on other sites
Are any of you reading what I said?
As long as it compliles on my machine and runs then it doesnt matter!

Share on other sites
ok whatever you say

--{You fight like a dairy farmer!}

Share on other sites
Sorry, I didn''t mean to sound rude. I really appreciate the help.
Thankyou :D

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

• Forum Statistics

• Total Topics
628736
• Total Posts
2984457

• 25
• 11
• 10
• 16
• 14