A question about insertion stream overloading

C

Colonel

I overloaded the operator "<<" via friend function, but it doesn't work.
The errors say that can't access the private member, don't the friend
functions have
the rights to access the private member?

My origin code is as follows:
#include <cstring>
#include <iostream>

// Base class using DMA
class baseDMA
{
public:
baseDMA (const char * l = "null", int r = 0);
baseDMA (const baseDMA & rs);
virtual ~baseDMA();
baseDMA & operator = (const baseDMA & rs);
friend std::eek:stream & operator << (std::eek:stream os, const baseDMA &
rs);
private:
char * label;
int rating;
};

// baseDMA methods
baseDMA::baseDMA(const char * l, int r)
{
label = new char[strlen(l) + 1];
strcpy(label, l);
rating = r;
}

baseDMA::baseDMA(const baseDMA & rs)
{
label = new char[strlen(rs.label) + 1];
strcpy(label, rs.label);
rating = rs.rating;
}

baseDMA::~baseDMA()
{
delete label;
}

baseDMA & baseDMA::eek:perator = (const baseDMA & rs)
{
if (this == & rs)
return * this;
delete label; // free the memory first
label = new char[strlen(rs.label) + 1];
strcpy(label, rs.label);
rating = rs.rating;

return * this; // return the object
}

std::eek:stream & operator << (std::eek:stream & os, const baseDMA & rs)
{
os << "Label: " << rs.label << std::endl;
os << "Rating: " << rs.rating << std::endl;
return os;
}

IDE:Visual C++ 6.0
The complier cites the errors as follows:
Compiling...
dma.cpp
D:\C++\PrimerPlus\13\dma\dma.cpp(40) : error C2248: 'label' : cannot access
private member declared in class 'baseDMA'
d:\c++\primerplus\13\dma\dma.h(17) : see declaration of 'label'
D:\C++\PrimerPlus\13\dma\dma.cpp(41) : error C2248: 'rating' : cannot access
private member declared in class 'baseDMA'
d:\c++\primerplus\13\dma\dma.h(18) : see declaration of 'rating'
D:\C++\PrimerPlus\13\dma\dma.cpp(105) : fatal error C1071: unexpected end of
file found in comment
Error executing cl.exe.
 
P

Pete Becker

friend std::eek:stream & operator << (std::eek:stream os, const baseDMA &rs);

std::eek:stream & operator << (std::eek:stream & os, const baseDMA & rs)

Look carefully at the friend declaration and the definition.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top