c++ - 通过引用传递 c++ 与命名空间中的类

标签 c++ reference namespaces args

我正在使用 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/

相关文章:

c++ - 无法编译,错误 : cryptlib. h:没有那个文件或目录

c++ - 有哪些有用的技巧可以追踪 C++ 中随机发生的 C0000005 访问冲突?

sql - 无法从命名空间读取 xml 节点

xml - JIBX:如何使用多个目标命名空间?

asp.net - 如何在解决方案中从我的类库访问我的默认 aspx 页面上的控件

c++ - 如何将另一个类的函数作为参数传递?

c++ - 在 SYCL 中使用屏障

c# - 为什么 Visual Studio 2010 不断在我的类库中添加 "reference to self"?

c++ - 对指针的 const 引用可以改变对象

c# - 现有 COM 引用或添加新引用时出错