使用std:string名称的类中的字符串
Strings in classes using std:string name
我遇到了一个错误,告诉我字符串不是类型。我查了一下这里,发现std::string name;在头文件中,但是当我编译程序时,它告诉我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;
}
您在getName()
方法上忘记了Inmate
前缀。
string Inmate::getName(){
return name;
}
如果没有前缀,则函数getName
存在于全局作用域中,而name
在全局作用域中解析,而不是在类作用域中解析。由于全局作用域中没有name
变量,编译器会报告一个错误。
相关文章:
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 使用std::mt19937从字符串中返回一个随机单词
- 我可以重新分配/覆盖std::字符串吗
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 如何将这个std::字符串转换为std::基本字符串
- 字符串化递归的"std::vector<std::vector<...>>"而不使用部分模板函数专用化
- std::字符串与 PCWSTR 的对话
- 确切地说,如何解释 std::getline(stream, string) 函数在C++中填充的字符串
- 将C++ std::string 转换为 UTF-16-LE 编码的字符串
- 使用 std::string_view 的子字符串控制台输出
- std::regex:匹配由数字和空格组成的字符串,并提取数字.如何?
- 重载 std::字符串运算符+ 用于打印枚举名称
- 从std::字符串创建OssBitString
- C++ TCP 套接字 | 无法将"std::__cxx11::字符串"转换为"const
- C 中的特殊字符的子字符串(STD :: substr)
- 是否建议将字符串std::移动到将被覆盖的容器中
- 显式指定要写入的字符串 std::ostringstream
- 如何将uint16_t转换为宽字符串(std::wstring)
- 读取和Huffman压缩4字节二进制字符串STD C++Linux环境