Redefinition using Constructor

80 views Asked by At

I cant understand why I have redefinition trying to run this example. Can anyone tell me?

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

There are 1 answers

0
Piotr Dajlido On BEST ANSWER

Witaj Przemeku Na StackOverflow!

How about this?:

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