How to get compiler to recognize previous declarations

/**

How would you get a class higher in the program
to recognize another class that is lower in the
program....? For example, "other class" hasn't a
problem recognizing the "myFuncs" class. How would
I get that to work the other way around if the
compiler compiles downward? Thanks.

**/

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 <string>
#include <vector>
#include <iterator>
///Practice

class myFuncs
{
friend void printInteger(myFuncs cFuncs);///Play with this friend func
private:
    int number;

public:
    myFuncs(){number = 0;}
    void print()
    {
            std::cout<<"The number is "<<number<<std::endl<<std::endl;
    }
    void setNumberToWhateverIChoose(int num){number = num;}
};

class otherClass
{
public:
    void printInteger(myFuncs cFunc){cFunc.print();}
};

int main()
{
    std::vector<int> myVec;
    std::vector<int>::iterator myVecIterator;
    myFuncs cFunc;
    otherClass cOther;

    cFunc.setNumberToWhateverIChoose(19);
    cFunc.print();
    cOther.printInteger(cFunc);

    return 0;
}
I think the word you are looking for is "forward declaration". This is where you make known a class to other objects before actually implementing the class
Last edited on
I just looked up "forward declaration" and you're right. Thanks but it's not in my book anywhere. Would you happen to have a link explaining how to implement it. I looked here and got confused. Thanks
chief wrote:
Would you happen to have a link explaining how to implement it.


http://lmgtfy.com/?q=forward+declaration+c%2B%2B
In short,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class myFuncs; // Forward declaration

class otherClass
{
public:
    void printInteger(myFuncs cFunc); // Okay, compiler knows myFuncs exists
};

class myFuncs
{
    // ...
};

void otherClass::printInteger(myFuncs cFunc)
{
    cFunc.print(); // Okay, compiler knows the full definition of myFuncs now
}
Sorry for the late, late reply but thanks a lot. Definitely helped.

@LB That article was too much but very informative. Need to study a little. Thanks a bunch. Opened my eyes to swerve a lot of future headaches.
Last edited on
Topic archived. No new replies allowed.