我一直在使用Visual Studio Express 13中的程序收到编译器错误 我在代码中注释了编译器错误出现的两行
Date.cpp
#include "Date.h"
using namespace std;
Date::Date(int d, int m, int y) : day(d), month(m), year(y)
{}
Date::Date() : day(0), month(0), year(0)
{}
const int Date::getDay() { return day; }
const int Date::getMonth() { return month; }
const int Date::getYear(){ return year; }
bool Date::operator<(const Date dOther)
{
return (year < dOther.year) || (year == dOther.year && month < dOther.month)
|| (year == dOther.year && month == dOther.month && day < dOther.day);
}
string Date::toString() //Error: Declaration is incompatible with...declared at line 21...Date.h
{
string s = month + "-" + day; s+="-" + year;
return s;
}
ofstream& operator<<(ofstream& out, Date& d)
{
string s = d.toString();
out.write(s.c_str(), s.size());
return out;
}
void Date::operator=(string s) //no instance of overloaded function "Date::operator=" matches the specified type
{
stringstream stream;
stream << s;
stream >> month;
stream >> day;
stream >> year;
}
Date.h
#ifndef DATE_CLASS
#define DATE_CLASS
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
class Date
{
private:
int day, month, year;
public:
Date();
Date(int, int, int);
const int getDay();
const int getMonth();
const int getYear();
string toString();
bool operator<(const Date);
friend ofstream& operator<<(ofstream& out, Date&d);
void operator=(string);
};
#endif
我不知道为什么会出现这些错误。这是运算符重载的问题吗?或者使用visual studio的东西(出于某种原因,如果我在Date.cpp中删除了一些代码,编译器错误会消失)?
答案 0 :(得分:0)
您的命名空间不匹配。在标题中,您不在std
之前使用string
命名空间前缀,因此编译器无法找到您需要的类型string
。