【发布时间】: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