Hello, I am writing a small project in C ++ where I would like some classes to do some work, I wrote interfaces and class implementations.
What surprises me is that I cannot have a simple class without main (), I would like to have a class that was once created, methods can be called, do something, but I don't need (and want) main ( ) in the implementation of the class. Here is an example that I have in my head of what I would 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 name the animal class another file and make it work, something like this: The app.cpp file:
#include <neededlib>
#include "animal.h"
int main()
{
animal dog;
cout << dog.method1(42);
return 0;
}
But the compiler gave 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 the main one there, or do I need it?
Where am I mistaken?
luiss source
share