Problem with object argument

One of my classes is supposed to change a value on my object, but how do I pass the object to the function?

Updated code if you scroll down...

EDIT: Updated code!
Last edited on
In Class1.cpp line 13, you should just use number or this->number. Corge.number doesn't make sense because Corge is not an object, it's a type.

In class2.cpp, if you want to use an array you should pass the size of the array as an additional parameter than use a for loop to go through each of the elements individually. I would just recommend using one of the standard containers though.
I think you do not need to pass any parameter because the function and the array are members of the same class. Just use a loop in the function to change all the values of the arrays.

HTH,
Aceix.
Last edited on
I managed to get it to compile, but still not sure I'm doing it right.
This is what I have now:

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
#include <iostream>

class Corge
{
    public:
        std::string text;
        int number;

        Corge();
        void changeNumber();
};

class Grault
{
    Corge waldo[10];

    void changeAll(Corge waldo);    //This function is supposed to take a waldo object as argument.
};

int main()
{
    Grault plugh;

    for(int i = 0; i < 10; i++)
    {
        plugh[i].changeAll(plugh[i]);
    }

Corge::Corge()
{
    text   = "Name";
    number = 1;
}

void Corge::changeNumber()
{
    std::cout << "Change number to: ";
    std::cin >> Corge.number;
}

void Grault::changeAll(Corge waldo)
{
    waldo.changeNumber();
}


Is this a good solution?
Check my hint.

NOTE: it is with reference with the first post.

Aceix.
Topic archived. No new replies allowed.