c++ template specialization - linker error multiple definitions

前端 未结 1 1329
有刺的猬
有刺的猬 2021-02-19 20:33

My third question here today ;-), but I am really new to c++ template programming and operator overloading.

I am trying the following:

terminallog.hh

<         


        
1条回答
  •  误落风尘
    2021-02-19 21:14

    This function

    Terminallog &Terminallog::operator<<(const char v[]) {
        std::cout << std::endl;
        this->indent();
        std::cout << v;
        return *this;
    }
    

    is not a template, but a regular member function. If this .h file is included in several .cpp filtes, it will cause the multiple definition error you're seeing. If you declare it inline, the compiler will allow the multiple definitions and your error should be fixed.

    inline
    Terminallog &Terminallog::operator<<(const char v[]) {
    

    0 讨论(0)
提交回复
热议问题