Vector pointing to Objects

hi, could you please tell me where have i gone wrong?
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <vector>
#include <cstdlib>
#include <iostream>
#include <string.h>

using namespace std;
class Stock
{
protected:
    char* name;
    int price;
public:
    virtual void display () = 0;
    virtual void setprice (int pricex) = 0;
};
class Commonstock : public Stock
{
public:
    Commonstock()
    { 
        name = new char[1];
        strcpy(name, "");
        price=0;
    }
    virtual void display ()
    {
        cout << name << endl;
        cout << price << endl;
    }
    virtual void setprice (int pricex)
    {
        price=pricex;
    }
    virtual void setname ()
    {
        char *str;
        str=new char[20];
        cin.getline(str, 20);
        int length=strlen(str);
        name= new char[length+1];
        strcpy(name, str);
    }
    ~Commonstock ()
    {
        delete []name;
    }
};
class Namedstock : public Stock
{
    char* holder_name;
public:
    Namedstock () 
    {
        name = new char[1];
        strcpy(name, "");
        holder_name = new char[1];
        strcpy(holder_name, "");
        price=0; 
    }
    virtual void display ()
    {
        cout << name << endl;
        cout << price << endl;
    }
    virtual void setprice (int pricex)
    {
        price=pricex;
    }
    virtual void setname ()
    {
        char *str;
        str=new char[20];
        cin.getline(str, 20);
        int length=strlen(str);
        name= new char[length+1];
        strcpy(name, str);
    }
    void setholder ()
    {
        char *str;
        str=new char[20];
        cin.getline(str, 20);
        int length=strlen(str);
        holder_name= new char[length+1];
        strcpy(holder_name, str);
    }
    ~Namedstock () 
    {
        delete []name;
        delete []holder_name;
    }
};
int main(int argc, char** argv) 
{
    vector<Stock*> stocks;
    stocks.push_back(new Namedstock); 
    vector<Stock*>::iterator it = stocks.begin();
    *it->setname(); 
    *it->setprice(24); 
    *it->display();
    return 0;
}


the output shows me :
main.cpp:98:10: error: request for member ‘setname’ in ‘* it.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-> [with _Iterator = Stock**, _Container = std::vector<Stock*>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::pointer = Stock**]()’, which is of non-class type ‘Stock*’


main.cpp:99:10: error: request for member ‘setprice’ in ‘* it.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-> [with _Iterator = Stock**, _Container = std::vector<Stock*>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::pointer = Stock**]()’, which is of non-class type ‘Stock*’


main.cpp:100:10: error: request for member ‘display’ in ‘* it.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-> [with _Iterator = Stock**, _Container = std::vector<Stock*>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::pointer = Stock**]()’, which is of non-class type ‘Stock*’


i dont get it ... why didnt those member functions werent created?
You are dereferencing incorrectly, which leaves you with a pointer to a pointer Stock**.
Change:
 
 *it->setname() to (*it)->setname().


In other words *it-> dereferences the iterator, not the object that the iterator points to.
By changing it to (*it)-> you are performing similar pointer arithmetic to **it->, only this is the correct way.
Last edited on
ohhh yeah i noticed it... thanks a lot friend
No problem. Just curious, are you working on something for work or learning?
a pointer of Stock can access setname? but setname is not a member method of Stock...
error C2039: 'setname' : is not a member of 'Stock'
careful - you have memory leaks all over your code

remember, the general principle is, you need a delete for every new and a delete[] for every new[]
@Melan

That is correct. He would want to either cast the element of the vector or create a list
of derived objects before using setname(). Ideally, you want to include setname(), etc to the base class to begin with :)
@johnnystarr
Its just an excercise for getting to know more about virtual functions ... i have to make a program that creates these stocks derived from Stock, which i could later manipulate its name or price or display them on screen. Using of course the the Stock as and abstract class

@Melan
dont worry melan i noticed that... this is just a quick thought...

@kfmfe04
dont worry man... this is just the first step to getting to know virtual functions...
It might help to lose the vector. I've found that when I'm trying to brush up on a core aspect of
a language, it helps to write very small applications that strictly focus on that subject. Once you
get a feel for that, then maybe you could at a container of objects.

Just my opinion talking ;)
A really nice point of view... althought the ecxercise states that a vector should be used
Does it also state to create this horrid C array mess full of memory leaks instead of using std::string?
yeap...
Then you should use a better source for getting your C++ exercises.
what do you mean???
Topic archived. No new replies allowed.