【问题标题】:C++ template errorC++ 模板错误
【发布时间】:2014-06-04 10:52:26
【问题描述】:

我正在学习C++模板,我写了一个小算法。不幸的是我遇到了一些错误 这是我的代码

    #include <iostream>
#include <iomanip>
#include <deque>
using namespace std;

template<typename T, typename S, typename W>
void push(const T& value, deque<S>& original_stack, deque<W>& min_index)
{
  original_stack.push_back(value);
  if (min_index.size() == 0)
    min_index.push_back(0);
  else
    {
      if (value < original_stack[min_index.back()])
    {
      min_index.push_back(original_stack.size() - 1);
    }
      else
    {
      min_index.push_back(min_index.back());
    }
    }
}

template<typename S, typename W>
void pop(deque<S>& original_stack, deque<W>& min_index)
{
  original_stack.pop_back();
  min_index.pop_back();
}

template<typename T, typename S, typename W>
const T& min(deque<S>& original_stack, deque<W>& min_index)
{
  return original_stack[min_index.back()];
}

int main()
{
  deque<int> data_stack;
  deque<int> min_index;
  push(3, &data_stack, &min_index);
  push(4, &data_stack, &min_index);
  push(2, &data_stack, &min_index);
  push(1, &data_stack, &min_index);
  pop(&data_stack, &min_index);
  pop(&data_stack, &min_index);
  push(0, &data_stack, &min_index);
  cout<<"min num is: "<<min(&data_stack, &min_index)<<endl;
}

当我使用 g++ 编译它时,我得到了这个:

g++ minstack.cpp -o minstack
minstack.cpp:42:3: error: no matching function for call to 'push'
  push(3, &data_stack, &min_index);
  ^~~~
minstack.cpp:7:6: note: candidate template ignored: could not match
      'deque<type-parameter-0-1, allocator<type-parameter-0-1> >' against
      'std::__1::deque<int, std::__1::allocator<int> > *'
void push(const T& value, deque<S>& original_stack, deque<W>& min_index)
     ^
minstack.cpp:43:3: error: no matching function for call to 'push'
  push(4, &data_stack, &min_index);
  ^~~~
minstack.cpp:7:6: note: candidate template ignored: could not match
      'deque<type-parameter-0-1, allocator<type-parameter-0-1> >' against
      'std::__1::deque<int, std::__1::allocator<int> > *'
void push(const T& value, deque<S>& original_stack, deque<W>& min_index)
     ^
minstack.cpp:44:3: error: no matching function for call to 'push'
  push(2, &data_stack, &min_index);
  ^~~~
minstack.cpp:7:6: note: candidate template ignored: could not match
      'deque<type-parameter-0-1, allocator<type-parameter-0-1> >' against
      'std::__1::deque<int, std::__1::allocator<int> > *'
void push(const T& value, deque<S>& original_stack, deque<W>& min_index)
     ^
minstack.cpp:45:3: error: no matching function for call to 'push'
  push(1, &data_stack, &min_index);
  ^~~~
minstack.cpp:7:6: note: candidate template ignored: could not match
      'deque<type-parameter-0-1, allocator<type-parameter-0-1> >' against
      'std::__1::deque<int, std::__1::allocator<int> > *'
void push(const T& value, deque<S>& original_stack, deque<W>& min_index)
     ^
minstack.cpp:46:3: error: no matching function for call to 'pop'
  pop(&data_stack, &min_index);
  ^~~
minstack.cpp:26:6: note: candidate template ignored: could not match
      'deque<type-parameter-0-0, allocator<type-parameter-0-0> >' against
      'std::__1::deque<int, std::__1::allocator<int> > *'
void pop(deque<S>& original_stack, deque<W>& min_index)
     ^
minstack.cpp:47:3: error: no matching function for call to 'pop'
  pop(&data_stack, &min_index);
  ^~~
minstack.cpp:26:6: note: candidate template ignored: could not match
      'deque<type-parameter-0-0, allocator<type-parameter-0-0> >' against
      'std::__1::deque<int, std::__1::allocator<int> > *'
