【问题标题】:Friend template function declared inside template class causing undefined symbol link error在模板类中声明的朋友模板函数导致未定义的符号链接错误
【发布时间】:2016-07-14 14:08:34
【问题描述】:

几天来我一直在努力解决这个问题,查找它并在开源项目中寻找类似的代码:无法真正找到我做错了什么。

基本上,鉴于下面的代码(提炼其本质):

#include <iostream>


using std::cout;
using std::endl;
using std::string;

template <typename T>
class Node {
    T value_;
public:
    Node(const T& value) : value_(value) {}
    T const value() const { return value_; }

    friend
    std::ostream& operator <<(std::ostream& out, const Node<T>& node);

    Node<T> operator +(const Node<T>& other) {
        return Node(value() + other.value());
    }
};


template <typename T>
std::ostream& operator <<(std::ostream& out, const Node<T>& node) {
    return out << node.value();
}

在这样的代码中使用时:

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

    Node<string> node("node X");
    cout << node << endl;

    Node<int> three(3);
    cout << three << endl;

    return EXIT_SUCCESS;
}

我收到以下链接器错误:

Undefined symbols for architecture x86_64:
  "operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > const&)", referenced from:
      _main in StlPractice.cpp.o
  "operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Node<int> const&)", referenced from:
      _main in StlPractice.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

据我所知,以上都是合法的 C++11 代码;模板定义明确,但它似乎以某种方式逃脱了链接器找到它的能力。

这是在 OS X 上使用 cmake 构建的:

cmake_minimum_required(VERSION 3.3)
project(simple_template)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES src/simple_template.cpp)

add_executable(simple ${SOURCE_FILES})

什么给了?

提前致谢!

更新在问题之后,我还运行了以下相同的结果:

$ clang++ src/simple_template.cpp 
Undefined symbols for architecture x86_64:
  "operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > const&)", referenced from:
      _main in StlPractice-e20370.o
  "operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Node<int> const&)", referenced from:
      _main in StlPractice-e20370.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

【问题讨论】:

  • 是的,它可以编译,并且主要包含“通常的嫌疑人”iostream;我正在使用 cmake - 将其添加到问题中。谢谢你的链接,我会在此期间检查一下。

标签: c++ templates c++11 operator-overloading friend


【解决方案1】:

类内声明为friend是非模板函数,类外定义是模板函数,两者不匹配。而对于overload resolution,在模板函数特化之前会选择非模板函数,这就是出现未定义符号链接错误的原因。

您可以将声明更改为模板函数:

template<typename X>
friend std::ostream& operator <<(std::ostream& out, const Node<X>& node);

LIVE

或者在类中定义函数:

friend 
std::ostream& operator <<(std::ostream& out, const Node<T>& node) { return out << node.value(); }

LIVE

【讨论】:

  • 谢谢!这就是正是它 - 我刚刚发现了friend 函数中的“罪魁祸首”,因为删除了类中的声明,这一切都奏效了。我没有意识到我还必须在类定义中添加template!
  • @Marco 是的,您需要说明它是一个模板函数。非模板函数和模板函数是不一样的。
【解决方案2】:

您的定义将与您没有的模板化朋友声明相匹配。

有时您只想允许非常特定的Node&lt;T&gt; 类型,因此您可以这样做。

http://rextester.com/GZKCJQ35441

template <typename T>
class Node {
    T value_;
public:
    Node(const T& value) : value_(value) {}
    T const value() const { return value_; }

    friend 
    std::ostream& operator <<(std::ostream& out, const Node<T>& node);

    Node<T> operator +(const Node<T>& other) {
        return Node(value() + other.value());
    }
};

std::ostream& operator <<(std::ostream& out, const Node<int>& node) { return out << node.value_; }
std::ostream& operator <<(std::ostream& out, const Node<string>& node) { return out << node.value_; }

或者只是改变你的定义,让它成为一个模板化的朋友。

【讨论】:

    【解决方案3】:

    大约有三种方法可以为您的类模板Node&lt;T&gt; 重载operator&lt;&lt;:

    1. 由于您提供了public 成员函数value(),因此您实际上不需要friend,而是可以完全按照@ 的public 接口定义一个非朋友非成员函数模板987654330@

    Live Example 1:

    template <typename T>
    std::ostream& operator <<(std::ostream& out, const Node<T>& node) {
        return out << node.value();
    }
    
    1. 对于Node&lt;T&gt; 的每个特化,您可以使用定义类内的非成员函数(并且将存在于包含您的类模板的命名空间中)

    Live Example 2:

    template <typename T>
    class Node {        
        // generates a non-template operator<< for this T
        friend std::ostream& operator<<(std::ostream& out, const Node<T>& node) {
            return out << node.value_;
        }   
    };
    
    1. 对于Node&lt;T&gt; 的每个特化,您可以通过使用operator&lt;&lt; &lt;&gt; 语法(或等效的operator&lt;&lt; &lt;T&gt; 语法)声明friend 来定义函数模板的伴随特化

    Live Example 3

    // forward declare to make function declaration possible
    template <typename T>
    class Node;
    
    // declaration
    template <typename T>
    std::ostream& operator <<(std::ostream& out, const Node<T>& node);
    
    template <typename T>
    class Node {
        // refers to a full specialization for this particular T 
        friend std::ostream& operator<< <>(std::ostream& out, const Node<T>& node);    
        // note: this relies on template argument deduction in declarations
        // can also specify the template argument with operator<< <T>"    
    };
    
    // definition
    template <typename T>
    std::ostream& operator <<(std::ostream& out, const Node<T>& node) {
        return out << node.value_;
    }
    

    还有第四种可能性,将template &lt;typename U&gt; operator&lt;&lt;(std::ostream&amp;, const Node&lt;U&gt;&amp;) 的所有专业化与类模板Node&lt;T&gt; 交朋友(@songyuanyao 回答的第一个选项),但在我看来,这是矫枉过正。

    如果您可以根据公共接口表达 I/O,我建议使用选项 1,否则建议使用选项 2 或 3。这两者大部分是等价的,只是在名称查找和参数推导期间的隐式转换方面存在一些细微差别。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 2017-02-09
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      相关资源
      最近更新 更多