Question
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?
Solution
but I don't need (nor want) a main() in the class implementation.
The function main
is your entry-point. That is where execution begins. You need to have one and only one such function.
But compiler give me "undefined reference to main" for animal.cpp, but I don't need a main there, or do I need it?
Now, your problem looks like you have not linked the compiled forms of app.cpp
and animal.cpp
.
I'm not so strong in Makefiles, I used something like g++ animal.h -o animal and g++ animal.cpp but it gives me the error above
You don't compile headers. So don't use: g++ animal.h
When you compiled the animal.cpp separately, g++ created an object file. You will also need to compile the app.cpp
because you do need the main
. Once you compile the app.cpp
file you will have to link it with the animal
object file created earlier. But if these did not get linked in, specially, the file containing the main
function you will hit the error you are getting now.
However, g++
takes care of what I have described above. Try something like this:
g++ animal.cpp app.cpp -o test
This will create an executable called test
and you can run your application using:
./test
OTHER TIPS
First, this is wrong: animal dog = new animal();
You either need
animal * dog = new animal();
cout << dog->method1(42);
delete animal;
or preferably just
animal dog;
Second, you need to link together the object files produced by compiling your two source .cpp files.
gcc -o animal animal.cpp app.cpp
should do the trick, if you're using gcc.
You should either compile them together (in a single g++
command) or compile both of them to object files (g++ -c
) and then use ld
to link the object files together.
Sample Makefile:
all: app.exe
app.exe: app.o animal.o
g++ -o app.exe app.o animal.o
app.o: app.cpp
g++ -c -o app.o app.cpp
animal.o: animal.cpp animal.h
g++ -c -o animal.o animal.cpp
class animal
{
public:
animal();
~animal();
public:
method1(int arg1);
private:
int var1;
};
Notice you didn't put a semi-colon at the end of the class declaration. C++ requires it and sometimes gives confusing/misleading error messages if you forget it.
public:
method1(int arg1);
You are missing the return type for method1. Did you mean something like
int method1(int arg1);