Hello I'm writing a little project in c++ where I would like to have some classes that does some work, I wrote the interfaces and the implementation of the classes.
The thing that surprises me is that I cannot have a simple class without a main(), I would like to have a class that once instantiated, It's methods can be called, do things, but I don't need (nor want) a main() in the class implementation.
Here's an example I have in my head of what I'd like to have:
file animal.h:
class animal
{
public:
animal();
~animal();
public:
int method1(int arg1);
private:
int var1;
};
file animal.cpp:
#include "animal.h"
animal::animal(){...}
animal::~animal(){...}
int animal::method1(int arg1){return var1;}
}
And I would like to call the animal class form another file and have it work, something like this:
file app.cpp:
#include <neededlib>
#include "animal.h"
int main()
{
animal dog;
cout << dog.method1(42);
return 0;
}
But compiler give me
/usr/lib/gcc/i686-pc-linux-gnu/4.3.3/../../../crt1.o: In function _start:
"(.text+0x18): undefined reference to `main`"
collect2: ld returned 1 exit status
for animal.cpp, but I don't need a main there, or do I need it?
Where Am I wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…