【问题标题】:Unresolved external symbol on operator++ in template [duplicate]模板中运算符++上未解析的外部符号[重复]
【发布时间】:2013-01-25 02:25:14
【问题描述】:

可能重复:
Why do I get “unresolved external symbol” errors when using templates?

我正在制作一个链接列表。我正在使用外部迭代器。 Iterator 类是一个模板,我在 Iterator.h 中实现我的方法。

这是模板:

#pragma once

#include "Node.h"

 namespace list_1
 {

template<typename T>
class Iterator
{
public:
    Iterator<T> (Node<T> *np);
    void operator++();
    bool is_item();
    T operator* ();

private:
    Node<T>* n;
};

template<typename T>
Iterator<T>::Iterator (Node<T> *np)
{

}

template<typename T>
void Iterator<T>::operator++()
{

}

template<typename T>
bool Iterator<T>::is_item()
{
    return false;
}

template<typename T>
T Iterator<T>::operator* ()
{

}
 }

我在尝试编译时收到此错误消息:1&gt;list_test.obj : error LNK2019: unresolved external symbol "public: void __thiscall list_1::Iterator&lt;double&gt;::operator++(void)"

加上整个项目中其他七个类似的错误。

我在这里做错了吗?还是我做错了什么?

谢谢!

【问题讨论】:

  • list_1 是类还是命名空间? Iterator 类定义是在 list_1 定义内部还是外部? operator++ 定义是在 list_1 定义内部还是外部?
  • 我在operator 实现之前没有看到inline,所以我显然与模板声明不在同一个头文件中。除非这全部都在一个单个源文件中,否则如果没有显式实例化,您将无法获得所需的内容。
  • 抱歉,Iterator 在命名空间 list_1 中。 template&lt;typename T&gt; void Iterator&lt;T&gt;::operator++() 也在 list_1 中。
  • @WhozCraig:inline 在模板函数或类模板的成员函数上根本没有做太多事情。
  • @aschepler 你是对的,但对于非模板代码,它会引发重复引用,因此我总是在任何类外成员 impl 上使用它,甚至是模板的.然而,你是最正确的。

标签: c++ templates operator-overloading


【解决方案1】:

如果我正确阅读了您的错误消息,您的迭代器将 Node&lt;T&gt; 作为输入,但是您将 double 应用于它,这是不适用的。要支持non-Node&lt;T&gt;类型,需要专门化Iterator&lt;T&gt;

public: void __thiscall list_1::Iterator<double>::operator++(void)"
                                         ^^^^^

【讨论】:

    猜你喜欢
    • 2016-06-19
    • 2011-10-21
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 2013-12-18
    • 1970-01-01
    相关资源
    最近更新 更多