如何在C++类中使用字符串 我在C++中使用字符串变量的问题。我尝试过使用include,但似乎没有任何明显的效果。 以下是声明类的头文件的内容: #include <string.h> #ifndef STUDENT_H #define STUDENT_H class Student { private: int iStudentAge; int iStudentBirthDay; int iStudentBirthMonth; int iStudentBirthYear; string sStudentName; string sStudentCourse; public: Student(); Student(int iValue1, int iValue2, int iValue3, int iValue4, string sValue5, string sValue6); void setAge(int iNewValue); int getAge(); void setBirthDay(int iNewValue); int getBirthDay(); void setBirthMonth(int iNewValue); int getBirthMonth(); void setBirthYear(int iNewValue); int getBirthYear(); void setName(string iNewValue); string getName(); void setCourse(string iNewValue); string getCourse(); }; #endif // !STUDENT_H 我所拥有的特定错误都是包含字符串的行,除了包含行。

如何在C++类中使用字符串 我在C++中使用字符串变量的问题。我尝试过使用include,但似乎没有任何明显的效果。 以下是声明类的头文件的内容: #include <string.h> #ifndef STUDENT_H #define STUDENT_H class Student { private: int iStudentAge; int iStudentBirthDay; int iStudentBirthMonth; int iStudentBirthYear; string sStudentName; string sStudentCourse; public: Student(); Student(int iValue1, int iValue2, int iValue3, int iValue4, string sValue5, string sValue6); void setAge(int iNewValue); int getAge(); void setBirthDay(int iNewValue); int getBirthDay(); void setBirthMonth(int iNewValue); int getBirthMonth(); void setBirthYear(int iNewValue); int getBirthYear(); void setName(string iNewValue); string getName(); void setCourse(string iNewValue); string getCourse(); }; #endif // !STUDENT_H 我所拥有的特定错误都是包含字符串的行,除了包含行。,c++,string,C++,String,对于C++,你应该使用相应的库,包括不包括女巫是C库。< /P> 对于变量声明,请使用作用域 std::字符串sStudentName; 或 将字符串识别为类型的类声明之前 建议使用第一个选项,以了解为什么see是用于处理以null结尾的字节字符串的C头文件。std::string在头文件中定义 因此,首先将include更改为正确的文件,然后需要对所有字符串使用std::scope前缀。作为在文件中到处乱扔std::和使用命名空间std之间的折衷,您可以编写: using std::strin

对于C++,你应该使用相应的库,包括不包括女巫是C库。< /P> 对于变量声明,请使用作用域

std::字符串sStudentName; 或

将字符串识别为类型的类声明之前

建议使用第一个选项,以了解为什么see

是用于处理以null结尾的字节字符串的C头文件。std::string在头文件中定义


因此,首先将include更改为正确的文件,然后需要对所有字符串使用std::scope前缀。

作为在文件中到处乱扔std::和使用命名空间std之间的折衷,您可以编写:

using std::string;

字符串在std名称空间中,所以除了在include中之外,在任何地方都将其作为std::string编写。也有使用名称空间std的情况,但这被认为是错误的做法:请在问题中包含错误消息。这是否回答了您的问题?请不要建议使用名称空间std;在一个头文件中:@mch,我确实推荐了第一个选项,如果它存在并且可以使用,最好谈谈它,并说明为什么它不应该存在,我在我的答案中添加了链接。
using std::string;