错误:使用不完整类型struct Subject无效;错误:struct subject的前向声明

时间:2012-04-23 05:49:51

标签: c++ forward-declaration

我从模板类继承。当我在教师课堂上录入时,我想在主题课程中输入一个条目。反之亦然。 我收到错误无效使用不完整类型struct Subect;

void addSubject(Subject *s) {
            this->addReference(s);
            s->addReference(this); when I comment this line the it compiles without errors, but I cannot insert into Subject 

        }

我的整个代码在

之下
#include <iostream>
#include <stdlib.h>
#include <vector>

using namespace std;
class Subject;


template <class T>
class abstractReference {


    vector<T*> list;
    public:
    string code;
        void addReference(  T*);
        void delReference(  T*);
        bool hasReference(  T*);
        T * getReference(int );
};

template <class T>
void abstractReference<T>::addReference(T* ref) {
    list.push_back(ref);
    //ref->addReference(this);
}

template <class T>
void abstractReference<T>::delReference(T* ref) {
    int i;
    for (i=0; i<list.size(); i++) {
        if (list[i]== ref) list.erase(i);
    }

}

template <class T>
bool abstractReference<T>::hasReference( T* ref) {
  if (list.size() == 0) return false;
  int i;
  for (i=0; i<list.size(); i++) {
      if (list[i] == ref) return true;
  }
  return false;
}

template <class T>
T* abstractReference<T>::getReference(int i) {
    if (i < 0 || i > list.size()) return NULL;
    return list[i];
}

class Teacher : public abstractReference<Subject> {

    string fname;
    string lname;
    float  sal;
public:
    Teacher(string c, string fn, string ln, float s): fname(fn), lname(ln), sal(s) {
    }
    Teacher() {sal =0; fname=""; lname="";}
    void setInfo(string c, string fn, string ln, float s=0.00) {
        code=c;
        fname = fn;
        lname = ln;
        sal = s;
    }

    string getName() {
        return fname + " " + lname;
    }

    float getSal() {
        return sal;
    }

    void addSubject(Subject *s) {
        this->addReference(s);
        s->addReference(this);

    }

    void delSubject(Subject *s) {

    }

};

class Subject : public abstractReference<Teacher> {
  string title;
public:
  Subject(string c=NULL, string t=NULL) {
      title = t;

  }
  string getTitle() {
      return title;
  }
};

main() {
    Teacher *t;
    t = new Teacher("J109", "Dexter", "McConnell",15000);
    Subject *s, *s1, *s2;
    s= new Subject("E001", "English");
    s1= new Subject("M001", "Maths");
    s2= new Subject("s001", "Science");

    t->addSubject(s);
    t->addSubject(s1);

    cout << "Has Reference " << t->hasReference(s2) << endl;
    cout << "Done..." << endl;
    cout << t->getReference(1)->getTitle() << endl;
}

2 个答案:

答案 0 :(得分:2)

问题是编译器不理解类型Subject,您可能转发声明它或忘记包含定义它的头文件。
结果是,编译器Subject不完整类型,并且它无法执行任何需要知道Subject布局的操作,调用其成员函数是一个这样的例子。

解决方案是:

您应该在源cpp文件中包含定义Subject的头文件,其中包含以下定义:

void addSubject(Subject *s)

如果您没有多个文件,请确保在之前定义了Subject您已定义函数addSubject()

答案 1 :(得分:1)

当编译器尝试编译类Teacher时,类Subject尚未定义,只有声明。因此编译器不知道Subject中存在哪些方法。

我的建议是你将函数的主体移动到下面 Subject的定义。

这样的事情:

class Teacher : public abstractReference<Subject> {
    // ...

    void addSubject(Subject *s);

    // ...
};

class Subject : public abstractReference<Teacher> {
    // ...
};

void Teacher::addSubject(Subject *s) {
    // ...
}