class object's function that has function.

1
2
Foo *myDevice = getDevice("hello");
Ham *myDriver = myDevice->getDriver();


^above is what I want to do.


below is how my classes and functions are made.

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
class Foo{
    public:
        void setName(string text){
            name = text;
        }
        string getName(){
            return name;
        }
        void printName(){
            cout << name << endl;
        }
        Ham *getDriver(){
            Ham *driver = new Ham;
            
            cout << "driver" << endl;
            
            return driver;
        }
    private:
        string name;
};

class Ham{
    public:
        void printDriver(){
            cout << "driver" << endl;
        }
};

Foo *getDevice(string name = "name not defined"){
    Foo *device = new Foo;
    device->setName(name);
    device->printName();
    
    cout << device << endl;
    
    return device;
}

Foo *getDriver(){
    Foo *driver = new Ham;
    
    return driver;
}



int main(){
    Foo *myDevice = getDevice("hello");
    Ham *myDriver = myDevice->getDriver();

    return 0;
}



I'm getting an error around myDevice->getDriver();.

Should getDriver() return Ham object? How would you make Ham object be able to use getDriver()? thanks.
Your problem is that on line 13 you have to know the definition of Ham, but you define Ham much later, on lines 23-28. Move Ham's definition to before Foo's definition.


PS. Lines 40-44 make no sense.
Topic archived. No new replies allowed.