簡體   English   中英

C ++從std :: pair映射到一些內部類

[英]C++ map from std::pair to some internal class

設定

#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(..)放在命名空間范圍內-我將其放在函數范圍內並且已修復。

只是不要將標准庫名稱(例如map用於變量名。 稱它為有意義的東西 ,它也將消除類型和變量之間的沖突。

另外,您忘記了#include <utility>來引入std::pair

您有名字沖突:

std::map<std::pair<int,int>,SmartPointer<A>> map;

名稱map就是類型。 您不能將類型名稱用作變量名稱。 就像是

std::map<std::pair<int,int>,SmartPointer<A>> myMapThatHasAUsefullName;

應該修復它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM