undefined refernce while using classes

S

sam.barker0

Hi ,
I am stuck with a simple problem.Its eating fri=om with in

My code is as below
in the file where main is

#include class.h
int main()
{
classa ex;
}

in .h file
class classa
{
classa();
~classa();
};

in the .cc file
#include class.h
classa::classa()
{}
classa::~classa()
{}

But g++ is throwing an error in main "undefined reference to
classa::classa()"
whats am i doing wrong?
 
R

Rahul

Hi ,
I am stuck with a simple problem.Its eating fri=om with in

My code is as below
in the file where main is

#include class.h
int main()
{
classa ex;

}

in .h file
class classa
{
classa();
~classa();

};

in the .cc file
#include class.h
classa::classa()
{}
classa::~classa()
{}

But g++ is throwing an error in main "undefined reference to
classa::classa()"
whats am i doing wrong?

Why are you having the constructor and destructor as private and
trying to create an object of the class in main()?

Second, is your main() defined in the header file?
 
M

Michael.Boehnisch

in .h file
class classa
{
classa();
~classa();

};
whats am i doing wrong?

try:

class classa {
public:
classa();
~classa();
};

In C++, class members are private by default. If you forget the
"public:", objects of type classa cannot be instantiated directly. You
would need a public object factory function inside classa. --- This is
wanted behavior sometimes.
I don't have gcc at hand quickly and the error it gives you does not
seem to match to this mistake, so there may be wrong more with your
real code. The code you gave is obviously not copy-paste from your
source (e.g. missing <...> or "..." around the #include files.).
However, fix this first and see if your problem goes away.

best,

Michael.
 

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

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,478
Latest member
ReginaldVi

Latest Threads

Top