3

I made a class. the header file is :

   #pragma once
#include <string>
class Player
{
public:
    Player();
private:
};

and the cpp file is :

#include "Player.h"
#include <iostream>
Player::Player()
{
}

When I define a string in the header file and add an argument to the Player function in the header file everything works fine

#pragma once
#include <string>
class Player
{
public:
    Player(string name);
private:
    string _name;
};

but when I add the same argument to the Player function in the cpp file

#include "Player.h"
#include <iostream>
Player::Player(string name)
{
}

I get an error: identifier "string" is undefined and I get the same error in the header file as well so it effects that too. I tried including string in the cpp file in hopes of solving the problem but it did not work. I'm really desperate for a solution, guys.

momomo
  • 319
  • 1
  • 5
  • 15

1 Answers1

9

All STL types, algorithms etc are declared inside std namespace.

To make your code compile, string type should also specify the namespace as:

Player(std::string name);  /* Most recommended */

or

using namespace std;
Player(string name);  /* Least recommended, as it will pollute the available symbols */

or

using std::string;
Player(string name);
Gyapti Jain
  • 4,056
  • 20
  • 40