Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

cMADsc

overloading << and >>

This topic is 5959 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings all, I keep getting an error when I overload the insertion and extraction operator using the new header file "iostream". I have the operator(s) declared as friends in my class but the error, "cannot access private member declared in class..C2248". Although, when I use the old header file no error occur!?? Even code straight out of one of my books get the error.
 

#include
#include
using namespace std;

class phonebook
{
	char name[80];
	int areacode;
	int prefix;
	int num;
public:
	phonebook(char *n, int a, int p, int nm)
	{
		strcpy(name, n); 
		areacode = a;
		prefix = p;
		num = nm;
	}
	friend ostream &operator<<(ostream &stream, phonebook o);
};

ostream &operator<<(ostream &stream, phonebook o)
{
	stream << o.name << " ";
	stream << "(" << o.areacode << ") ";
	stream << o.prefix << "-" << o.num << "\n";
	return stream;
}

int main()
{
	phonebook a("Ted", 111, 555, 1234);
	phonebook b("Alice", 312, 555, 5768);
	phonebook c("Tom", 212, 555, 9991);

	cout << a << b << c;
	return 0;
}
 
----------------------------- "There are ones that say they can and there are those who actually do." "...u can not learn programming in a class, you have to learn it on your own."

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
There IS a bug in MSVC++ 6.0 that has to do with the <B>using namespace</B> and also <B>friend functions</B>. Service Pack 5 is supposed to eliminate these problems.

Perhaps you should try installed that.

Share this post


Link to post
Share on other sites
Here is a small basic sample of what i did a long time ago in Computer Science.. maybe it will help a little.. This isn't the full program but i'm posting the functions that should give you an idea on how to do it..


// Main.cpp - Calls to DoubleVectorOps...
/*------------------------------------------------------------*/
#include <iostream> // cin, cout, ...
#include <fstream> // ifstream, ofstream, ...
#include <cassert> // assert()
#include <string> // string
#include <vector> // vector
using namespace std;

#include "DoubleVectorOps.h" // Average(), StandardDev(), ...

void DisplayVectors(ostream & out, vector nameVec, vector scoreVec, vector gradeVec)
{
double numValues = nameVec.size(); //get number of lines
for (int i = 0; i < numValues; i++)
{
//output name, score, and grade in that order
out << endl << nameVec << " " << scoreVec[i] << " " << gradeVec[i];
}
}


/*------------------------------------------------------------*/
// Call To DisplayVectors
cout << endl << "Mean score: "
<< Average(scoreVec)
<< endl
<< "Std. Dev: "
<< StandardDev(scoreVec)
<< endl;

DisplayVectors(cout, nameVec, scoreVec, gradeVec);

// End of Main.cpp
/*------------------------------------------------------------*/





// DoubleVevtorOps.cpp - [Holds the Definition of ostream]
/*------------------------------------------------------------*/
#include "DoubleVectorOps.h"
#include <numeric>
#include <cmath>

using namespace std;

ostream & operator<<(ostream & out, const vector & vec)
{

for (int i = 0; i < vec.size(); i++)

out << vec[i] << ' ';



return out;

}

/*------------------------------------------------------------*/


Sorry if it's a little confusing, i don't have time at the moment to rewrite it... but it is pretty basic. just forget about the vector's....

But take a Look when DisplayVectors(cout, nameVec, scoreVec, gradeVec); Gets Called.... it's passing "cout" .. lates..


[edited by - tonic151 on March 27, 2002 3:36:23 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by sjelkjd
You should probably consider getting a new book if it uses char* for strings, rather than std::string.

What''s worse:

  • it doesn''t check for NULL
  • it uses strcpy instead of strncpy
  • the n parameter should be made const


Share this post


Link to post
Share on other sites
Hey! Whats wrong with using char* s? I use em all the time.

I''ll grant you that if(str1 == str2) can be kinda handy though.



/* Ignorance is bliss, then you go and spoil it by learning stuff */

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!