【问题标题】:Redefinition using Constructor使用构造函数重新定义
【发布时间】:2015-06-10 19:38:19
【问题描述】:

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

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");
}

【问题讨论】:

  • 应该标记为c++,但我批准了一个待处理的编辑,但没有注意到它丢失了。
  • 你到底得到了什么错误?
  • 我有错误 - 重新定义,不同的基本类型,但 Piotr Dajlido 提供了帮助。

标签: c++ constructor redefinition


【解决方案1】:

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! ;)

【讨论】:

    猜你喜欢
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    相关资源
    最近更新 更多