简体   繁体   English

错误:“新”不能出现在常量表达式中

[英]error: 'new' cannot appear in a constant-expression

class A
{
    int data;

public:
        void display()
        {
            cout<<"Value is "<<data;
        }
        void set_data(int x)
        {
            this->data = x;
        }
        A object = new A();
};

When I run the above code, I get the error stating "new cannot appear in constant expression". 当我运行上面的代码时,出现错误,指出“新值不能出现在常量表达式中”。 Why is it so? 为什么会这样呢?

Operator new returns a pointer but A is not a pointer type. 运算符new返回一个指针,但A不是指针类型。 You want A* : 您想要A*

A* object = new A();

You also want to move the above statement outside your class body and place it into appropriate function such as main() : 您还希望将以上语句移到类主体之外,并将其放入适当的函数中,例如main()

int main() {
    A* p = new A();
    // do work
    delete p;
}

That being said you either don't need a pointer at all and you can simply use an object with automatic storage duration: 话虽如此,您根本不需要指针,而您可以简单地使用具有自动存储持续时间的对象:

A object;

Or you want to consider using a smart pointer such as std::unique_ptr : 或者您想考虑使用诸如std :: unique_ptr之类的智能指针:

std::unique_ptr<A> p = std::make_unique<A>();
class A
{
public:

    A * object = new A(); // In any case not: "A object = new A();"
};   

Or: 要么:

class A
{
public:

    A object;
};

- -

See (let's assume, for a moment, that you don't get the error), in both cases, on the first construction of A object, it creates another A object as a data-member. 参见(让我们暂时假设您没有收到错误),在这两种情况下,在第一个A对象的构造上,它都会创建另一个A对象作为数据成员。 This A data-member (let's call it object.object ) creates in its turn another A as its data-member (let's call it object.object.object), and so to infinity (or until no more memory). 这个A数据成员(我们称其为object.object)依次创建另一个A作为其数据成员(我们称其为object.object.object),从而无限期(或直到没有更多内存)。 I mean, as a data-member, it can't be either as A* object = new A(); 我的意思是,作为数据成员,它不能是A* object = new A(); , or as A object; ,或作为A object;

- -

I am not sure what was your intention, but if you want to link one A-object to another A-object, the class should be something like that: 我不确定您的意图是什么,但是如果您要将一个A对象链接到另一个A对象,则该类应该是这样的:

class A
{
public:

    A * object = nullptr
};  

you have to make object of class A into main() . 您必须将A类的对象放入main()

void main(){
     A object;
}

First of all, you cannot create an object in the class declaration. 首先,您不能在类声明中创建对象。 Class declaration is like a blue print of the class. 类声明就像类的蓝图。 It is to say these are the components of my class - variables and member functions. 就是说这些是我班的组成部分-变量和成员函数。 You cannot instantiate anything inside it as no memory is allocated during this stage. 您无法实例化其中的任何内容,因为在此阶段没有分配内存。

Note that you can instantiate an object inside one of the member function including constructor. 请注意,您可以在成员函数之一(包括构造函数)中实例化一个对象。 These are called during object creation when memory is allocated. 这些在分配内存时在对象创建期间调用。

Even if you use this statement inside a constructor you will go into an infinite loop as the constructor calls its constructor and so on until you have memory overflow. 即使在构造函数内部使用此语句,构造函数调用其构造函数时也会陷入无限循环,依此类推,直到内存溢出。

You can declare the object in main like this: 您可以像这样在main中声明对象:

int main() {
    A obj = new A();
    //other operations
} //Object A is destroyed once you come out of main.

Or dynamically like this 或像这样动态

int main() {
   A* obj = new A(); //dynamic allocation
   //other operations
   delete obj; //explicitly destroy
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM