我尝试使用我在上一个问题中得到的优秀答案来理解这个问题,但我不明白为什么这段代码不能编译。
#include <iostream>
#include <ostream>
#include <string>
#include <cstdlib>
#include <vector>
#include <memory>
template <typename T,
template <typename ELEM,
typename = std::allocator<ELEM> >
class CONT = std::deque>
class Stack {
public:
typedef typename CONT<T>::size_type size_type;
private:
CONT<T> elems; // elements
public:
void push(T const&); // push element
void pop(); // pop element
T top() const; // return top element
bool empty() const { // return whether the stack is empty
return elems.empty();
}
size_type size();
};
template <typename T, template <typename,typename> class CONT>
void Stack<T,CONT>::push (T const& elem)
{
elems.push_back(elem); // append copy of passed elem
}
template<typename T, template <typename,typename> class CONT>
void Stack<T,CONT>::pop ()
{
if (elems.empty()) {
throw std::out_of_range("Stack<>::pop(): empty stack");
}
elems.pop_back(); // remove last element
}
template <typename T, template <typename,typename> class CONT>
T Stack<T,CONT>::top () const
{
if (elems.empty()) {
throw std::out_of_range("Stack<>::top(): empty stack");
}
return elems.back(); // return copy of last element
}
template<typename T,
template<typename, typename= std::allocator<T> > class CONT>
typename Stack<T, CONT>::size_type Stack<T, CONT>::size()
{
return elems.size();
}
template <typename T,
template <typename ELEM, typename = std::allocator<ELEM> > class CONT>
static inline void Pout(const CONT<T>& container)
{
typedef typename CONT<T >::size_type size_type;
size_type idx = 0;
size_type sz = CONT<T>::size();
CONT<T> temp = container;
std::cout << '[';
while (idx < sz)
{
std::cout << temp.top();
temp.pop();
idx++;
if (idx == sz) break;
std::cout << ", ";
}
std::cout << "]";
}
int main()
{
try {
Stack<int, std::vector > vStack;
//...
vStack.push(42);
vStack.push(7);
Pout<Stack<int, std::vector > >(vStack);
}
catch (std::exception const& ex) {
std::cerr << "Exception: " << ex.what() << std::endl;
}
}
我使用g ++ 4.3.4获得了编译器错误:
stack8test.cpp: In function int main():
stack8test.cpp:28: error: no matching function for call to Pout(Stack<int, std::vector>&)
make: *** [stack8test.o] Error 1
我将非常感谢您的帮助。
我做了更改以注释掉函数调用Pout&gt;(vStack)并将vStack的内容打印到std :: cout。这是新代码。它按预期编译和工作:
#include <iostream>
#include <ostream>
#include <string>
#include <cstdlib>
#include <vector>
#include <memory>
#include <deque>
#include <stdexcept>
template <typename T,
template <typename ELEM,
typename = std::allocator<ELEM> >
class CONT = std::deque>
class Stack {
public:
typedef typename CONT<T>::size_type size_type;
typedef typename CONT<T>::value_type value_type;
private:
CONT<T> elems; // elements
public:
void push(T const&); // push element
void pop(); // pop element
T top() const; // return top element
bool empty() const { // return whether the stack is empty
return elems.empty();
}
size_type size();
void Pout();
};
template <typename T, template <typename,typename> class CONT>
void Stack<T,CONT>::push (T const& elem)
{
elems.push_back(elem); // append copy of passed elem
}
template<typename T, template <typename,typename> class CONT>
void Stack<T,CONT>::pop ()
{
if (elems.empty()) {
throw std::out_of_range("Stack<>::pop(): empty stack");
}
elems.pop_back(); // remove last element
}
template <typename T, template <typename,typename> class CONT>
T Stack<T,CONT>::top () const
{
if (elems.empty()) {
throw std::out_of_range("Stack<>::top(): empty stack");
}
return elems.back(); // return copy of last element
}
template<typename T,
template<typename, typename= std::allocator<T> > class CONT>
typename Stack<T, CONT>::size_type Stack<T, CONT>::size()
{
return elems.size();
}
template <typename T,
template<typename, typename = std::allocator<T> > class CONT>
void Stack<T, CONT>::Pout()
{
size_type idx = 0;
size_type sz = size();
CONT<T> temp(elems); // make a temp copy of the underlying container and print the temp, since printing is destructive. Note that the underlying CONT must already support copy constructor.
std::cout << std::endl << '[';
while (idx < sz)
{
std::cout << temp.back();
temp.pop_back();
idx++;
if (idx == sz) break;
std::cout << ", ";
}
std::cout << "]" << std::endl;;
}
template <typename T,
template <typename ELEM, typename = std::allocator<ELEM> > class CONT>
void Pout(const CONT<T>& container)
{
typedef typename CONT<T >::size_type size_type;
size_type idx = 0;
size_type sz = CONT<T>::size();
CONT<T> temp = container;
std::cout << '[';
while (idx < sz)
{
std::cout << temp.top();
temp.pop();
idx++;
if (idx == sz) break;
std::cout << ", ";
}
std::cout << "]";
}
int main()
{
try {
Stack<int, std::vector > vStack;
//...
vStack.push(42);
vStack.push(7);
// Pout<Stack<int, std::vector > >(vStack);
vStack.Pout();
std::cout << "vStack = [" << vStack.top(); vStack.pop();
std::cout << ", " << vStack.top() << "]" << std::endl; vStack.pop();
}
catch (std::exception const& ex) {
std::cerr << "Exception: " << ex.what() << std::endl;
}
}
编译器输出为:
g++ -O2 -g -Wall -c -o stack8test stack8test.cpp
运行程序,我得到了:
./stack8test
vStack = [7, 42]
我真正的问题是找到函数模板Pout的正确语法来打印出我的类模板Stack的内容。为了澄清,我的意图是将类模板Stack实现为类型为T的元素的容器。我想根据std :: vector或std :: deque类型的std容器实现Stack,甚至std :: list 。
我打电话给Pout:
Pout<int, Stack<int, std::vector>>(vStack);
但是编译器仍然说:
stack8test.cpp: In function int main():
stack8test.cpp:137: error: no matching function for call to Pout(Stack<int, std::vector>&)
make: *** [stack8test.o] Error 1
更新:为了解决我的原始(即非成员)函数的原始问题,将我的Stack的内容打印到std :: cout,我已经定义了Pout()作为会员功能。它编译并按预期工作。找到全局函数的正确语法仍然是一个挑战。
答案 0 :(得分:2)
你的事情太复杂了。您应该让编译器尽可能多地为您推断出信息,而不是编写
template <typename T,
template <typename ELEM, typename = std::allocator<ELEM> > class CONT>
static inline void Pout(const CONT<T>& container)
简单地写
template <class CONT>
static inline void Pout(const CONT& container)
这是在STL中完成的。如果您需要访问容器的基础类型,请再次使用STL方法并将value_type
typedef添加到类中:
class Stack {
public:
...
typedef T value_type;
...
}
然后,您可以在CONT::value_type
函数中访问它。但是,您甚至不在Pout
函数中使用此信息。
其他一些错误:
size
功能标记为const
,因为它应该是static inline
说明符。可能,你不是。在修复这些错误后编译代码,但总体设计很差。