自己定义的类模版 运行时说“=”没有定义

Linkhai 2012-03-26 09:44:41
本人刚刚接触这个,按照书上写了下面这个程序,是在没有办法让它通过,
希望得到高人指点

我再VC6.0中定义了两个文件
Multiplication Table.cpp和Test.cpp


//Multiplication Table.cpp

#ifndef STACKTP_H_
#define STACKTP_H_

template <class Type>
class Stack
{
private:
enum {MAX = 10};
Type item[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)
{
item[top++]=item;
return true;
}
else
{
throw("over flow!");
return false;
}
}


template <class Type>
bool Stack<Type>::pop(Type & item)
{
if(top>0)
{
item=item[--top];
return true;
}
else
{
throw("over flow!");
return false;
}
}

#endif


//Test.cpp

#include<iostream>
#include<string>
#include"Multiplication Table.cpp"
using namespace std;



int main(void)
{
Stack<string> st;
string s;
cout<<"Please input a string!\n";
cin>>s;
st.push(s);
if(!st.isempty()) cout<<"push hase successed\n";
st.pop(s);
if(st.isempty()) cout<<"pop hase successed\n";

return 250;
}

错误提示如下

1. error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (o
r there is no acceptable conversion)
2. while compiling class-template member function 'bool __thiscall Stack<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char>
> >::push(const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)'
执行 cl.exe 时出错.
...全文
50 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Linkhai 2012-03-27
  • 打赏
  • 举报
回复
我后来知道,原来是push函数里面的item[top++]=item;名称好像冲突了,谢谢你的回答
W170532934 2012-03-26
  • 打赏
  • 举报
回复

template <class Type>
class Stack
{
private:
enum {MAX = 10};
Type item[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 & item1)
{
if(top<MAX)
{
item[top++]=item1;
return true;
}
else
{
throw("over flow!");
return false;
}
}


template <class Type>
bool Stack<Type>::pop(Type & item)
{
if(top>0)
{
item=item[--top];
return true;
}
else
{
throw("over flow!");
return false;
}
}


int main(void)
{
Stack<string> st;
string s;
cout<<"Please input a string!\n";
cin>>s;
st.push(s);
if(!st.isempty()) cout<<"push hase successed\n";
st.pop(s);
if(st.isempty()) cout<<"pop hase successed\n";

return 250;
}

VS2005编译通过

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