Iterators & pointers

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?

Thanks so much
Last edited on
Would I be right in going at the end?

(*it) -> display();

I still get this error though- I know it's from my push_back loop so I'm not sure what's wrong there still.


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 = DateTime*]' [-fpermissive]




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.
You don't need pointer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
enum EColor { None = 0, Orange, Blue, Gree, etc };
struct Colours
{
  int m_Color;

  Colours(int c = None) : m_Color{c}
  {
  }
};

    vector<Colours> coloursvect;

    for(int i=1; i<=24; i++){
		coloursvect.emplace_back(i);
	} 

    for(vector<Colours>::iterator it = coloursvect.begin();
    it != coloursvect.end(); 
    ++it){

        it -> display() ;
    }
Last edited on
added 24 colours & derived classes into the vector

the following program adds 3 instead of 24 but you should get the idea:
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
#include <iostream>
#include <vector>
#include <string>
#include <memory>

struct Colors
{
    std::string m_name;
    Colors(const std::string& name)
        : m_name (name){}
    virtual void display()const
    {
        std::cout << "base class: " << m_name << "\n";
    }
};
struct brightColors : public Colors
{
    using Colors::Colors;
    void display ()const
    {
        std::cout << "derived class bright: " << m_name << "\n";
    }
};
struct dullColors : public Colors
{
    using Colors::Colors;
    void display()const
    {
        std::cout << "derived class dull: " << m_name << "\n";
    }
};

int main()
{
    std::vector<Colors*> myVec{};
    myVec.push_back(new Colors{"primary"});
    myVec.push_back(new brightColors{"purple"});
    myVec.push_back(new dullColors{"yellow"});
    for (const auto& elem : myVec) elem -> display();
    for (auto& elem : myVec)delete(elem);
}

however this program is not exception safe and for the reasons see here:
http://www.cplusplus.com/forum/beginner/214920/#msg999700

So a more robust program would use std::unique_ptr:
1
2
3
4
5
6
7
8
9
int main()
{
    std::vector<std::unique_ptr<Colors>> myVec{};
    myVec.push_back(std::make_unique<Colors>(Colors("primary")));
    myVec.push_back(std::make_unique<brightColors>(("purple")));
    myVec.push_back(std::make_unique<dullColors>(("yellow")));
    for (const auto& elem : myVec) elem -> display();
//    for (auto& elem : myVec)delete(elem);//not required
}

Topic archived. No new replies allowed.