By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,401 Members | 803 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,401 IT Pros & Developers. It's quick & easy.

Error: function already has a body

P: n/a
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.

Apr 16 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
na***********@gmail.com wrote:
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
Apr 17 '06 #2

P: n/a
In article <11*********************@g10g2000cwb.googlegroups. com>,
na***********@gmail.com wrote:
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.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.