Hi,

if we define a vector class,
class vector
{
private:
int size;
int* array;
...........
public:
vector (int n=10);
.........
}

then we use it in main like this:

int main()
{
const vector v(5);
cout <<v(1) <<endl;
return 0;
}

there's a problem with compiling this code, i think because v is not initiliased and declared as const, and because the program is trying to output something both uninitialised and const, but how can i solve this problem, because the default constructor, as far as i know in this case, allocates an array of size 10 but without initialising, so how am i supposed to change class vector in order to make it compile?

>there's a problem with compiling this code
Of course, it could also be the syntax errors.

>so how am i supposed to change class vector in order to make it compile?
Maybe I'm missing something, but if you want to change the contents of your object, you shouldn't have declared it as const in the first place. :icon_rolleyes:

no i am supposed to add something in class vector in order to make it work. that's the question i am supposed to solve, as useless as it is.

initialize the data in the constructor then. Or add another const method to do that

class vector
{
...
const void initialize() const;
};

int main()
{
   const vector v(5);
   v.initialize();

}

>that's the question i am supposed to solve, as useless as it is.
Smack your teacher for me.

This article has been dead for over six months. Start a new discussion instead.