Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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...

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:

#ifndef Article_H
#define Article_H

class Article
{
public:
 int year;
 string name;

 Article(char *i_name);
};

#endif
like image 247
kaycee Avatar asked Feb 12 '10 12:02

kaycee


3 Answers

You should add:

#include <string>

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

std::string name;
like image 148
Sean Avatar answered Nov 18 '22 06:11

Sean


It seems the string type is not defined in the artivle.h file. Try to include iostream and add using namespace std (or write std::string instead of using namespace)

like image 24
Patrice Bernassola Avatar answered Nov 18 '22 07:11

Patrice Bernassola


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

std::string name;
like image 3
Péter Török Avatar answered Nov 18 '22 06:11

Péter Török