I'm not grasping the concept at all when there's so many options, and therefore my program implementation is failing.
I'm trying to get my main function working but getting errors, could someone point me in the right direction please? I had it working but then I realised I had to change formats and now back to square one :-(
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
vector<Colours*> coloursvect(24);
for(int i=1; i<=24; i++){
coloursvect.push_back(i);
}
//added 24 colours & derived classes into the vector using
colours[0] = new Colours(Orange) etc (should there be a better way to do all that?
for(vector<Colours*>::iterator it = coloursvect.begin();
it != coloursvect.end();
++it){
//cout<< *it<<" ";
//for (unsigned int i = 0; i < coloursvect.size(); i++){
it -> display() ;
}
It gives me the following errors:
Task_2_5.cpp: In function 'int main()':
task_2_5.cpp:13:20: error: invalid conversion from 'int' to 'std::vector<Colours*>::value_type {aka Colours*}' [-fpermissive]
In file included from c:\mingw-4.7.1\bin\../lib/gcc/mingw32/4.7.1/include/c++/vector:65:0,
from task_2_5.cpp:2:
c:\mingw-4.7.1\bin\../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_vector.h:881:7: error: initializing argument 1 of 'void std::vector<_Tp, _Alloc>
::push_back(const value_type&) [with _Tp = Colours*; _Alloc = std::allocator<Colours*>;
std::vector<_Tp, _Alloc>::value_type = Colours*]' [-fpermissive]
task_2_5.cpp:52:15: error:
request for member 'display' in '* it.__gnu_cxx
::__normal_iterator<_Iterator, _Container>::operator-><Colours**, std::vector<Colours*> >()',
which is of pointer type Colours*' (maybe you meant to use '->' ?)
I've tried different combinations of pointers and nothing works. What am I missing?
If you have a vector of Colours pointers you have to store Colour pointers in your vector.
Do you really have to use pointers?
Much easier:
1 2 3 4 5 6 7
vector<Colours> colours;
for (int i = 0; i < 25; i++)
colours.push_back( Colours(Orange)); // choose different colors
// To dislpay
for (const Colours c : colours)
c.display();
Hmm... I believe I need the pointers, it gives me pages of errors if I don't have it, but I think I can get around having the for loop and just declare the vector as having 24 objects then using colours[0-24] = new Colours(Orange) in there.