0

I was running into an error telling me string was not a type. So I looked up on here and found out that std::string name; to be in the header, but when I compile the program it tells me that name was not declared in the scope.

here is my code for the header:

#ifndef INMATE_H
#define INMATE_H
#include <iostream>
#include <string>

class Inmate
{
public:
    Inmate();
    int getID();
    std::string getName();
    //int getHeightFt();
    //int getHeightInch();
    void setID(int x);
    //void setName():
    //void setHeightFt();
    //void setHeightInch();

private:
    int idNumber;
    std::string name;
    //int heightFt;
    //int heightInch;

};

#endif // INMATE_H

and this is my cpp file code

#include "Inmate.h"
#include <iostream>
#include <string>

using namespace std;

Inmate::Inmate()
{
    cout << "What is the inmates name"<<endl;
    cin >> name;
}

void Inmate :: setID(int x){
    idNumber = x;
}

int Inmate :: getID(){
    return idNumber;
}
string getName(){
    return name;
}
  • [Why is “using namespace std;” considered bad practice?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Deduplicator Jun 30 '14 at 20:52
  • This problem has absolutely nothing to do with strings... – Lightness Races in Orbit Jun 30 '14 at 21:14
  • I haven't done any coding in a long while. It's been even longer since the last time I used this site. I have pretty much forgotten all code, and while I have recently been considering jumping back in, code is not the reason I came back. I logged in to check a post about possible worth in fixing or recycling old mini fridges, and I seen this very old post. It pretty great that stack keeps these. Now I have a decent repository, for lack of a better word, on my old code and old mistakes to relearn from. @Boundry – JayB901662 Apr 18 '17 at 11:16

1 Answers1

2

You forgot the Inmate prefix on your getName() method.

string Inmate::getName(){
    return name;
}

Without that prefix, the function getName exists in the global scope, and name is resolved in the global scope instead of the class scope. Since there is no name variable in the global scope, the compiler reports an error.

merlin2011
  • 71,677
  • 44
  • 195
  • 329