C++模板类的问题,这段程序哪里错了,要怎么改
程序代码如下:#include<iostream>usingnamespacestd;template<classType>classStack{private:enum...
程序代码如下:
#include <iostream>
using namespace std;
template <class Type>
class Stack
{
private:
enum {MAX = 10};
Type items[MAX];
int top;
public:
Stack();
bool isempty();
bool isfull();
bool push(const Type & item);
bool pop(Type & item);
};
template <class Type>
Stack<Type>::Stack()
{
top=0;
}
template <class Type>
bool Stack<Type>::isempty()
{
return top==0;
}
template <class Type>
bool Stack<Type>::isfull()
{
return top == MAX;
}
template <class Type>
bool Stack<Type>::push(const Type & item)
{
if (top < MAX)
{
items[top++] = item;
return true;
}
else
return false;
}
template <class Type>
bool Stack<Type>::pop(Type & item)
{
if (top > 0)
{
item = items[--top];
return true;
}
else
return false;
}
template<template <typename T> class Thing>
class Crab
{
private:
Thing<int> s1;
Thing<double> s2;
public:
Crab();
bool push(int a,double x)
{
return s1.push(a) && s2.push(x);
}
bool pop(int &a,double &x)
{
return s1.push(a) && s2.push(x);
}
};
int main()
{
Crab<Stack> nebula;
int ni;
double nb;
cout << "enter int double pairs:\n";
while(cin>>ni>>nb&&ni>0&&nb>0)
{
if(!nebula.push(ni,nb))
break;
}
while(nebula.pop(ni,nb))
cout << ni << ", " <<nb <<endl;
cout<<"Done.\n";
return 0;
}
编译器提示错误:
||=== Build: Debug in 22 (compiler: GNU GCC Compiler) ===|
obj\Debug\111.o||In function `main':|
C:\Users\tom\Desktop\11\22\111.cpp|83|undefined reference to `Crab<Stack>::Crab()'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
谢谢大神给予指点(*^__^*) 展开
#include <iostream>
using namespace std;
template <class Type>
class Stack
{
private:
enum {MAX = 10};
Type items[MAX];
int top;
public:
Stack();
bool isempty();
bool isfull();
bool push(const Type & item);
bool pop(Type & item);
};
template <class Type>
Stack<Type>::Stack()
{
top=0;
}
template <class Type>
bool Stack<Type>::isempty()
{
return top==0;
}
template <class Type>
bool Stack<Type>::isfull()
{
return top == MAX;
}
template <class Type>
bool Stack<Type>::push(const Type & item)
{
if (top < MAX)
{
items[top++] = item;
return true;
}
else
return false;
}
template <class Type>
bool Stack<Type>::pop(Type & item)
{
if (top > 0)
{
item = items[--top];
return true;
}
else
return false;
}
template<template <typename T> class Thing>
class Crab
{
private:
Thing<int> s1;
Thing<double> s2;
public:
Crab();
bool push(int a,double x)
{
return s1.push(a) && s2.push(x);
}
bool pop(int &a,double &x)
{
return s1.push(a) && s2.push(x);
}
};
int main()
{
Crab<Stack> nebula;
int ni;
double nb;
cout << "enter int double pairs:\n";
while(cin>>ni>>nb&&ni>0&&nb>0)
{
if(!nebula.push(ni,nb))
break;
}
while(nebula.pop(ni,nb))
cout << ni << ", " <<nb <<endl;
cout<<"Done.\n";
return 0;
}
编译器提示错误:
||=== Build: Debug in 22 (compiler: GNU GCC Compiler) ===|
obj\Debug\111.o||In function `main':|
C:\Users\tom\Desktop\11\22\111.cpp|83|undefined reference to `Crab<Stack>::Crab()'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
谢谢大神给予指点(*^__^*) 展开
2个回答
展开全部
template<template <typename T> class Thing>
class Crab
{
private:
Thing<int> s1;
Thing<double> s2;
public:
Crab(){}; //没有实现代码~,可以加个大括号表示什么都不做,或者去掉这行
bool push(int a,double x)
{
return s1.push(a) && s2.push(x);
}
bool pop(int &a,double &x)
{
return s1.push(a) && s2.push(x);
}
};
class Crab
{
private:
Thing<int> s1;
Thing<double> s2;
public:
Crab(){}; //没有实现代码~,可以加个大括号表示什么都不做,或者去掉这行
bool push(int a,double x)
{
return s1.push(a) && s2.push(x);
}
bool pop(int &a,double &x)
{
return s1.push(a) && s2.push(x);
}
};
2015-10-22
展开全部
我是代码
追答
666
666
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询