我试图使用同一类的另一个成员进行数组成员
我的代码还有更多,但是我卡住的地方是,在运行此代码时,它说对非静态成员“ A”的使用无效。这种方式是无效的还是我的代码有问题?
include <iostream>
using namespace std;
class equation{
public :
int a;
int arr[a][2];
void seta(int r){
a = r;
}
void setcoef(equation c){
for(int i = 0; i<c.a; i++){
cin>>arr[i][1];
}
}
There is more to my code but where I'm stuck is that while I run this code it says INVALID USE OF NON STATIC MEMBER 'a'. Is this way invalid or is there something wrong with my code?
include <iostream>
using namespace std;
class equation{
public :
int a;
int arr[a][2];
void seta(int r){
a = r;
}
void setcoef(equation c){
for(int i = 0; i<c.a; i++){
cin>>arr[i][1];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,在标准C ++中,数组的大小必须为编译时间常数,但是由于
a
是非静态数据成员需要使用一个对象。换句话说,我们需要此
,该在编译时不可用,而是在运行时可用,因此我们会得到上述错误,说:solve 您可以使
a <
指针)。a < /code> a
constexpr static
数据成员如下所示。这是因为使用静态数据成员不需要类对象(因此您可以使用
std :: vector :: vector
如下所示:The problem is that in standard C++ the size of an array must be a compile time constant but since
a
is a non-static data member it requires an object to be used on. In other words we needthis
which is available not at compile time but at runtime and hence we get the mentioned error saying:To solve this you can make
a
aconstexpr static
data member as shown below. This works because using a static data member doesn't require a class object(and hencethis
pointer).You can use
std::vector
as shown below: