C++ 模板错误 [英] C++ template error
本文介绍了C++ 模板错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在学习 C++ 模板,并且我写了一个小算法.不幸的是我有一些错误这是我的代码
I am learning C++ template, and I write a little algorithm. Unfortunately I got some errors Here is my code
#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++ 编译它时,我得到了这个:
When I use g++ to compile it, I get this:
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++ 模板的新手.
I am confused, could anyone help me to fix these bugs, I am kind of new to C++ template.
推荐答案
不要将地址传递给函数,参数是引用而不是指针
Don't pass the addresses to the functions, the arguments are references not pointers
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()];
}
这篇关于C++ 模板错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文