【发布时间】:2021-01-08 07:00:32
【问题描述】:
这是我在这里的第一篇文章。我有一个关于“使用命名空间 std;”的问题。我希望我发布的是正确的,但如果我做错了什么,请告诉我!
**问题:**所以出现的问题是当我删除'using namespace std;'时我收到一个意外错误,它给了我以下错误:“identifier 'to_string' is undefined”。 所以我的问题是,为什么我会收到这个意外错误。 (我在下面用“
#include <iostream>
#include <string>
using namespace std;
// class name {}
class Person
{
private:
std::string name;
int age;
public:
Person()
{
std::cout << "Constructor called!" << "\n";
// (this) signals we are trying to access the veriables of private
this->name = "N/A";
this->age = 0;
}
~Person() // Destructor is a member function which destructs or deletes an object.
{
std::cout << "Destrouctor called!" << "\n";
}
// Accessors (Getters)
const std::string& getName() const { return this->name; }
const int& getAge() const { return this->age; }
// Modifiers (Setters)
void setName(const std::string name) { this->name = name; }
void setAge(const int age) { this->age = age; }
// Functions
const std::string toString() const
{
return "Name: " + this->name + " Age: " + to_string(this->age); <-- This part here
}
};
【问题讨论】:
-
@Blastfurnace,哈哈,好吧!下次我会确保不签名,谢谢提示! ^^
标签: c++ compiler-errors namespaces std