Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

c++ - undefined reference to `mtm::operator<<(std::ostream&, mtm::DateWrap const&)'

I keep getting this error and I don't know why.

undefined reference to `mtm::operator<<(std::ostream&, mtm::DateWrap const&)'

I have to use all those namespaces, it is homework' but I still don't really get how to use those namespaces and it would really help!

date_wrap.h:

#ifndef DATE_WRAP_H
#define DATE_WRAP_H

#include <iostream>

extern "C" {   

#include "date/date.h"    }  
namespace mtm {


class DateWrap{
    Date date;

    public:
    DateWrap(int day, int month, int year);
    DateWrap(const DateWrap& date2);
    ~DateWrap();
    int day() const;
    int month() const;
    int year() const;
    DateWrap operator++(int);
    DateWrap& operator+=(int num);
    friend std::ostream& operator<<(std::ostream& os, const DateWrap&);
    bool operator==(const DateWrap& date1) const;
    bool operator>(const DateWrap& date1) const; };

std::ostream& operator<<(std::ostream& os, const DateWrap&);
 bool operator!=(const DateWrap& date1, const DateWrap& date2);
 bool operator<=(const DateWrap& date1, const DateWrap& date2);
 bool operator>=(const DateWrap& date1, const DateWrap& date2);
 DateWrap operator+(DateWrap& date ,int num);
 DateWrap operator+(int num, DateWrap& date);
    

#endif //DATE_WRAP_H }

date_wrap.cpp:

ostream& operator<<(ostream& os, const DateWrap& date){
    return os << date.day() << "/" << date.month() << "/" << date.year();
}

main.cpp:

#include <iostream>
#include "date_wrap.h"
using std::cout;
using std::endl;
using mtm::DateWrap;

int main(){
    DateWrap date(30, 11, 2020);
    cout << date << endl; // output: "30/11/2020"
    cout << date + 4 << endl; // output: "4/12/2020"
    cout << 3 + date << endl; // output: "3/12/2020"
    date++;
    cout << date << endl; // output: "1/12/2020"
    date += 7;
    cout << date << endl; // output: "8/12/2020"
    cout << (date > DateWrap(29, 11, 2020)) << endl; // output: "1"
    cout << (date <= DateWrap(29, 11, 2020)) << endl; // output: "0"
    cout << (date == DateWrap(30, 11, 2020)) << endl; // output: "0"
    date += (-3); // throw exception NegativeDays
    date = date + (-3); // throw exception NegativeDays
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's not hard, it should be

namespace ntm {

    ostream& operator<<(ostream& os, const DateWrap& date){
        return os << date.day() << "/" << date.month() << "/" << date.year();
    }

}

or

ostream& ntm::operator<<(ostream& os, const DateWrap& date){
    return os << date.day() << "/" << date.month() << "/" << date.year();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...