Error: function already has a body

N

nabeel.girgis

When I compile, I get two error messages per member function I defined
in my class declaration. The first error states the function name and
says that it already has a body. While the second error message says
to see previous definition of the function.

Here is an example. These are my constructors declarations and their
corresponding function code:

card ();


card::card()
{
}


card (int face, char csuit);

card::card(int face, char csuit) : rank(face), suit(csuit)
{
}
I copied and pasted these directly from my file. Thanks.
 
L

Larry I Smith

When I compile, I get two error messages per member function I defined
in my class declaration. The first error states the function name and
says that it already has a body. While the second error message says
to see previous definition of the function.

Here is an example. These are my constructors declarations and their
corresponding function code:

card ();


card::card()
{
}


card (int face, char csuit);

card::card(int face, char csuit) : rank(face), suit(csuit)
{
}
I copied and pasted these directly from my file. Thanks.

Here is one way to do it (there are several ways)...

--- In the file card.hpp:

// definition of the class 'card' (no inline methods).
// this header (card.hpp) can be included by any source
// file (.hpp or .cpp) that needs to know about the
// 'card' class.
class card
{
public:
card();
card(int face, char csuit);
// declare any other public methods here...

private:
int rank;
char suit;
};


--- In the file card.cpp:

#include "card.hpp"

// implement the funtions of class 'card'...

card::card() : rank(0), suit(0)
{
// do any other constructor stuff here...
}

card::card(int face, char csuit) : rank(face), suit(csuit)
{
// do any other constructor stuff here...
}

// implementation of other 'card' methods here...

---

Which C++ tutorial book are your reading?

Regards,
Larry
 
D

Daniel T.

When I compile, I get two error messages per member function I defined
in my class declaration. The first error states the function name and
says that it already has a body. While the second error message says
to see previous definition of the function.

Here is an example. These are my constructors declarations and their
corresponding function code:

card ();


card::card()
{
}


card (int face, char csuit);

card::card(int face, char csuit) : rank(face), suit(csuit)
{
}
I copied and pasted these directly from my file. Thanks.

I suspect you are including something more than once, or possibly
including a cpp file into another cpp file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top