模板ID ’empty<>’ 不匹配任何模板声明,候选项为: ‘X’

huangapple go评论85阅读模式
英文:

template-id 'empty<>' for 'X' does not match any template declaration, candidate is: 'X'

问题

我正在尝试为我的类进行模板特化。我有一个头文件和一个源文件:

stack.h

#ifndef EX9_STACK_H
#define EX9_STACK_H

#include <deque>
#include <string>
#include <vector>

template <typename T>
class Stack
{
public:
    void push(T const& x);
    T pop();
    T top() const;
    bool empty() const;

private:
    std::vector<T> m_elems;
};

template <>
class Stack<std::string>
{
public:
    void push(std::string const& x);
    std::string pop();
    std::string top() const;
    bool empty() const;

private:
    std::deque<std::string> m_elems;
};

#include "stack.tpp"

#endif // EX9_STACK_H

stack.tpp

#include "stack.h"

template <typename T>
void Stack<T>::push(T const& x)
{
    m_elems.push_back(x);
}

template <typename T>
T Stack<T>::pop()
{
    T x;
    x = top();
    m_elems.pop_back();
    return x;
}

template <typename T>
T Stack<T>::top() const
{
    return m_elems.back();
}

template <typename T>
bool Stack<T>::empty() const
{
    return m_elems.empty();
}

template <>
void Stack<std::string>::push(std::string const& x)
{
    m_elems.push_back(x);
}

template <>
std::string Stack<std::string>::pop()
{
    std::string x;
    x = top();
    m_elems.pop_back();
    return x;
}

template <>
std::string Stack<std::string>::top() const
{
    return m_elems.back();
}

template <>
bool Stack<std::string>::empty() const
{
    return m_elems.empty();
}

在所有模板特化的定义中,我得到以下错误:

stack.tpp:31:6: error: template-id 'push<>' for 'void Stack<std::__cxx11::basic_string<char> >::push(const string&)' does not match any template declaration
   31 | void Stack<std::string>::push(std::string const& x)
      |      ^~~~~~~~~~~~~~~~~~
stack.h:25:10: note: candidate is: 'void Stack<std::__cxx11::basic_string<char> >::push(const string&)'
   25 |     void push(std::string const& x);
      |          ^~~~

候选项与我编写的相同。
对于std::string Stack<std::string>::pop()std::string Stack<std::string>::top() constbool Stack<std::string>::empty() const也有相同的错误。

我还尝试将所有代码放在同一个文件中,但是我得到了相同的错误。

英文:

I am trying to do template specialization for my class. I have a header file and a sorce file:

stack.h

#ifndef EX9_STACK_H
#define EX9_STACK_H

#include &lt;deque&gt;
#include &lt;string&gt;
#include &lt;vector&gt;

template &lt;typename T&gt;
class Stack
{
public:
    void push(T const&amp; x);
    T pop();
    T top() const;
    bool empty() const;

private:
    std::vector&lt;T&gt; m_elems;
};

template &lt;&gt;
class Stack&lt;std::string&gt;
{
public:
    void push(std::string const&amp; x);
    std::string pop();
    std::string top() const;
    bool empty() const;

private:
    std::deque&lt;std::string&gt; m_elems;
};

#include &quot;stack.tpp&quot;

#endif // EX9_STACK_H

stack.tpp

#include &quot;stack.h&quot;

template &lt;typename T&gt;
void Stack&lt;T&gt;::push(T const&amp; x)
{
    m_elems.push_back(x);
}

template &lt;typename T&gt;
T Stack&lt;T&gt;::pop()
{
    T x;
    x = top();
    m_elems.pop_back();
    return x;
}

template &lt;typename T&gt;
T Stack&lt;T&gt;::top() const
{
    return m_elems.back();
}

template &lt;typename T&gt;
bool Stack&lt;T&gt;::empty() const
{
    return m_elems.empty();
}

template &lt;&gt;
void Stack&lt;std::string&gt;::push(std::string const&amp; x)
{
    m_elems.push_back(x);
}

template &lt;&gt;
std::string Stack&lt;std::string&gt;::pop()
{
    std::string x;
    x = top();
    m_elems.pop_back();
    return x;
}

template &lt;&gt;
std::string Stack&lt;std::string&gt;::top() const
{
    return m_elems.back();
}

template &lt;&gt;
bool Stack&lt;std::string&gt;::empty() const
{
    return m_elems.empty();
}

On all the definitions of template specialization I get the following error:

stack.tpp:31:6: error: template-id &#39;push&lt;&gt;&#39; for &#39;void Stack&lt;std::__cxx11::basic_string&lt;char&gt; &gt;::push(const string&amp;)&#39; does not match any template declaration
   31 | void Stack&lt;std::string&gt;::push(std::string const&amp; x)
      |      ^~~~~~~~~~~~~~~~~~
stack.h:25:10: note: candidate is: &#39;void Stack&lt;std::__cxx11::basic_string&lt;char&gt; &gt;::push(const string&amp;)&#39;
   25 |     void push(std::string const&amp; x);
      |          ^~~~

The candidate is the same as what I wrote.
The same error is also for std::string Stack&lt;std::string&gt;::pop(), std::string Stack&lt;std::string&gt;::top() const and bool Stack&lt;std::string&gt;::empty() const.

I have also tryed to put all the code in the same file, but I get the same errors.

答案1

得分: 2

语法错误。template&lt;&gt; 始终引入显式特化,但您并未尝试显式特化模板的成员函数。您正试图定义一个模板的显式特化的成员函数,该模板本身不是一个模板。

因此,只需删除显式特化类的类外成员定义上的 template&lt;&gt;

另外,因为类的显式特化及其成员函数本身不是模板实体,您需要在单个翻译单元(.cpp 文件)中定义这些成员函数,或将它们标记为 inline。一次定义规则对它们适用的方式与普通函数一样,而不是模板函数。

英文:

The syntax is wrong. template&lt;&gt; always introduces an explicit specialization, but you are not trying to explicitly specialize the member function of a template. You are trying to define a member function of an explicit specialization of a template, which itself is not a template.

So just remove the template&lt;&gt; on the out-of-class member definitions for the explicitly specialized class.

Also, because the explicit specialization of the class and its member functions are not themselves templated entities, you need to either define the member functions in a single translation unit (.cpp) file or mark them inline. The one-definition-rule applies to them in the same way it applies to normal functions, not templated ones.

huangapple
  • 本文由 发表于 2023年7月3日 04:24:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定