我正在使用 g++ 4.4.1 并且在我的类中的命名空间中通过引用传递时遇到问题。我在下面创建了能够证明我的问题的测试程序,如果有人知道为什么它不会编译
#include <stdio.h>
namespace clshw
{
class hwmgr
{
private:
hwmgr() {};
public:
~hwmgr() {};
static hwmgr* Instance();
int Read(int crumb, int& state);
private:
static hwmgr* instance;
};
}
namespace clshw
{
hwmgr* hwmgr::instance = NULL;
hwmgr* hwmgr::Instance()
{
instance = new hwmgr;
return instance;
}
int hwmgr::Read(int crumb, int state)
{
state = true;
return 1;
}
}
using namespace clshw;
hwmgr *hw = hwmgr::Instance();
int main()
{
int state;
int crumb;
int ret = hw->Read(crumb, state);
}
编译错误是:
test7.cpp:30: 错误:“int clshw::hwmgr::Read(int, int)”的原型(prototype)与类“clshw::hwmgr”中的任何内容都不匹配 test7.cpp:13: 错误:候选人是:int clshw::hwmgr::Read(int, int&)
TIA, 基思
最佳答案
问题出在第 13 行。
int Read(int crumb, int& state);
您提供了一个函数 Read,它接受一个 int 和一个 int 地址。
在第 30 行,int hwmgr::Read(int crumb, int state)
您正在定义一个函数 Read,它接受两个 int。
由于您提供了不同类型的参数,因此两种方法都不同。所以编译器会给你错误:“int clshw::hwmgr::Read(int, int)”的原型(prototype)与“clshw::hwmgr”类中的任何内容都不匹配
请修改如下定义:
在第 30 行,这样写:
int hwmgr::Read(int crumb, int &state)
还有 tadaaaaa!我们已经成功了。
关于c++ - 通过引用传递 c++ 与命名空间中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17843345/