Inheritance + Arrays

Nov 30, 2012 at 8:40pm
Hi!

I've made my program running, but as soon as I try to create my object, it crashes.

It looks something like this (of course I'm writing just the most important, they're separated well and the files are included correctly, otherwise it wouldn't run):

//A.h

Class A {
protected:
string something;
private:
A();
};

//A.cpp

A::A() {
something = "hi";
}

//B.h

Class B: public A {
protected:
int n;
int** array;
public:
B();
};
//B.cpp

B::B(): A() {
n = 8;
array = new int*[n];
for (int i = 0; i<n; i++) {
array[n] = new int[n];
}
for (int i = 0; i<n; i++) {
for (int j = 0; j<n; j++) {
array[i][j] = 0;
}
}
}

//C.h
class C: public B {
protected:
int v1;
int v2;
private:
C();
};

//C.cpp
cout<<helloworld<<endl;
C::C(): B() {
v1 = 1;
v2 = 2;
//extra commands
}

//main.cpp

int main() {
C Myobject;
return 0;
}
I put the hello world part to know where the problem is, the program crashes before that, so it's probably a problem with inheritance. Any help is appreciated.
Last edited on Nov 30, 2012 at 9:39pm
Nov 30, 2012 at 9:59pm
The problem is when you're allocating the 2nd dimension of your array.
It should be array[i] = new int[n];.
Nov 30, 2012 at 10:13pm
It does not compile
Nov 30, 2012 at 10:15pm
OH. MY. GOD. That one letter. Thank you very much!
Topic archived. No new replies allowed.