Verified:

TheORKINMan Game profile

Member
1305

Sep 30th 2011, 14:11:10

So I'm working on assignments where we have to define a class with a default constructor, two argument constructor, destructor, assignment operator etc.... as well as the implementation file for it and then use the instructors test.cpp which houses int main() to run it. I was going along fine until I got to the part where I have to do the << operator for outputting an ID object which only has two variables. name_ which is a dynamically allocated string (char*) and age_ which is just an int and holds a number value.

In my header file I have:

std::ostream& operator << ( std::ostream& os, const ID& id );

now in my implementation I'm stuck as to how to output this. I can't just put os << id.name_ << ' ' << id.age_; because std::ostream& is not a member function of the class (and the professor does not want us to declare it as a friend). Any thoughts on how I'd go about doing this?
Smarter than your average bear.

Dibs Ludicrous Game profile

Member
6702

Sep 30th 2011, 16:07:19

you going for a system's degree?
the syntax is over my head.
where are you trying to output the thingie?
There are no messages in your Inbox.
Elvis has left the building.

TheORKINMan Game profile

Member
1305

Sep 30th 2011, 17:06:19

It's just computer science at Florida State and it's actually not even an advanced class :P COP3330 Object Oriented Programming which is the second programming class of the track.

I am defining what the int main() function is supposed to output when you do cout << objectname. I actually figured it out though. ostream operators cannot be members of the class or friends of the class, and the data in the .h file is private, however you can make a public function that returns the value of the data members and call that in the << operator. At least I'm hoping it works :P Next semester is Data Structures, Algorithms, and Generic Programming which by the course description is supposed to be 100x harder than this so I'm hoping I am starting to figure this out ;P
Smarter than your average bear.

Xinhuan Game profile

Member
3728

Sep 30th 2011, 20:33:03

Since your class is not allowed to declare that overloaded << operator function as a friend function, your only ways are:

A) Make the members age_ and name_ public members of the class.

B) Create ::GetAge() and ::GetName() public methods that return those member values respectively and have your overloaded operator function use them for its output.


Yes, its that simple.

TheORKINMan Game profile

Member
1305

Sep 30th 2011, 20:41:48

Yep Xinhuan, I did eventually come to that conclusion as well and made those functions although it took me a day to let it sink in and realize that's what I needed to do :P I'm still having a problem when it comes to outputting though. This is my operator implementation:


std::ostream& operator << ( std::ostream& os, const ID& id )
{
os << (*id).GetName() << ' ' << (*id).GetAge();
return os;
}

When I try to compile int main() in g++ I get the following error:

test.cpp:26: error: initializing argument 1 of âstd::ostream& operator<<(std::ostream, const ID&)â
test.cpp:42: warning: deprecated conversion from string constant to âchar*â

If I can get over this hurdle I think I'm home free as that's the last warning/error g++ is giving me.
Smarter than your average bear.

Xinhuan Game profile

Member
3728

Sep 30th 2011, 21:18:56

os << id.GetName() << ' ' << id.GetAge();

Xinhuan Game profile

Member
3728

Sep 30th 2011, 21:22:05

Oh and make sure your ::GetName() and ::GetAge() are const member functions for that to work.

Dibs Ludicrous Game profile

Member
6702

Sep 30th 2011, 21:51:20

does it have to be delared before you use it? what's initialization error mean, grass hopper?
There are no messages in your Inbox.
Elvis has left the building.

TheORKINMan Game profile

Member
1305

Sep 30th 2011, 22:39:12

I'm just an idiot, I had the reference ampersand missing from my header file, that + xinhaun's suggestion fixed it and now my default constructor is outputting beautifully, readded in the two argument constructor to the test code and it's back to breaking again, with no errors or warnings, just the command prompt telling me your program has stopped working which is lovely :P
Smarter than your average bear.