I have a project for university that I managed to get working using my own makefile/terminal command. BUT I get zero marks on the university's auto marker. I THINK this is because they use a makefile that tries to execute all three cpp files in one line with the compiler(if that's even possible), it's difficult to tell, because the marker doesn't give any feedback. When I try to compile all three .cpp files(g++ main.cpp A.cpp B.cpp) I get the following error messages:
- B.cpp:4:3: error: redefinition of ‘T B::getVal()’ T B::getVal(){
- A.cpp:4:6: error: redefinition of ‘void A::print(B)’ void A::print(B bInstance){
In my MRE the following needs to stay true:
- A and B must have a .h and .cpp file
- main.cpp may only include "A.h"
- A must require B for it to work
Here is my MRE: -It makes an instance of B and then uses an instance of A to print a message with the value of instance B.
main.cpp
#include "A.h"
int main(){
B<char> b1;
b1.val='B';
A<char> a1;
a1.print(b1);
}
A.h
#ifndef AAAA
#define AAAA
#include<iostream>
#include "B.h"
using namespace std;
template <class T>
class A{
public:
void print(B<T> bInstance);
};
#include "A.cpp"
#endif
A.cpp
#include "A.h"
template <class T>
void A<T>::print(B<T> bInstance){
cout<<"I am A, and I am using "<<bInstance.getVal()<<endl;
}
B.h
#ifndef BBBB
#define BBBB
template <class T>
class B{
public:
T val;
T getVal();
};
#include "B.cpp"
#endif
B.cpp
#include "B.h"
template <class T>
T B<T>::getVal(){
return val;
}
Working compile and run commands
g++ main.cpp
./a.out
Commands it should work with(if possible)
g++ main.cpp A.cpp B.cpp
./a.out