I'm doing an assignment for my OOP class, and I am a little confused about the use of operator overloading. I've seen a few examples, but not using the operator<<, and I don't quite get how this works. Here is the part I'm having trouble with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
ID::ID()
{
SetName("#");
SetAge(-1);
};
ID::ID ( const char * name, int age )
{
SetName(name);
SetAge(age);
};
|
I have included the following in my public class declaration:
|
friend std::ostream &operator<<(std::ostream &, ID &);
|
And I need to be able to output what is passed into the constructor using the following:
1 2 3
|
ID p1, p2("Jane Doe", 25);
std::cout<< " p1 = " << p1 << '\n';
std::cout<< " p2 = " << p2 << '\n';
|
This should read:
1 2
|
p1 = # -1
p2 = Jane Doe 25
|
I am confused about the operator overloading part, and right now I'm getting a linker error saying:
Undefined symbols for architecture x86_64:
"operator<<(std::ostream&, ID&)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any help or a push in the right direction would be greatly appreciated. Thanks in advance.
**Update**
And now Im extra confused, I just realized that I'm not allowed to use 'friend', so I went back and changed the line in my header file to:
|
std::ostream &operator<<(std::ostream &output, ID &p1);
|
but now I'm getting "Overloaded 'operator<<' must be a binary operator(has 3 parameters)"
What now?