Member Avatar for icewizard

I have a very simple question. I am trying to link the following 2 files, but I get an error saying "multiple definition of function()"

In a.cpp

int function(){
return 1;
}


In main.cpp

#include "a.cpp"
int main(){
return 0;
}


I know I can fix the error by making a header file for a.cpp, or putting function() inside a class, but I'm wondering what is causing this error, since I only defined function() once. Thanks.

Recommended Answers

All 3 Replies

> since I only defined function() once.
you defined it twice. once in a.cpp and once in main.cpp (which includes a.cpp)

Member Avatar for icewizard

Oh ok, are there any simpler ways to fix this problem than making a separate header file or putting all the functions in a.cpp inside a class. And, I don't want to just put everything in my main.cpp file.
Thanks.

The best method is just to make a header file for every source file. Source files aren't really supposed to be #include'd anyway, for the very reason you had your error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.