错误:请求成员'..'在'this',这是非类类型' - * const' [英] error: request for member '..' in 'this', which is of non-class type '--* const'
问题描述
我的第一个问题在这里。请原谅我,我刚刚进入C ++,并开始与DS。 STACK !!!
我的代码:我认为
std;
typedef char stackElement;
类栈
{
public:
stackElement * contents; //动态分配:因为我们不知道什么是我们的数组的大小。
int top,maxSize; // current数组中的最高索引
//数组的最大尺寸;我们需要它知道数组是否已满
stack(int maxSize)
{
contents = new stackElement(maxSize);
this.maxSize = maxSize;
if(contents == NULL)
{
cout<<Insufficient memory;
exit(1);
}
top = -1;
}
〜Stack()
{
delete [] content;
contents = NULL;
maxSize = 0;
top = -1;
}
bool isEmpty()const
{
return top< 0;
}
bool isFull()const
{
return top == maxSize - 1;
}
void push(stackElement element)
{
if(isFull())
{
cout< ;STACK IS ALREADY FULL;
exit(1);
}
top = top + 1;
contents [top] = element;
}
};
int main()
{
cout<<STACK IMPLEMENTATION;
int i = 1;
stack s1(i);
s1.push('a');
s1.push('1');
return 0;
}
我收到此错误:
错误:请求'this'中的成员'maxSize',它是非类型'Stack * const'
如果有,你必须写 this-> maxSize = maxSize;
,因为此
是一个指针。
但是最好不要写这个,而是使用构造函数初始化列表:
显式Stack(int m)
:contents(new stackElement [m]),top(-1),maxSize(m)
{
//没有别的事情
}
我还添加了 explicit
,所以你不会意外转换 5
into a Stack
。
/ p>
此外,您不需要检查 contents
不为null:当 new
失败,退出时出现异常,它不会返回空指针。
这是至关重要的,注意,你至多有一个赤裸裸的 new
- 在您的构造函数中。其他任何事情都是一个例外 - 安全灾难,您需要重构和使用单一责任资源管理类。
析构函数应该是:〜Stack(){delete [] content; }
想象一下,你必须支付
strong>为你写的每一行代码。耐心,失去源头,思考。My first ever question here. Please excuse me, I have just entered into C++ and was starting up with DS. STACK!!!
My code: I think
using namespace std;
typedef char stackElement;
class Stack
{
public:
stackElement *contents; //dynamically allocated: as we do not know what would be the size of our array.
int top, maxSize; // current Top index in the array
//max size of the array; we need it to know if the array is full
Stack(int maxSize)
{
contents = new stackElement(maxSize);
this.maxSize = maxSize;
if(contents == NULL)
{
cout<<"Insufficient memory";
exit(1);
}
top = -1;
}
~Stack()
{
delete [] contents;
contents = NULL;
maxSize = 0;
top = -1;
}
bool isEmpty()const
{
return top < 0;
}
bool isFull() const
{
return top == maxSize - 1;
}
void push(stackElement element)
{
if(isFull())
{
cout<<"STACK IS ALREADY FULL";
exit(1);
}
top = top + 1;
contents[top] = element;
}
};
int main()
{
cout<<"STACK IMPLEMENTATION";
int i = 1;
Stack s1(i);
s1.push('a');
s1.push('1');
return 0;
}
I am getting this error:
error: request for member 'maxSize' in 'this', which is of non-class type 'Stack* const'
If at all, you'd have to write this->maxSize = maxSize;
, since this
is a pointer.
But better not to write that at all and instead use the constructor-initializer list:
explicit Stack(int m)
: contents(new stackElement[m]), top(-1), maxSize(m)
{
// nothing else to do
}
I also added explicit
so you don't accidentally convert 5
into a Stack
.
You also wrote the array initialization wrong.
Also, you don't need to check that contents
is not null: When new
fails, it exits with an exception, it does not return a null pointer. (That behaviour would make no sense when you think in terms of objects.)
It is crucial to note that you have at most one naked new
-expression in your constructor. Anything else is an exception-safety disaster, and a sign that you need to refactor and use single-responsibility resource-managing classes.
The destructor should just be: ~Stack() { delete [] contents; }
Everything else is pointless waste.
Imagine you had to pay for every line of code you write. Be patient, lose the source, think.
这篇关于错误:请求成员'..'在'this',这是非类类型' - * const'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!