gpt4 book ai didi

c++ - 尝试验证日期时 undefined reference

转载 作者:行者123 更新时间:2023-11-28 06:02:25 24 4
gpt4 key购买 nike

这是我的错误

/tmp/cce7wiXI.o: 在函数 main' 中:
ChronoTest.cpp:(.text+0x652): undefined reference
Chrono::operator>>(std::istream&, Chrono::Date&)'collect2:错误:ld 返回 1 退出状态

这是我的头文件

#include <iostream>
using namespace std;
namespace Chrono{
enum class Month{

jan=1,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec
};
class Date{
public:

class Invalid{};

Date(int y, Month m, int d);
Date();

int day() const{return d;}
Month month()const{return m;}
int year()const{return y;}


void add_day(int n);
void add_month(int n);
void add_year(int n);

private:
int y;
Month m;
int d;
};
bool is_date(int y, Month m, int d);
bool leapyear(int y);

bool operator==(const Date& a, const Date& b);
bool operator!=(const Date& a, const Date& b);

ostream& operator<<(ostream& os, const Date& d);
istream& operator>>(istream& is, Date& dd);

Date& operator++(Date& d);
Date operator++(Date& dl, int);
}

这是我的主文件

#include "Chrono.h"


//------------------------------------------------------------------------------

namespace Chrono{

Date::Date(int yy, Month mm, int dd)
:y{yy},m{mm},d{dd}
{
if(!is_date(yy,mm,dd))throw Invalid();
}

const Date& default_date()
{
static Date dd{2001,Month::jan,1};
return dd;
}
Date::Date()
:y{default_date().year()},
m{default_date().month()},
d{default_date().day()}{}
void Date::add_day(int n){
for(int i = 0; i<n; ++i){
++d;
if(!is_date(y, m, d)){
d = 1;
if(m == Month::dec){
m = Month::jan;
++y;
}else
m = Month(static_cast<int>(m)+1);
}
}
}
void Date::add_month(int n){

}
void Date::add_year(int n){
if (m==Month::feb&& d==29&&!leapyear(y+n)){
m = Month::mar;
d=1;
}
y+=n;
}

bool is_date(int y, Month m, int d)
{
if(d<=0)return false;
if(m<Month::jan || Month::dec<m)return false;

int days_in_month = 31;

switch(m){
case Month::feb:
days_in_month = (leapyear(y))?29:28;
break;
case Month::apr:case Month::jun:case Month::sep:case Month::nov:
days_in_month = 30;
break;
}
if(days_in_month<d)return false;
return true;
}
bool leapyear(int y)
{
return false;
}
bool operator==(const Date& a, const Date& b)
{
return a.year()==b.year()
&&a.month()==b.month()
&&a.day()==b.day();
}
bool operator!=(const Date& a, const Date& b)
{
return !(a==b);
}
ostream& operator<<(ostream& os, const Date& d)
{
return os<<'('<<d.year()
<<','<<static_cast<int>(d.month())
<<','<<d.day()<<')';
}
istream& operator<<(istream& is, Date& dd)
{
int y, m, d;
char ch1, ch2, ch3, ch4;
is>>ch1>>y>>ch2>>m>>ch3>>d>>ch4;
if(!is)return is;
if(ch1!='('||ch2!=','||ch3!=','||ch4!=')'){
is.clear(ios_base::failbit);
return is;
}
dd = Date(y, Month(m), d);
return is;
}
Date operator++(Date& d1, int)
{
Date d2 = d1;
d1.add_day(1);
return d2;
}
enum class Day{
sunday,monday,tuesday,wednesday,thursday,friday,saturday
};

Day day_of_week(const Date& d)
{

}
Date next_Sunday(const Date& d)
{
return d;
}
Date next_weekday(const Date& d)
{
return d;
}
}
int main()
try
{

using namespace Chrono;
cout << "enter some dates: ";
Date d;
cin>> d;
cout<< d << "is a valid date!";

// For some Windows(tm) setups
}
catch (Chrono::Date::Invalid&) {
cerr << "error: Invalid date\n";
// For some Windows(tm) setup
return 1;
}
catch (runtime_error& e) { // this code is to produce error messages
cout << e.what() << '\n';
// For some Windows(tm) setups
}

我正在尝试输入一个日期来验证它是否是真实的一天。

最佳答案

header 中的函数签名是:

istream& operator>>(istream& is, Date& dd);

这在您的实现文件中是不正确的:

istream& operator<<(istream& is, Date& dd)
// ^^ Should be >>

关于c++ - 尝试验证日期时 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32983199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com