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

标签 c++ namespaces

我一直收到这个错误,我不知道为什么。
对 `mtm::operator<<(std::ostream&, mtm::DateWrap const&)' 的 undefined reference
我必须使用所有这些命名空间,这是作业'
但我仍然不知道如何使用这些命名空间和它
真的会有帮助!
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();
}
主.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;
}

最佳答案

不难,应该是

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

关于c++ - 对 `mtm::operator<<(std::ostream&, mtm::DateWrap const&)' 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65614182/

相关文章:

c++ - std::vector 和 llvm::SmallVector 有什么区别?什么时候使用哪一个?

ruby-on-rails - rails : form_for namespaced resource

c# - 无法从 C# 应用程序调用简单的自定义 Dll 导入

c++ - 32 位 arm 处理器上的链接错误 : "Cannot find -ltinfo" on Ubuntu 12. 04

c++ - 展开恒定数量的论点

全局命名空间的 C++ 别名

C++ 重新定义命名空间的变量?

c++ - 命名空间的类析构函数

c# - 命名空间与嵌套类

c++ - 除我的应用程序外全部静音