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?

Recommended Answers

All 5 Replies

>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:

dont't declare it with const keyword. A class object declared like that is pretty useless.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.