不是“using namespace std;”给我一个意想不到的错误(C++)
not " using namespace std; " gives me an unexpected error (C++)
这是我第一次 post 来这里。我有一个关于“使用命名空间标准;”的问题。我希望我 post 做对了,但如果我做错了什么,请告诉我!
**问题:** 所以出现的问题是,当我删除 'using namespace std;' 时,出现意外错误,出现以下错误:"identifier 'to_string'未定义”。 所以我的问题是,为什么我会收到这个意外错误。 (我在下面用“<-- 这部分在这里”标记)一旦我删除 'using namespace std;' 就会发生错误。对我来说似乎有点奇怪,我必须事先声明“to_strong” ,但是当使用 namespace std; 它以某种方式为我声明它?
#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
}
};
你需要使用
std::to_string()
std:: 表示标准命名空间
这是我第一次 post 来这里。我有一个关于“使用命名空间标准;”的问题。我希望我 post 做对了,但如果我做错了什么,请告诉我!
**问题:** 所以出现的问题是,当我删除 'using namespace std;' 时,出现意外错误,出现以下错误:"identifier 'to_string'未定义”。 所以我的问题是,为什么我会收到这个意外错误。 (我在下面用“<-- 这部分在这里”标记)一旦我删除 'using namespace std;' 就会发生错误。对我来说似乎有点奇怪,我必须事先声明“to_strong” ,但是当使用 namespace std; 它以某种方式为我声明它?
#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
}
};
你需要使用
std::to_string()
std:: 表示标准命名空间