C++ 错误 C2146:语法错误:缺少“;” 标识符前

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2251724/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:42:54  来源:igfitidea点击:

error C2146: syntax error : missing ';' before identifier

c++

提问by kaycee

i can't get rid of these errors... i have semicolons everywhere i checked... the code is simple: the error takes me to the definition "string name" in article.h...

我无法摆脱这些错误......我检查的每个地方都有分号......代码很简单:错误将我带到article.h中的定义“字符串名称”......

main.cpp

主程序

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

#include "article.h"

int main()
{
 string si;
 char article[128];
 vector<Article> articles;
 ifstream file;


 file.open("input.txt",ifstream::in);

 while(!file.eof())
 {
  file.getline(article,128);
  articles.push_back(Article(article));

 }

 file.close();

 while(1);
 return(1);
}

article.h:

文章.h:

#ifndef Article_H
#define Article_H

class Article
{
public:
 int year;
 string name;

 Article(char *i_name);
};

#endif

回答by Sean

You should add:

你应该添加:

#include <string>

to your "article.h" header file and declare namelike this:

到您的“article.h”头文件并像这样声明名称

std::string name;

回答by Patrice Bernassola

It seems the stringtype is not defined in the artivle.h file. Try to include iostreamand add using namespace std(or write std::stringinstead of using namespace)

似乎该string类型未在 artivle.h 文件中定义。尝试包含iostream和添加using namespace std(或写入std::string而不是使用命名空间)

回答by Péter T?r?k

You should use the std:: namespace prefix in the header, like

您应该在标头中使用 std:: 命名空间前缀,例如

std::string name;