未定义引用`operator <<(std :: ostream&,/ *具有非类型模板参数的类* /&)'

时间:2020-04-16 18:52:51

标签: c++ templates linker-errors friend-function

对于家庭作业,我必须使用非类型模板参数创建一个类,然后向其添加std::i / ostream运算符。但是,当我尝试编译clang ++时,出现链接器错误:

$ clang++ -o foo ./*.cpp -std=c++11 -Wall -Wextra -Wpedantic -Wconversion -Wnon-virtual-dtor
/tmp/16_15-8cda65.o: In function `main':
main.cpp:(.text+0x108): undefined reference to `operator<<(std::ostream&, Screen<9ul, 9ul> const&)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我知道模板声明和定义必须在同一个翻译单元中,这里有很多问题和答案指出了这一点。

我的简化代码如下:

main.cpp:

#include <iostream>

#include "Screen.h"

int main()
{
    Screen<9,9> smile =
    {
        {0,0,0,1,1,1,0,0,0},
        {0,1,1,0,0,0,1,1,0},
        {0,1,0,0,0,0,0,1,0},
        {1,0,0,1,0,1,0,0,1},
        {1,0,0,0,0,0,0,0,1},
        {1,0,1,0,0,0,1,0,1},
        {0,1,0,1,1,1,0,1,0},
        {0,1,1,0,0,0,1,1,0},
        {0,0,0,1,1,1,0,0,0}
    };

    std::cout << smile;

    return 0;
}

Screen.h:

#ifndef SCREEN_H
#define SCREEN_H

#include <iostream>
#include <array>
#include <initializer_list>
#include <cstddef>

template <std::size_t W, std::size_t H>
class Screen
{
    /////////////
    // FRIENDS //
    /////////////

    friend std::ostream& operator<<(std::ostream&, const Screen<W,H>&);

    public:

        // declarations of ctors, public members, etc.

    private:

        //////////
        // DATA //
        //////////

        std::array<std::array<bool,W>,H> pixels;
};

/////////////////
// NON-MEMBERS //
/////////////////

// ostream operator
template <std::size_t W, std::size_t H>
std::ostream& operator<<(std::ostream&, const Screen<W,H>&);

#include "Screen_impl.h"
#endif

Screen_impl.h:

#ifndef SCREEN_IMPL_H
#define SCREEN_IMPL_H

#include <iostream>
#include <array>
#include <algorithm>
#include <stdexcept>
#include <initializer_list>
#include <cstddef>

// definitions...

/////////////////
// NON-MEMBERS //
/////////////////

// ostream operator
template <std::size_t W, std::size_t H>
std::ostream& operator<<(std::ostream& lhs, const Screen<W,H>& rhs)
{
    for (auto y = rhs.pixels.cbegin(); y < rhs.pixels.cend(); ++y)
    {
        for (auto x = y->cbegin(); x < y->cend(); ++x)
        {
            if (*x)
                lhs << '#';
            else
                lhs << ' ';
        }

        lhs << std::endl;
    }

    return lhs;
}

#endif

2 个答案:

答案 0 :(得分:2)

在类内部声明的operator<<函数不是函数模板,但是稍后您将定义函数模板。因此,您要声明的实体与您定义的实体不同。

您需要在类中将函数模板声明为

template <std::size_t WX, std::size_t HX>
friend std::ostream& operator<<(std::ostream&, const Screen<WX, HX>&);

请注意,模板参数的名称必须与类模板参数的名称不同,以免产生阴影。

您也可以只在类中定义operator<<

答案 1 :(得分:1)

这位朋友接线员

template <std::size_t W, std::size_t H>
class Screen
{
    /////////////
    // FRIENDS //
    /////////////

    friend std::ostream& operator<<(std::ostream&, const Screen<W,H>&);
    //

不是模板运算符。

因此,其作为模板运算符的定义无效。那不是在类定义中声明的非模板Friend运算符的定义。

结果,编译器发出错误,提示找不到操作符的定义。

将friend运算符的定义放在类定义中。在这种情况下,该定义将用于该类的每个专业化。

否则,您将需要为该类的每个具体专业分别定义运算符。

这是一个演示程序。

#include <iostream>
#include <iterator>
#include <numeric>

template <size_t N>
class Array
{
private:
    int a[N];

public:
    Array() 
    {
        std::iota( std::begin( a ), std::end( a ), 0 );
    }

    friend std::ostream & operator <<( std::ostream &os, const Array &a )
    {
        for ( const auto &item : a.a ) os <<item << ' ';
        return os;
    }
};

int main() 
{
    std::cout << Array<1>() << '\n';
    std::cout << Array<2>() << '\n';
    std::cout << Array<3>() << '\n';
    std::cout << Array<4>() << '\n';
    std::cout << Array<5>() << '\n';

    return 0;
}

其输出为

0 
0 1 
0 1 2 
0 1 2 3 
0 1 2 3 4