gpt4 book ai didi

c++ - 在 C++ 类中使用字符串输出错误

转载 作者:行者123 更新时间:2023-11-28 00:46:20 25 4
gpt4 key购买 nike

我想创建一个使用字符串库的 C++ 类。这是我的源代码:

测试.h:

#include <string>


class info {
public:
// constructor
info (string first_name, string last_name, int age);
// deconstructor
~info ();



private:
string first_name;
string last_name;
int age;

};

这是我的头辅助文件:test.cpp

#include <string>
#include "test.h"


info::info (string first_name, string last_name, int age) {
this->first_name = first_name;
this->last_name = last_name;
this->age = age;

}
info::~info () {
}

但是它给了我语法错误:标识符“string”未定义

这是为什么呢?我对 C++ 数据结构有点陌生

此外,我正在编译这是 Visual Studio 2012

最佳答案

您需要在 string 之前添加 std::,因为您使用的是限定名称(命名空间 std 中的标识符)。

例如,您的构造函数应该如下所示:

info (std::string const& first_name, std::string const& last_name, int age);

关于c++ - 在 C++ 类中使用字符串输出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16131154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com