【问题标题】:undefined reference to `mtm::operator<<(std::ostream&, mtm::DateWrap const&)'未定义对 `mtm::operator<<(std::ostream&, mtm::DateWrap const&)' 的引用
【发布时间】:2021-04-13 06:52:16
【问题描述】:

我不断收到此错误,我不知道为什么。

对`mtm::operator的未定义引用

我必须使用所有这些命名空间,这是功课' 但我仍然不知道如何使用这些命名空间和它 真的很有帮助!

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;
}

【问题讨论】:

    标签: c++ namespaces


    【解决方案1】:

    不难,应该是的

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

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

    【讨论】:

      猜你喜欢
      • 2021-04-20
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多