error: use of undeclared identifier 'std' c++

SahithiumZ picture SahithiumZ · Jan 23, 2020 · Viewed 7.3k times · Source

I am new to coding in C++ and I want to make a simple Pokemon game.

I created a class in a header file and I am defining the functions in a separate .cpp file.

I also have a main file where I will run my actual code.

So I am defining a std::string function in my functions file, and it says std is an undeclared identifier.

Here are each of my files:

Function Definition:

#include "fns.hpp"
int Pokemon::getHP() {
  return hp;
}
int Pokemon::getAttack() {
  return attack;
}
int Pokemon::getDefense() {
  return defense;
}
int Pokemon::getSpecialAttack() {
  return specialAttack;
}
int Pokemon::getSpecialDefense() {
  return specialDefense;
}
int Pokemon::getSpeed() {
  return speed;
}
std::string Pokemon::getAttack1() {
  return attack1;
}
std::string Pokemon::getAttack2() {
  return attack2;
}
std::string Pokemon::getAttack3() {
  return attack3;
}
std::string Pokemon::getAttack4() {
  return attack4;
}
Pokemon::Pokemon(int qhp,int qdefense,int qattack,int qspecialAttack,int qspecialDefense,int qspeed,std::string qattack1,std::string qattack2,std::string qattack3,std::string qattack4)
: hp(qhp),attack(qattack),defense(qdefense),specialAttack(qspecialAttack),specialDefense(qspecialDefense),speed(qspeed),attack1(qattack),attack2(qattack2),attack3(qattack3),attack4(qattack4) {}

Function Declaration:

class Pokemon {
  int hp,attack,defense,specialAttack,specialDefense,speed;
  std::string attack1,attack2,attack3,attack4;
  public:
  int getHP();
  int getAttack();
  int getDefense();
  int getSpecialAttack();
  int getSpecialDefense();
  int getSpeed();
  int getAttack1();
  int getAttack2();
  int getAttack3();
  int getAttack4();
  Pokemon(int qhp,int qdefense,int qattack,int qspecialAttack,int qspecialDefense,int qspeed,std::string qattack1,std::string qattack2,std::string qattack3,std::string qattack4);
};

Whenever I say std::string, it says it is an undeclared identifier.

Can someone please help me?

Answer

William Awad picture William Awad · Jan 23, 2020

It is because you have not used the library for it.

use the below at the top of your header file

#include<string>