C2664错误,请问这个怎么解决?

院里有座山 2021-01-16 11:24:20

#ifndef STACKTP1_H_
#define STACKTP1_H_

template<class Type>
class Stack
{
private:
static const int SIZE = 10;
int stacksize;
Type* items;
int top;
public:
explicit Stack(int ss = SIZE);
Stack(const Stack& st);
~Stack()
{
delete[]items;
}
bool isempty() { return top == 0; };
bool isfull() { return top == stacksize; };
bool push(const Type& item);
bool pop(Type& item);
Stack& operator=(const Stack& st);
};

template<class Type>
inline Stack<Type>::Stack(int ss) :stacksize(ss), top(0)
{
items = new Type[stacksize];
}

template<class Type>
Stack<Type>::Stack(const Stack& st)
{
stacksize = st.stacksize;
top = st.top;
items = new Type[stacksize];
for(int i = 0; i < top; i++)
items[i] = st.items[i];
}

template<class Type>
bool Stack<Type>::push(const Type& item)
{
if (top < stacksize)
{
items[top++] = item;
return true;
}
return false;
}

template<class Type>
bool Stack<Type>::pop(Type& item)
{
if (top > 0)
{
item = items[--top];
return true;
}
return false;
}

template<class Type>
Stack<Type>& Stack<Type>::operator=(const Stack<Type>& st)
{
if (this == &st)
return *this;
delete[]items;
stacksize = st.stacksize;
top = st.top;
items = new Type[stacksize];
for (int i = 0; i < top; i++)
items[i] = st.items[i];
return *this;
}

#endif // !STACKTP1_H_


#include <iostream>
#include <cstdlib>
#include <ctime>
#include "stacktp1.h"

using std::cin;
using std::cout;
using std::endl;
using std::string;

const int Num = 10;

int main()
{
srand(time(0));
cout << "Please enter stack size: ";
int stacksize;
cin >> stacksize;
Stack<char*>st(stacksize);

const char* in[Num] = {
" 1: Hank Gilgamesh"," 2: Kiki Ishtar",
" 3: Betty Rocker"," 4: Ian Flagrant",
" 5: Wolfgang Kibble"," 6 Portia Koop",
" 7: Joy Almondo"," 8: Xaverie Paprika",
" 9: Juan Moore"," 10: Misha Mache"
};
const char* out[Num];

int processed = 0;
int nextin = 0;
while (processed < Num)
{
if (st.isempty())
st.push(in[nextin++]);
else if (st.isfull())
st.pop(out[processed++]);
else if (rand() % 2 && nextin < Num)
st.push(in[nextin++]);
else
st.pop(out[processed++]);
}

for (int i = 0; i < Num; i++)
{
cout << out[i] << endl;
}

cout << "Bye\n";
return 0;
}


严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C2664 “bool Stack<char *>::push(const Type &)”: 无法将参数 1 从“const char *”转换为“const Type &” Project3 C:\Users\shuru\source\repos\Project3\Project3\stkoptr1.cpp 35
...全文
1230 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
真相重于对错 2021-01-16
  • 打赏
  • 举报
回复
const const 没有意义,还是const
院里有座山 2021-01-16
  • 打赏
  • 举报
回复
引用 1 楼 真相重于对错 的回复:
泛型里面const是影响模板类型决定。也就是说const char* 不是char* 你的in 是const char* ,而你的stack模板类型 是char*,吧他俩改成一致
Stack<const char*>st(stacksize); 我如果这样声明 bool Stack<Type>::push(const Type& item) { if (top < stacksize) { items[top++] = item; return true; } return false; } 那这个函数的形参,实际上会编译成什么? const const char*& 模板是怎么替换形参的?
真相重于对错 2021-01-16
  • 打赏
  • 举报
回复
泛型里面const是影响模板类型决定。也就是说const char* 不是char* 你的in 是const char* ,而你的stack模板类型 是char*,吧他俩改成一致
qybao 2021-01-16
  • 打赏
  • 举报
回复
模板类型推导可以参考以下 http://blog.leanote.com/post/gaunthan/C-模板类型推导

65,210

社区成员

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

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