不能将一个指针赋值给另一个指针,因为类型不匹配,或者编译器是这么说的
Cannot assign one pointer to another because of type mismatch, or so the compiler says
这是头文件代码,stack.h:
#include <iostream>
using namespace std;
//template <class T> struct stackNode;
//template <class T>
struct stackNode {
int item;
stackNode *Next;
stackNode *Prev;
//stackNode *Temp;
};
class stackClass {
public:
stackClass();
stackClass(const stackClass &right);
~stackClass();
stackClass &operator=(const stackClass &right);
int counter;
int StackIsEmpty(void);
//stackNode *origptr;
void push( int item, bool &success);
int pop(void);
int GetStackTop(void);
protected:
stackNode *GetStackTopPtr(void);
private:
stackNode *Top;
stackNode *Current;
//stackNode *origptr;
//stackNode<T> *Next;
};
下面是实现文件stack.cpp:
#include "stack.h"
//constructor
stackClass::stackClass(){
Top = NULL;
int counter = 0;
}
//copy constructor
stackClass::stackClass(const stackClass &right){
if (right.Top == NULL){
Top = NULL;
counter = 0;
}
else {
stackNode * origptr = right.GetStackTopPtr;
stackNode * currptr;
Top = new stackNode;
currptr = Top;
while(origptr->Next != NULL){
currptr->item = origptr->item;
origptr = origptr->Next;
currptr->Next = new stackNode;
currptr->Next->item = origptr->item;
currptr = currptr-> Next;
}
currptr->Next = NULL;
}
}
//Destructor
stackClass::~stackClass(){
while (Top != NULL){
pop();
}
}
//push
void stackClass::push(int item, bool &success){
success = false;
if (Top == NULL){
Top = new stackNode;
Top -> item = item;
Top -> Next = NULL;
Top -> Prev = NULL;
Current = Top;
counter++;
success = true;
}
else {
stackNode * Temp = new stackNode;
Current -> Next = Temp;
Temp -> Prev = Current;
Temp -> Next = NULL;
Current = Temp;
Current -> item = item;
counter++;
success = true;
}
}
int stackClass::pop(void){
if (Top == NULL){
cout<<"Stack is empty"<<endl;
}
else if (counter == 1){
Top = NULL;
delete Current;
counter--;
}
else if(counter > 1){
Current -> Prev -> Next = NULL;
Current = Current -> Prev;
stackNode * Temp = Current -> Next;
delete Temp;
counter--;
}
}
int stackClass::StackIsEmpty(void){
if (counter == 0)
return 1;
else
return 0;
}
int stackClass::GetStackTop(void){
int item = Top->item ;
return item;
}
stackNode *stackClass::GetStackTopPtr(void){
return Top;
}
问题就在这里,
stack.cpp:在复制构造函数' stackClass::stackClass(const stackClass&) ':
stack.cpp:19:31:错误:无法将' stackClass::GetStackTopPtr '从类型' stackknode * (stackClass::)() '转换为类型' stackknode * '
我不知道为什么编译器会抱怨,似乎在有问题的行中,在stack.cpp中,第19行,right.GetStackTopPrt
将返回stackNode *
类型,这应该很好地分配给左侧的stackNode*
。
请帮助如果你知道是什么问题。谢谢!
你想设置
stackNode * origptr = right.GetStackTopPtr;
式中,左侧为stackNode
指针,右侧为函数 GetStackTopPtr
。我想你的意思是:
stackNode * origptr = right.GetStackTopPtr(); // Note the parentheses!
请记住,编译器非常擅长找出类型,并且在这些事情上几乎总是正确的。不要假设编译器是错误的,先找出你犯的错误!
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- 这是系统资源吗?(或者我怎么知道我是否需要删除指针) - 在 C++ 中使用 C
- C 中的堆是否包含类和结构对象,或者仅包含指针
- (C++)通过指针递增,或者:为什么这不起作用?指针->int = 指针->int+1;
- C++:图形 ADT 应该有一个顶点列表和一个边列表,或者只是带有指向其他顶点的指针的顶点
- 最好强制转换列表中的一个元素,或者使用指向它的指针
- 如何初始化指针数组(或者我是否需要这样做)
- 不能将一个指针赋值给另一个指针,因为类型不匹配,或者编译器是这么说的
- 指向对象的指针没有任何值(或者没有得到值)
- 如何从不透明的指针类型定义进行shared_ptr?(或者:如何"unwrap" typedef?
- 引用是否可以跨二进制边界工作,或者必须使用指针