C ++ Undefined对重载运算符的引用

时间:2016-09-10 11:26:44

标签: c++ overloading undefined-reference

我的重载<<运算符,cygwin返回2个错误,

    DeckOfCardsDemo.o:DeckOfCardsDemo.cpp:(.text+0xd3): undefined reference to `samuel::operator<<(std::ostream&, samuel::DeckOfCards const&)'

    DeckOfCardsDemo.o:DeckOfCardsDemo.cpp:(.text+0xd3): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `samuel::operator<<(std::ostream&, samuel::DeckOfCards const&)'

这是我在DeckOfCards.cpp中的功能

    std::ostream& operator << (std::ostream& out, DeckOfCards& card) 
    {
       return out << card.value() << " ";
    } 

声明

    friend ostream& operator << (ostream& out, const DeckOfCards& card);

和我的DeckOfCardsDemo.cpp

    #include <iostream>
    #include <cstdlib> 
    #include <ctime> 
    #include "DeckOfCards.h"
    using namespace std;
    using namespace samuel;

    int main(int argc, char* argv[]) {

    if (argc >= 2) {

        int seed = atoi(argv[1]);     
    }
    else {
        int seed = time(NULL);
        srand(seed);
    }
    DeckOfCards* deck = new DeckOfCards(); //creating a deck
    cout << deck->value() << endl; //printing deck 
    cout << *deck << endl;


    return EXIT_SUCCESS;


} 

1 个答案:

答案 0 :(得分:0)

支持第二个论点的类型

      DeckOfCards& card

      DeckOfCards const& card

是不同的类型。

所以,检查operator <<的声明(遗憾的是我们看不到它的声明)