使用std:string name的类中的字符串

时间:2014-06-30 20:48:35

标签: c++ string header

我遇到一个错误,告诉我字符串不是一个类型。所以我查看了这里,发现了std :: string name;在标题中,但是当我编译程序时,它告诉我名称未在范围内声明。

这是我的标题代码:

#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

这是我的cpp文件代码

#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;
}

1 个答案:

答案 0 :(得分:2)

您忘记了Inmate方法的getName()前缀。

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

如果没有该前缀,则函数getName存在于全局范围中,name在全局范围内而不是类范围内解析。由于全局范围中没有name变量,编译器会报告错误。