2010-11-14 80 views
0

當我嘗試在VS 2010中進行編譯時,出現以下錯誤。它也在抱怨字符串未定義,這沒有任何意義,因爲我明確包含它。我明白了什麼錯誤的意思,但他們似乎沒有任何意義:C2146缺失;在Visual Studio 2010中

1>c:\users\jon\documents\visual studio 2010\projects\project 2\project 2\userfactory.cpp(11): error C2146: syntax error : missing ';' before identifier 'profession' 1>c:\users\jon\documents\visual studio 2010\projects\project 2\project 2\userfactory.cpp(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\users\jon\documents\visual studio 2010\projects\project 2\project 2\userfactory.cpp(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\users\jon\documents\visual studio 2010\projects\project 2\project 2\userfactory.cpp(16): error C2143: syntax error : missing ';' before '<' 1>c:\users\jon\documents\visual studio 2010\projects\project 2\project 2\userfactory.cpp(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\users\jon\documents\visual studio 2010\projects\project 2\project 2\userfactory.cpp(16): error C2238: unexpected token(s) preceding ';' 1>c:\users\jon\documents\visual studio 2010\projects\project 2\project 2\userfactory.cpp(23): error C2061: syntax error : identifier 'string' 1>c:\users\jon\documents\visual studio 2010\projects\project 2\project 2\userfactory.cpp(19): error C2061: syntax error : identifier 'map'

這是我的代碼:

#include "objbase.h" //I found this recommendation while googling, but had the errors prior to adding this. 
#include <string> 
#include <map> 
#include <utility> 

class UserFactory { 
public: 
    class User { 
     char gender; 
     int id; 
    string profession;// line 11 
    int zip; 
    friend class UserFactory; 
}; 
private: 
map<int,User*>* map; 

public: 
UserFactory() : map(new map<int,User*>()) { } 

virtual ~UserFactory(void); 

void process(string s) { 
    //user id | age | gender | occupation | zip code 

} 
}; 

任何幫助,將不勝感激之前,我撕裂我的頭髮!

謝謝!

+0

應該不是std :: string職業? – Bart 2010-11-14 19:25:41

回答

5

字符串是std名稱空間的一部分,所以您必須將其稱爲std :: string。這同樣適用於地圖。

+0

或者你可以使用'using namespace std;'。 – 2010-11-14 19:27:24

+1

或'使用std :: string;'只將'string'導入默認名稱空間。 – ThiefMaster 2010-11-14 19:29:26

+8

@Fabian:這是[我建議反對](http://stackoverflow.com/questions/2879555/2880136#2880136)。 – sbi 2010-11-14 19:31:31