Assertion failed, overloading operator=

Why does my assertion fail?
If I implemented it correctly the line " vector y(2);" in main should initialize an object of the class vector.
The constructor sets n = 2, because I used the parameter "2".
And that should also set y.n = 2.

Am I wrong here? Or did I mess up something else?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
using namespace std;

class vector {
public:
  int n; // after initializing this should equal 2, right?
  double *x;

  vector(int n) : n(n) { 
    x=new double[n]; // n=2 -> x[0] and x[1]
    for (int i=0;i<n;i++) x[i]=0;
  }
  ~vector() { delete [] x; }

  vector(const vector& y) : n(y.n) {
    x=new double[y.n]; 
    for (int i=0;i<n;i++) x[i]=y.x[i];
  }

  vector& operator=(const vector& y) {
    if (&y!=this) {
      assert(n==y.n); // this assertion fails, but why?
      for (int i=0;i<n;i++) x[i]=y.x[i];
    }
    return *this;
  }

  void dump() const {
    for (int i=0;i<n;i++) cout << x[i] << endl;
  }

  vector operator[](int i) {
    	return x[i];
  }
};

int main(int d, char*v[]) {

 vector y(2);
// y[0]=2; // vector& vector::operator=(const vecto&): Assertion 'n==y.n' failed
 y.dump(); // gives two zeros, as defined in constructor
Last edited on
I have problems compiling your code, especially at lines 32,33. x[i] is a double, not a vector. I was able to make it work by replacing 32 to 34 by
1
2
3
  double* operator[](int i) {
    	return x+i;
  }

and 30 by
*(y[0])=2;
If that is not what you want, please be more specific
Hey ats15, thanks for the help! Now it works for me too.
To be more specific:
I wanted to access the first element of the vector y, which is y[0], via "y[0]=2"

Just to get this straight, is it possible to write in y[0] with saying y[0]=2 (at this stage of the code it is not). But can i tweak something to make it look like this? And not like this *(y[0]) = 2

If not, I'm completely fine with that. I just want to know all possible ways for my program to work.

thanks,
/bored
There is already a vector class in namespace std, so I used vector1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
using namespace std;

class vector1 {
public:
  int n; // after initializing this should equal 2, right?
  double *x;

  vector1(int n) : n(n) { 
    x=new double[n]; // n=2 -> x[0] and x[1]
    for (int i=0;i<n;i++) x[i]=0;
  }
  ~vector1() { delete [] x; }

  vector1(const vector1& y) : n(y.n) {
    x=new double[y.n]; 
    for (int i=0;i<n;i++) x[i]=y.x[i];
  }

  vector1& operator=(const vector1& y) {
    if (&y!=this) {
      assert(n==y.n); // this assertion fails, but why?
      for (int i=0;i<n;i++) x[i]=y.x[i];
    }
    return *this;
  }

  void dump() const {
    for (int i=0;i<n;i++) cout << x[i] << endl;
  }

  double& operator[](int i) {
    	return x[i];
  }
};

int main(int d, char*v[]) {

 vector1 y(2);
 y[0]=2; // vector& vector::operator=(const vecto&): Assertion 'n==y.n' failed
 y.dump(); // gives two zeros, as defined in constructor
}
Topic archived. No new replies allowed.