I’ve hit my first assignment requiring object oriented programs. I started working on the (rather large and complicated) project and hit a few snags when it came to linking multiple class files and their associated .h files. Basically I keep getting “Multiple Definition of Class” errors, even though I have my Include guards and theres no Global Variables. I won’t post my code for the assignment itself, but here’s a much simplified example giving me an identical error:
//Runner file.
#include "Class1.cpp"
int main(){
Class1 classtest();
return 0;
}
–
//Class1.h
#ifndef CLASS1_H
#define CLASS1_H
class Class1
{
public:
Class1();
};
#endif // CLASS1_H
–
//Class1.cpp
#include "Class1.h"
#include <iostream>
#include "Class2.cpp"
using namespace std;
Class1::Class1()
{
cout << "Created Test Class1";
Class2 testClass();
}
And then nearly identical Class2 files
//Class2.h
#ifndef CLASS2_H
#define CLASS2_H
class Class2
{
public:
Class2();
};
#endif // CLASS2_H
–
#include "Class2.h"
#include <iostream>
using namespace std;
Class2::Class2()
{
cout << "Created Class 2";
}
The whole thing then generates around 1.4 metric craptons of errors, most of which are in the format:
obj\Debug\Runner.o||In functionClass2′:|C:\Users\MikeD\Desktop\C++\ObjectOTesting\Class2.cpp|6|
multiple definition ofClass2::Class2()'|
obj\Debug\Class2.o:C:\Users\MikeD\Desktop\C++\ObjectOTesting\Class2.cpp|6|first defined here|
I KNOW I’m doing something stupid here, but I haven’t been able to find out what exactly. Any help is really appreciated.
You don’t
#includeCPP files — you compile them. CPP files, in turn,#includeH files.