typedef映射作为返回类型到函数会引发编译器错误
Typedef map as return type to a function throws a compiler error
typedef a map
并使用 typedef
名称作为函数的返回参数失败。
以下是一个伪代码,解释了场景:
/**
* a.h
*/
class A
{
public:
typedef std::map<string,int> Int_map;
A();
~A();
const Int_map& getMap();
private:
Int_map my_map;
}
/**
* a.cpp
*/
A::A() {}
A::~A() {}
const Int_map& A::getMap() // gives a compiler error : Int_map does not name a type
{
return my_map;
}
但是,如果我在" A.CPP"中使用以下声明,则没有编译器错误。(注意:A.H文件仍然包含声明为const Int_map& A::getMap()
)
const std::map<string,int>& A::getMap()
是什么原因导致这种行为?
类似于相同行为,我还有另一个与std::string
有关的问题:
我知道字符串也是C 中的Typedef,并且使用了模板。返回string
的函数如何在C 中起作用,typedef map
会引发错误?
范围。定义功能时,您不在类A
的范围,而是在全局范围中。您需要使用A::Int_map
。
这是您需要在A::getMap
中使用范围操作员的原因。
相关文章:
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 重载方法的方式会在使用临时调用时生成编译器错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 编译器错误:destuctor 的更宽松的抛出说明符
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- C++ G++ 编译器 - 错误:隐式声明的定义
- 这个失败的测试是将零添加到空指针未定义的行为、编译器错误还是其他什么?
- 模板模板参数和模板别名:编译器错误?
- C++ 编译器错误:P1LinkedList.cpp:145:错误:重载的"to_string(int&)"调用不明确
- 在gcc中意外调用了Const重载.编译器错误或兼容性修复程序
- C1001内部编译器错误是由于矢量初始化(如数组)引起的
- 编译器错误(英特尔并行工作室 2019 与 Visual Studio 社区 2019)
- 如何在C++中克服此 C4430 编译器错误?
- 尝试使用 WinBGI 显示文本时出现编译器错误
- 使用typedef'ed返回类型声明友元函数时出现编译器错误
- 如何在类模板的成员函数中正确调用函数对象?正在生成 Visual Studio 编译器错误 C2440
- C++自定义分配器大小参数作为模板参数会引发编译器错误