tags:

views:

318

answers:

3

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
A: 

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)

Patrice Bernassola
Don't include `iostream` in article.h: include `string`, which is all that header needs.
Steve Jessop
This is good practice as each header should be compilable if included on its own. However in his case it does not matter as string is defined before article.h is included
Mark
+1  A: 

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

std::string name;
Péter Török
The putting `using namespace std` before the `#include` means this isn't needed and doesn't really explain the errors. That said, relying on someone to put a `using namespace std` before including a header is pretty bad practice
Glen
+2  A: 

You should add:

#include <string>

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

std::string name;
Sean
It's true, you should, but I don't see why it would make any difference to whether the code compiles in this case.
Steve Jessop