使用构造函数重定义

Redefinition using Constructor

本文关键字:定义 构造函数      更新时间:2023-10-16

我不明白为什么我试图运行此示例要重新定义。谁能告诉我?

using namespace std;
class Base {
        protected: int *value;
        public: Base() { 
           value = new int, 
          *value = 1; 
        };
        Base(int &n) { 
            value = new int[n]; 
        }
};
int main() {
        int x=2;
        Base zm;
        Base(x);
        system("Pause");
}

Witaj przemeku na stackOverflow!

这个怎么样?

class Base {
        protected: int *value;
        public: 
        Base() { 
           value = new int, 
          *value = 1; 
        };
        Base(int &n) { 
            value = new int[n]; 
        };
};
int main()
{
        int x;
        x = 2;
        Base base;
        base = Base(x); <--- fix
        return 1;
}

Proszę bardziej formatować kod! ;)