0

I'm trying to instantiate a few classes, with references to the first class passed along.

The compiler gives me an error stating: error: 'classData' is not a type.

ClassData hold some complicated data structures and has a bunch of accessors to that data. ClassFunc has a bunch of functions that operate on that data. Then the Work class does a bunch of work and occasionally needs to call a function in ClassB that will do some work on the data in ClassData.

Below is the code:

/////////////////////////
//ClassData.h
class ClassData {
public:
    ClassData(){
        // initialize a bunch of stuff
    };
    virtual ~ClassData(){};
}

/////////////////////////
//ClassFunc.h
#include "ClassData.h"

class ClassFunc {
public:
    ClassFunc(ClassData& in_classData) : classData(in_classData){};
    virtual ~ClassFunc();

    float updateEta(float deltaVJ, int column);

private:
    ClassData& classData;

};

/////////////////////////
//ClassFunc.cpp
#include "ClassFunc.h"

float ClassFunc::updateEta(float a, int b){
    float foo = 0.0
    // Do a bunch of work to foo
    return foo;

};

/////////////////////////
// Work.h
#include "ClassData.h"
#include "ClassFunc.h"

class Work{

public:
    Work(ClassData& in_class) :  classData(in_class){
        // initialize some stuff
    };
    ~Work(){};

    float updateTheta(int a, float b, float c);

private:
    ClassData& classData;
    ClassFunc classFunc(classData);  //// ERROR IS HERE
}

/////////////////////////
//  Work.cpp
#include "Work.h"
float Work::updateTheta(int a, float b, float c){

    // do some work first
    double foo = classFunc.updateEta(d, e);
    return foo
};
Noah
  • 567
  • 8
  • 19
  • You're missing semicolons after class definitions. Is this your actuall code? – jrok Mar 30 '12 at 17:51
  • ClassB(Class A& in_classA) --> You seem to have an extra space "Class A" vs. "ClassA" – Fred Mar 30 '12 at 17:52
  • possible duplicate of [Proper way to #include when there is a circular dependency?](http://stackoverflow.com/questions/3901606/proper-way-to-include-when-there-is-a-circular-dependency) – Emile Cormier Mar 30 '12 at 17:55
  • No wait, it's not a duplicate, sorry. I wish I could unvote to close. – Emile Cormier Mar 30 '12 at 17:57
  • Not my actual code - that is way to long to include here. I very likely made a typo or two in typing up this quick example – Noah Mar 30 '12 at 18:19
  • **Here is actual code - shouldn't have any typos this way** – Noah Mar 30 '12 at 18:38

3 Answers3

1

Your compiler's right: classA isn't a type. C++ is case-sensitive; ClassA is the type you're looking for (check the first line of ClassB's constructor).

Hope that helps!

Xavier Holt
  • 14,471
  • 4
  • 43
  • 56
0

Answer After question modification

ClassFunc classFunc(classData); is not valid syntax in the definition of a class. You will need to have this classFunc variable by either a set function or through the constructor.

However, writing just a setter will be difficult because your ClassFunc requires a ClassData. In order to work around this, you may need to modify your ClassFunc also.

Also, there is another error. ClassData and Work are missing a ; at the end of its definition.

Original Answer before question modification

There are several errors in this code. Such as

float ClassA::funcA{

Should be

float ClassA::funcA(){

As other wise, it looks like a function definition.

Second, there is

ClassB(Class A& in_classA): classA(in_classA){

As it should be

ClassB(ClassA& in_classA): classA(in_classA){

As Class A is not a type.

Also, you are missing several semicolons but those should be obvious to spot.

josephthomas
  • 3,256
  • 15
  • 20
0

The problem is that you cannot initialize member variables in the class declaration:

ClassFunc classFunc(classData);

Instead, initialize it in the initializer list of the constructor:

Work(ClassData& in_class) :  classData(in_class), classFunc(classData) {}
Lubo Antonov
  • 2,301
  • 14
  • 18
  • That fails with the error: error: invalid initialization of reference of type ‘ClassFunc&’ from expression of type ‘ClassData’ – Noah Mar 31 '12 at 04:01