【问题标题】:not " using namespace std; " gives me an unexpected error (C++)not " using namespace std; " 给我一个意外错误 (C++)
【发布时间】: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


【解决方案1】:

你需要使用

std::to_string()

std:: 表示标准命名空间

【讨论】:

  • 哦!哈哈,这就解释了。谢谢!已经想了将近一个小时了,这么简单的错过就是问题所在。谢谢您的帮助! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 2011-09-22
  • 1970-01-01
  • 2018-10-04
  • 2016-02-27
  • 1970-01-01
  • 2017-04-20
相关资源
最近更新 更多