C++ 错误 C2061:语法错误:标识符“字符串”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10589355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error C2061: syntax error : identifier 'string'
提问by Michael
this is probably an include problem, i get these errors all over the code, and not only for string identifier for example error C2146: syntax error : missing ';' before identifier 'getName'
and error C2146: syntax error : missing ';' before identifier 'name'
这可能是一个包含问题,我在整个代码中都遇到了这些错误,而不仅仅是例如字符串标识符error C2146: syntax error : missing ';' before identifier 'getName'
和error C2146: syntax error : missing ';' before identifier 'name'
here's an example class:
这是一个示例类:
#include "stdafx.h"
class participant
{
public:
participant(int id, string name);
~participant(void);
int getId();
string getName();
private:
int id;
string name;
};
here's my stdafx.h
file:
这是我的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;
回答by Ed S.
So I compiled your code as-posted without your custom header files and it worked just fine. Based on that, I am going to wager that you have a problem in one of these header files:
因此,我在没有自定义头文件的情况下按原样编译了您的代码,并且运行良好。基于此,我敢打赌您在以下头文件之一中遇到了问题:
#include "day.h"
#include "appointment.h"
#include "extendedAppointment.h"
#include "participant.h"
#include "calendar.h"
It could be a macro, a class/struct not terminated with a semi-colon, etc. Check those out.
它可能是一个宏、一个不以分号结尾的类/结构等。检查一下。
Lastly, a few of tangential issues:
最后,一些切中要害的问题:
First, using
a namespace in a header file is a terrible idea. Any file that includes your header now has using namespace std;
in it (this is bad). You probably don't want to include that many header files in every file that includes stdafx.h
.
首先,using
头文件中的命名空间是一个糟糕的主意。任何包含您的标题的文件现在都包含using namespace std;
在其中(这很糟糕)。您可能不想在每个包含stdafx.h
.
Secondly, once you remove that then string
immediately becomes undefined (use std::string instead).
其次,一旦你删除它,然后string
立即变得未定义(使用 std::string 代替)。
Last, why are your #define
s ended with semi-colons? No need for that.
最后,为什么你的#define
s 以分号结尾?不需要那个。