我有以下问题:

我有两节课:

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 vector 中。
如果需要,我想使用vBaseClass [key]访问BaseClass实例并在FooClass中调用doStuff()函数。我给FooClass作为指针参数,是因为我仍然想从doStuff()函数访问vBaseClass vector ,然后由BaseClass实例调用该函数。

一切正常,但是当我添加 vector 时,出现以下错误:
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);

如果有人知道解决方案,非常感谢!

最佳答案

多亏了所有,我的问题解决了,这是工作代码:

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 //控制台输出“做得好”
#include "lib/FooClass.h"

int main()
{
    FooClass fooclass;
    BaseClass baseclass;
    fooclass.addBaseClass(baseclass);

}

感谢大家,这是我的第一个 Activity 和在这里提出的问题,让我印象深刻的是我得到答案的速度如何,非常感谢!



因为我被要求说出问题是什么以及如何解决,所以我加了

到FooClass.h
#ifndef TESTPROJECT_FOOCLASS_H
#define TESTPROJECT_FOOCLASS_H

class BaseClass;

并转到BaseClass.h
#ifndef TESTPROJECT_BASECLASS_H
#define TESTPROJECT_BASECLASS_H

class FooClass;

因此,主要的问题是我必须如上所述在每个其他类的 header 中声明BaseClass和FooClass。

09-07 04:35