2012-05-14 58 views
2

這可能是一個包括問題,我得到這些錯誤遍佈代碼,而不是隻針對例如error C2146: syntax error : missing ';' before identifier 'getName'字符串標識符和error C2146: syntax error : missing ';' before identifier 'name' 錯誤C2061:語法錯誤:標識符「串」

這裏是一個例子類:

#include "stdafx.h" 

class participant 
{ 
public: 
participant(int id, string name); 
~participant(void); 

int getId(); 
string getName(); 

private: 
     int id; 
    string name; 
}; 

這裏是我的stdafx.h文件:

#pragma once 

#include "targetver.h" 

#include <stdio.h> 
#include <tchar.h> 
#include <iostream> 
#include <string> 
#include <sstream> 
#include <vector> 
#include <list> 
#include "day.h" 
#include "appointment.h" 
#include "extendedAppointment.h" 
#include "participant.h" 
#include "calendar.h" 
using namespace std; 

#define no_such_appointment_error 20; 
#define conflicting_appointments_error 21; 
#define noSuchDayError 22; 
#define incorrectAppointmentError 23; 
+6

'在頭文件中使用'命名空間是一個可怕的想法。任何包含頭文件的文件現在都有'using namespace std;'(這很糟糕)。 –

+0

謝謝,刪除它。仍然不是固定的 – Michael

+3

它不會直接顯示爲您發佈的代碼中的問題,因爲您發佈的內容並未顯示它們正在使用,但以分號結尾的宏幾乎總是會導致問題。 –

回答

7

所以我編譯你的代碼,而無需發佈您的自定義頭文件nd它工作得很好。在此基礎上,我會打賭你在這些頭文件中的一個有一個問題:

#include "day.h" 
#include "appointment.h" 
#include "extendedAppointment.h" 
#include "participant.h" 
#include "calendar.h" 

這可能是一個宏,一類/結構不能用分號結束,等檢查的出。

最後,少數的切線問題:

首先,using在頭文件命名空間是一種可怕的想法。任何包含頭文件的文件現在都有using namespace std;(這很糟糕)。您可能不希望在包含stdafx.h的每個文件中包含多個頭文件。

其次,一旦你刪除,然後string立即變得未定義(改爲使用std :: string)。

最後,爲什麼您的#define以分號結尾?沒有必要。

+0

'targetver.h'是由Visual Studio生成的,所以我們可以假定它不是罪魁禍首。 – AJG85

+0

我明白了。謝謝,我將開始消除所有可能性 – Michael

+0

@ AJG85:好吧,不知道。謝謝。 –

相關問題