【问题标题】:Error: prototype for ‘XXX’ does not match any in class ‘YYY’错误:“XXX”的原型与“YYY”类中的任何原型都不匹配
【发布时间】:2016-01-19 16:20:42
【问题描述】:

向我的项目添加命名空间时出现以下错误:

CPoolElement.h:

#ifndef CPOOLELEMENT_H_
#define CPOOLELEMENT_H_

namespace TestName {

class CPoolElement {
public:
    CPoolElement();
    virtual ~CPoolElement();
};

}
#endif /* CPOOLELEMENT_H_ */

CPoolElement.cpp:

#include "CPoolElement.h"

namespace TestName {

CPoolElement::CPoolElement() {
    // TODO Auto-generated constructor stub

}

CPoolElement::~CPoolElement() {
    // TODO Auto-generated destructor stub
}

}

CRecordingPoolElement.cpp:

#include "CRecordingPoolElement.h"

namespace TestName {

CRecordingPoolElement::CRecordingPoolElement() {
    // TODO Auto-generated constructor stub

}

CRecordingPoolElement::~CRecordingPoolElement() {
    // TODO Auto-generated destructor stub
}

}

CRecordingPoolElement.h:

#ifndef CRECORDINGPOOLELEMENT_H_
#define CRECORDINGPOOLELEMENT_H_

#include "CPoolElement.h"

namespace TestName {

class CRecordingPoolElement : public CPoolElement{
public:
    CRecordingPoolElement();
    virtual ~CRecordingPoolElement();
};

}
#endif /* CRECORDINGPOOLELEMENT_H_ */

CTwo.h:

#ifndef CTWO_H_
#define CTWO_H_

class CPoolElement;

namespace TestName {


class CTwo {
public:
    CTwo();
    virtual ~CTwo();
    CPoolElement* GetElm();
};

}
#endif /* CTWO_H_ */

CTwo.cpp:

#include "CTwo.h"
#include "CRecordingPoolElement.h"

namespace TestName {

CTwo::CTwo() {
    // TODO Auto-generated constructor stub
}

CTwo::~CTwo() {
    // TODO Auto-generated destructor stub
}

CPoolElement* CTwo::GetElm() {
    return new CRecordingPoolElement();
}

}

错误: “错误:TestName::CPoolElement* TestName::CTwo::GetElm() 的原型与 TestName::CTwo 类中的任何一个都不匹配”

  • 我无法理解是什么问题?原型相同,没有循环头。

  • 此外,如果我删除命名空间的声明,我不会收到错误。

    1. 那么如何在使用命名空间时修复此错误?
    2. 为什么使用命名空间会在我的代码中添加错误?

【问题讨论】:

  • 您是否尝试过在 CTwo.h 的命名空间内向前声明 class CPoolElement;

标签: c++ namespaces function-prototypes


【解决方案1】:

您在“TestName”命名空间之外转发声明的“CPoolElement 类”,但在您的 CTwo::GetElm 定义中找到的 CPoolElement 是在 TestName 命名空间中声明的类。

命名空间允许您将代码与其他类名分开,这些类名可能名称相似但在其他标头中声明,可能来自库或某些外部依赖项。这些是完全不同的类,可以做完全不同的事情。

当您在 CTwo.h 中转发声明的 CPoolElement 时,您指定了您想要的 CPoolElement 类应该存在(被声明)在“全局”命名空间中。但是,当编译器遇到您的 GetElm 声明时,它发现了一个不同的类“TestName::CTwo”。

将您的前向声明移至 TestName 命名空间,我认为您将解决您的错误。

CTwo.h:

#ifndef CTWO_H_
#define CTWO_H_

//class CPoolElement; <--- NOT HERE

namespace TestName {

class CPoolElement; // <--- HERE

class CTwo {
public:
    CTwo();
    virtual ~CTwo();
    CPoolElement* GetElm();


};

}
#endif /* CTWO_H_ */

【讨论】:

    【解决方案2】:

    这确实有点令人费解。但我认为您可以通过将“class CPoolElement”移动到“CTwo.h”中的命名空间 TestName 来修复它。

    这是因为您在 CPoolElement.h 中定义了 TestName::CPoolElement,但在 CTwo.h 中,您引用的是 ::CPoolElement。确实不匹配。

    【讨论】:

      【解决方案3】:

      class CPoolElement 必须在命名空间内移动:

      CTwo.h:

      namespace TestName {
      
      class CPoolElement;
      
      // ...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-22
        • 1970-01-01
        • 1970-01-01
        • 2014-05-25
        • 2023-03-12
        相关资源
        最近更新 更多