c++ - 不断收到 unordered_map 编译器错误

标签 c++ unordered-map

<分区>

标题:

#include <unordered_map>
#include "O.h"
#include "P.h"

using namespace std;

class O{
public:
    O();

    unordered_map<int,P>* X();
    unordered_map<int,P>* Y();

private:
    unordered_map<int,P>* b;
    unordered_map<int,P>* a;
};

来源:

#include "O.h"
#include "P.h"
#include <unordered_map>

using namespace std;

O::O(){
    a= new unordered_map<int,P>();
    b= new unordered_map<int,P>();
}

unordered_map<int,P>* O::X(){
        return b;
}

unordered_map<int,P>* O::Y(){
        return a;
}

错误是:

1>O.cpp(76): error : return value type does not match the function type 1> return b;

1>O.cpp(80): error : return value type does not match the function type 1> return a;

我要疯狂地尝试调试它....

编辑:英特尔编译器 v13

最佳答案

您发布的代码是有效的 C++,因此问题一定出在代码的其他地方。我会检查包含的标题。这是一个有效声明 P 的示例:

#include <unordered_map>
using namespace std;

class P{
public:
    int a = 3;  
};

class O{
public:
    O();

    unordered_map<int,P>* X();
    unordered_map<int,P>* Y();

private:
    unordered_map<int,P>* b;
    unordered_map<int,P>* a;
};

O::O(){
    a= new unordered_map<int,P>();
    b= new unordered_map<int,P>();
}

unordered_map<int,P>* O::X(){
    return b;
}

unordered_map<int,P>* O::Y(){
    return a;
}

int main(){
    O o;
    auto map = o.X();
    return 0;
}

ideone: http://ideone.com/Y4ydzj

关于c++ - 不断收到 unordered_map 编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18905203/

相关文章:

c++ - std::unordered_map::emplace 对象创建

php - 如何从 C++ 调用 php-cgi

c++ - 该片段在 Coliru 中编译时带有警告,但在 Ideone 中编译正常。哪一个是正确的?

c++ - 使用 Autoconf 处理已弃用的包含

c++ - unordered_map 无法检索在参数中指定为变量的键的值

c++ - 使用 unordered_map 方法在 C++ 中查找最(多个)常用词

c++ - unordered_map 未正确更新

c++ - 使用 json 库创建 json 字符串

c++ - 合并排序 - 段错误

c++ - 人机界面命令语法及解析