【发布时间】: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<S>?
标签: c++ parameter-passing