错误 C2061:语法错误:标识符'string'

error C2061: syntax error : identifier 'string'

本文关键字:错误 string 标识符 C2061 语法      更新时间:2023-10-16
这可能是

一个包含问题,我在整个代码中都收到这些错误,而不仅仅是字符串标识符,例如error C2146: syntax error : missing ';' before identifier 'getName'error C2146: syntax error : missing ';' before identifier 'name'

下面是一个示例类:

#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;

所以我在没有自定义头文件的情况下编译了您的代码,它工作得很好。 基于此,我敢打赌,您在以下头文件之一中存在问题:

#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以分号结尾? 没这个必要。

相关文章: