【发布时间】:2013-07-29 08:42:19
【问题描述】:
设置
#include<utility> // I actually use precompiled headers
#include<map>
...SOME CODE...
namespace{
... SOME CODE...
/*Line 278*/ std::map<std::pair<int,int>,SmartPointer<A>> myMap;
/*Line 279*/ myMap.at(std::make_pair(1,1));
}
SmartPointer 就是所说的 - 一个使用智能指针包装其他类的类,用于自动堆内存管理。
发生的情况是,当我尝试编译它时,我得到了一大堆错误:
cpp(279): error C2143: syntax error : missing ';' before '.'
cpp(279): error C4430: missing type specifier - int assumed. Note: C++ does not
support default-int
cpp(279): error C2371: '`anonymous-namespace'::map' :
redefinition; different basic types
cpp(278) : see declaration of '`anonymous-namespace'::map'
第 278 和 279 行是上面的代码行。
map 位于匿名命名空间中,可以看出。我怀疑这是因为内部映射未配置为接受非标准类型作为值。
这一切都发生在 VS 2010 + 我也在使用 C++11。
问题
为什么会出现这些编译错误以及如何修复它们?
进展
>> 不是问题 - 当我注释掉第二行时,文件编译时不会抱怨(>> 和 > >)。
我将代码简化为这个 - 看看错误可能来自哪里 - 我得到以下一组编译错误:
代码:
std::map < int, int > myMap;
myMap[3] = 4;
错误:
cpp(279): error C4430: missing type specifier - int assumed. Note: C++ does not
support default-int
cpp(279): error C2373: 'myMap' : redefinition; different type modifiers
cpp(278) : see declaration of 'myMap'
cpp(279): error C2440: 'initializing' : cannot convert from 'int' to 'int [3]'
There are no conversions to array types, although there are conversions
to references or pointers to arrays
回答
@凯西
根据 Casey 的建议,我不能将 myMap.at(..) 放在命名空间范围内 - 我将它放在函数范围内,它得到了修复。
【问题讨论】:
-
那是因为您在
>和>之间缺少一个空格:std::map<std::pair<int,int>,SmartPointer<A>/*HERE*/ > map; -
编译器可能将>>解析为操作符。
-
不是!它与
>>编译得很好,但我还是改变了它并且错误仍然存在。 -
您不能只将
map.at(...)放在命名空间范围内,它需要在函数内部。 -
它刚刚编译 - 点了
标签: c++ visual-studio-2010 visual-c++ c++11 map