我有以下问题:
我有两个班级:
FooClass.h
#include <vector>
#include "BaseClass.h"
using namespace std;
class FooClass
{
public:
vector<BaseClass> vBaseClass;
void doStuff();
void addBaseClass(BaseClass &baseClass);
};
FooClass.cpp
#include <iostream>
#include "FooClass.h"
void FooClass::doStuff()
{
cout << "Well nice done" << endl;
}
void FooClass::addBaseClass(BaseClass &baseClass)
{
baseClass.updateData(this);
vBaseClass.push_back(baseClass);
}
和
BaseClass.h
#include "FooClass.h"
class BaseClass {
public:
void updateData(FooClass *pFooClass);
};
BaseClass.cpp
#include "BaseClass.h"
void BaseClass::updateData(FooClass *pFooClass)
{
//We try to get some data, and if we get the data we call pFooClass->doStuff
pFooClass->doStuff();
}
所以,基本上该函数应该是我创建一个FooClass实例。 然后我创建了多个BaseClass实例,我想将它们添加到FooClass中的vBaseClass向量中。 如果needet我想用vBaseClass [key]访问BaseClass实例并在FooClass中调用doStuff()函数。我将FooClass作为指针参数给出,因为我希望仍然可以从doStuff()函数访问vBaseClass向量,然后由BaseClass实例调用。
一切正常,但是当我添加矢量时,我会收到以下错误:
error: ‘BaseClass’ was not declared in this scope
vector<BaseClass> vBaseClass;
error: template argument 1 is invalid
vector<BaseClass> vBaseClass;
error: template argument 2 is invalid
error: ‘BaseClass’ has not been declared
void addBaseClass(BaseClass &baseClass);
error: ‘FooClass’ has not been declared
void updateData(FooClass *pFooClass);
error: no matching function for call to ‘BaseClass::updateData(FooClass*)’
baseClass.updateData(this);
如果有人知道解决方案,非常感谢!
答案 0 :(得分:1)
感谢所有人,我的问题已经解决,这是工作代码:
FooClass.h
#ifndef TESTPROJECT_FOOCLASS_H
#define TESTPROJECT_FOOCLASS_H
#include <vector>
#include "BaseClass.h"
using namespace std;
class BaseClass;
class FooClass
{
public:
vector<BaseClass> vBaseClass;
void doStuff();
void addBaseClass(BaseClass &baseClass);
};
#endif //TESTPROJECT_FOOCLASS_H
FooClass.cpp
#include <iostream>
#include "FooClass.h"
void FooClass::doStuff()
{
cout << "Well nice done" << endl;
}
void FooClass::addBaseClass(BaseClass &baseClass)
{
baseClass.updateData(this);
vBaseClass.push_back(baseClass);
}
BaseClass.h
#ifndef TESTPROJECT_BASECLASS_H
#define TESTPROJECT_BASECLASS_H
#include "FooClass.h"
class FooClass;
class BaseClass {
public:
void updateData(FooClass *pFooClass);
};
#endif //TESTPROJECT_BASECLASS_H
BaseClass.cpp
#include "BaseClass.h"
void BaseClass::updateData(FooClass *pFooClass)
{
//We try to get some data, and if we get the data we call pFooClass->doStuff
pFooClass->doStuff();
}
main.cpp //控制台输出&#34;做得好&#34;
#include "lib/FooClass.h"
int main()
{
FooClass fooclass;
BaseClass baseclass;
fooclass.addBaseClass(baseclass);
}
感谢大家,这是我在这里的第一个活动和问题,我真的很高兴得到答案的速度,非常感谢!
#因为我被要求说明问题是什么以及我是如何解决的,所以我加入了
到FooClass.h
#ifndef TESTPROJECT_FOOCLASS_H
#define TESTPROJECT_FOOCLASS_H
class BaseClass;
和BaseClass.h
#ifndef TESTPROJECT_BASECLASS_H
#define TESTPROJECT_BASECLASS_H
class FooClass;
所以主要的问题是我必须在每个其他类的标题中声明BaseClass和FooClass。