0

I am very new to c++ and I am having trouble with circular dependencies. I have these classes that call each other, and now I have no clue how to compile this code correctly.

Main.cpp

#include "ClassB.hpp" //Rate
#include "ClassC.hpp" //Molecule
#include "ClassA.hpp" //Matrix

using namespace std;

int main(int argc, char **argv) {
    ClassC CaH (some_var);
    return 1;
}

ClassA.hpp

using namespace std;

template< class T > class ClassA : public vector< vector< T > > {
    //variables

    Public:
        //variables and functions
};

ClassA.cpp

#include "ClassA.hpp"

using namespace std;

ClassA::ClassA() : vector< vector< T > >() { }

ClassA::some_function ()

//more functions

ClassB.hpp

#include "ClassA.hpp"
#include "ClassC.hpp"

using namespace std;

class ClassB {
    friend class ClassC;

    private:
        //variables
        ClassC var1;
        ClassA<long double> var2;

    public:
        //variables
        ClassB (ClassC&);
};

ClassB.cpp

#include "ClassA.hpp"
#include "ClassB.hpp"
#include "ClassC.hpp"

using namespace std;

ClassB::ClassB(ClassC& m) {
    var1 = *m;
}

ClassB::some_functions //these functions call ClassC variables

ClassC.hpp

using namespace std;

class ClassC {
    friend class ClassB;

    private:
        //some variables
        run_stuff();

    public:
        //some variables
        ClassC ();
        ClassC(some_var);
};

ClassC.cpp

#include "ClassB.hpp"
#include "ClassC.hpp"

using namespace std;

Molecule::Molecule() { }

Molecule::Molecule(some_var) {
    //do stuff
}

run_stuff() {
    for loop:
        ClassB r (this);
        // do stuff
}

Here is the command that I am using to compile right now. It is giving pages upon pages of errors. "error: previous definition of ‘class", "error: redefinition of ‘class" "error: no matching function for call to" "no known conversion for argument 1 from ‘ClassC*’ to ‘ClassC&’" "‘variable’ was not declared in this scope" "error: invalid use of ‘this’ in non-member function" "‘template class ClassA’ used without template parameters"

Compile Command

g++ -std=c++11 lib1.cpp ClassA.cpp ClassC.cpp -lib2 ClassB.cpp  Main.cpp -o main

I never had any of these other problems when I had all of class in the same main.cpp file. I just had a problem with forward declarations being not enough for the circular dependence. Now I am stuck with compiling. Do I change the order in the compile command?

S. J.
  • 11
  • 3
  • You need to use #ifdef or some other method to make sure that headers are only included once. – john elemans Feb 16 '17 at 23:32
  • 2
    use [include guard](https://en.wikipedia.org/wiki/Include_guard) – felix Feb 16 '17 at 23:35
  • See also http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file . `ClassA`, being a template, must be implemented entirely in the header file. – Igor Tandetnik Feb 17 '17 at 00:06

0 Answers0