0

I'm having a problem with my header file. For some reason it gives me errors about the strings. In Header.h I have

#include <string>
#ifndef HEADER_H
#define HEADER_H
class Player
{
    public:
    string firstName;
    string lastName;
    int birthYear;
    string *matchDates;

    string toString();
    void addMatchDate(string date);
    Player();
    Player(string firstName, string lastName, int birthYear);
    ~Player();
};
#endif

But for God knows what reasons, it gives me a bunch of errors. These are some of the errors:

Error C2146: syntax error : missing ';' before identifier 'firstName'   
Error C2146: syntax error : missing ';' before identifier 'lastName'    
Error C2061: syntax error : identifier 'string'

Any ideas how to solve this?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Krippkrupp
  • 184
  • 1
  • 2
  • 12
  • 1
    Use qualified name (`string` is defined in namespace `std`): `std::string`. – hmjd Feb 12 '14 at 16:47
  • Oh, so I could just add using namespace std? – Krippkrupp Feb 12 '14 at 16:48
  • 2
    See http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – hmjd Feb 12 '14 at 16:49
  • @user3194111 Prefer to qualify the name: `std::string`, for the reasons in hmjd's link. Also, put the `include` inside the `#ifndef` guard (not that it will make a significant difference, but the point of the guard is to hide *everything* inside the file from being seen twice). – BoBTFish Feb 12 '14 at 16:51
  • Thank you everyone for the help! I've also found a problem with creating the function toString in Test.cpp I've included "Header.h", string, iostream. Namespace std has been declared. But when trying to create the function, Player::toString() {} I get errors saying Error C2371: 'Player::toString' : redefinition; different basic types Error C2556: 'int Player::toString(void)' : overloaded function differs only by return type from 'std::string Player::toString(void)' Error C4430: missing type specifier - int assumed. Note: C++ does not support default-int – Krippkrupp Feb 12 '14 at 17:00

1 Answers1

2

Name string is defined in the standard name space std.

So either before the class definition use directive

using std::string;

or specify qualified name

std::string

whenever you use this type.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335