I am trying to write a series of programs that use the same main file (main.cpp), but with different source files (object1.cpp, object2.cpp, etc.). Therefore, I will compile them mainly like this:
g++ -o program1.exe main.cpp object1.cpp g++ -o program2.exe main.cpp object2.cpp
I want objectN.cpp to define a class with some implemented methods that will be called from the main file. The source codes look something like this:
header file (object.hpp)
#ifndef INCLUDE_OBJECT_HPP #define INCLUDE_OBJECT_HPP class MyObjectInterface { public: MyObjectInterface(); virtual ~MyObjectInterface() {}; virtual void MethodA() = 0; virtual void MethodB() = 0; }; #endif
object1.cpp
#include <iostream>
main.cpp
#include <iostream> #include "object.hpp" using namespace std; MyObjectInterface *x; int main() { x = new MyObject(1); x->MethodA(); x->MethodB(); return 0; }
object2.cpp will have a similar structre for object1.cpp, but the class will have different data members.
I can't get this to work because I need to include the declaration of the MyObject class in main.cpp. But each object * .cpp file will declare a MyObject class with different members, so I canβt just create a separate object.hpp header and include it in main, because I need different headers for different objects. The main thing you need to know about is methods.
I hope I have clearly explained the problem; if you do not leave a comment, and I will try to clarify. There seems to be a really easy way to do something like this, but I can't figure it out.
thanks
smead source share