void pop(deque<S>& original_stack, deque<W>& min_index)
     ^
minstack.cpp:48:3: error: no matching function for call to 'push'
  push(0, &data_stack, &min_index);
  ^~~~
minstack.cpp:7:6: note: candidate template ignored: could not match
      'deque<type-parameter-0-1, allocator<type-parameter-0-1> >' against
      'std::__1::deque<int, std::__1::allocator<int> > *'
void push(const T& value, deque<S>& original_stack, deque<W>& min_index)
     ^
7 errors generated.

我很困惑,谁能帮我修复这些错误,我是 C++ 模板的新手。

【问题讨论】:

  • 为什么你要传递你的双端队列的地址,而不是函数所要求的简单引用? IE。 push(3, data_stack, min_index);
  • 对我来说,你有一个处理外来对象地址的函数看起来很糟糕。闻起来像 C 代码。我更喜欢 OOP!
  • 当您只将T 类型的对象推入其中时,为什么original_stack 的类型为deque&lt;S&gt;

标签: c++ parameter-passing


【解决方案1】:

您在呼叫站点遇到问题,例如:

deque<int> data_stack;
deque<int> min_index;
push(3, &data_stack, &min_index);

如果您将上述push() 函数调用与您的push() 模板重载进行比较:

template<typename T, typename S, typename W>
void push(const T& value, deque<S>& original_stack, deque<W>& min_index)

您可以注意到第二个和第三个参数是引用(对一些deque 实例)。

但在调用站点上,您使用地址运算符 (&amp;) 传递 deque 实例的 地址,如果将参数定义为 指针不是引用)。

由于您选择在函数定义中使用引用(而不是指针),因此只需去掉调用站点的地址运算符即可。
这样就好了:

push(3, data_stack, min_index); // References used

其他函数调用也是如此。


请注意,这是一个与引用与指针相关的问题。您可以抽象出模板部分,只需考虑以下简单的 C++ 代码即可解决问题:

void f(int& n) {
    n++;
}

int main() {
    int i = 1;
    // f(&i); // <-- fails to compile
    f(i);
}

由于 f() 函数需要 referenceint,因此您不想在调用站点使用指针传递 int i 变量:只需传递 @ 987654333@(不是&amp;i)。

【讨论】:

    【解决方案2】:

    不要将地址传递给函数,参数是引用而不是指针

      push(3, data_stack, min_index);
      push(4, data_stack, min_index);
      push(2, data_stack, min_index);
      push(1, data_stack, min_index);
      pop(data_stack, min_index);
      pop(data_stack, min_index);
      push(0, data_stack, min_index);
    

    你需要明确告诉min的返回类型

    cout << min<int>(data_stack, min_index) << std::endl ;
    

    或使用:

    template<typename S, typename W>
     typename deque<S>::value_type& min(deque<S>& 
                                      original_stack, deque<W>& min_index)
    {
      return original_stack[min_index.back()];
    }
    

    【讨论】:

      【解决方案3】:
      original_stack.push_back(value); // T and S should have the same type or at least T should be derived from S
      

      但这不是问题。

      int main()
      {
        deque<int> data_stack;
        deque<int> min_index;
        push(3, data_stack, min_index);
        push(4, data_stack, min_index);
        push(2, data_stack, min_index);
        push(1, data_stack, min_index);
        pop(data_stack, min_index);
        pop(data_stack, min_index);
        push(0, data_stack, min_index);
        cout<<"min num is: "<<min(data_stack, min_index)<<endl;
      }
      

      如您所见,我删除了所有地址运算符,因为您是通过引用传递的;当您的参数是指针时,您需要传递地址。

      另外,min 的返回类型应该是 S,因为你返回栈的一个元素。 而且你甚至不需要 W,因为索引总是整数。

      【讨论】:

      • TS 不需要派生或具有相同的类型。 T 只需转换为 S。如果它们是派生出来的,你会得到切片。
      • 好的,谢谢。我不知道如何表达自己。
      猜你喜欢
      • 2015-08-05
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      相关资源
      最近更新 更多