在c++类中使用string会输出错误

Using string in a class c++ outputs error

本文关键字:string 输出 错误 c++      更新时间:2023-10-16

我想创建一个使用string库的c++类。这是我的源代码:

test.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);