【问题标题】:'to_string' unknown override specifier'to_string' 未知覆盖说明符
【发布时间】:2015-07-29 03:51:23
【问题描述】:

对于一个作业,我应该在头文件'date.h' 中使用以下类但是当我尝试编译我的程序时,我收到一个错误说明:

'to_string':未知的覆盖说明符。

以下代码有什么问题?我似乎无法弄清楚。

#pragma once
#ifndef DATE_H
#define DATE_H

#include <string>
#include <sstream>

class Date
{
private:
    int m_day, m_month, m_year;
public:
    Date(int d, int m, int y)
    {    
        m_day = d; m_month = m; m_year = y;
    }

    int getDay() const { return m_day; }
    int getMonth() const { return m_month; }
    int getYear() const { return m_year; }
    string to_string() const
    {
        stringstream s;
        s << getMonth() << "/" << getDay() << "/" << getYear();
        return s.str();
    }
};
#endif

【问题讨论】:

  • 这段代码对我来说编译得很好(gcc 4.9)。也许它在您的 .cpp 文件中。 to_string 是一个 c++11 标准函数,我注意到您没有使用范围解析,所以那里可能存在编译器混淆。我会避免使用using namespace std,而是使用std::string 等。您使用的是什么编译器?我们能看到调用to_string的函数吗?

标签: c++ string class header overriding


【解决方案1】:

这可能是因为字符串是标准库的一部分。你应该这样做:

std::string to_string() const {

}

【讨论】:

  • 或者在文件顶部添加using namespace std;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 2021-08-01
  • 1970-01-01
相关资源
最近更新 更多