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?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include 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
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
 ``123`` `````` 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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include 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
Topic archived. No new replies allowed.