friend not getting the private members - c++

I've a class called Packet That I want to serialize with QDataStream I overloaded operator>> and operator<< and in the over loaded function I called stream << somIntMember Though Its declared as friend its complaining for Private Variables
error: 'int DG::Packet::_state' is private
error: 'DG::Packet::PacketType DG::Packet::_type' is private
Here goes my Header.
namespace DG{
class Packet{
public:
struct CommonHeader{
public:
quint32 id;
QTime time;
quint32 size;
PacketType packetType;
public:
CommonHeader();
CommonHeader(quint32 sz, PacketType type);
friend QDataStream& operator<<(QDataStream&, const Packet::CommonHeader& header);
friend QDataStream& operator>>(QDataStream&, Packet::CommonHeader& header);
};
private:
PacketType _type;
int _state;
public:
friend QDataStream& operator<<(QDataStream&, const Packet& packet);
friend QDataStream& operator>>(QDataStream&, Packet& packet);
};
}
And here goes the Ciode
#include "packet.h"
using namespace DG;
QDataStream& operator<<(QDataStream& stream, const Packet& packet){
stream << packet._state << packet._type;
return packet.serialize(stream);
}

Well, the reason for the no match for 'operator>>' error is
that there isn't any match for the operator>>, at least not in
the code you've shown. The only operator>> and operator<<
in the code you've shown are for Packet::CommonHeader and for
Packet. Nothing for a quint32, nor for a QTime, nor for
a PacketType, nor for an int.
For that matter, the implementations you've shown us are for
Packet::CommonHeader and Packet; the classes, however, are
in namespace DG, and not in the global namespace.
That could also explain the reason why friend isn't working.
The operators you've declared as friend are in namespace DG,
those you define are in the global namespace (and are thus
completely unrelated functions).

First remove the const from the rhs parameters of in the >> operators as you are modifying them.

Related

friend function has no access to private class members

I am still new to C++ and as part of an assignment I have written a class that needs an overload of the stream extraction operator '>>' for file stream extraction in order to make things a bit easier, so says the instructions. I have declared and defined 2 overloads for both operators, one oveload for iostream objects and one overload for fstream object. Now, everything is fine until i get to the definition of '>>' for file stream objects, apparently that function has no access to the private (or protected) member of the class of which it is a friend of.
Here is my code, i thank you all in advance:
stock.h
#ifndef STOCK_H
#define STOCK_H
#include<iostream>
#include<fstream>
class Stock_Type
{
friend std::ostream& operator<<(std::ostream&, const Stock_Type&);
friend std::istream& operator>>(std::istream&, Stock_Type&);
friend std::ofstream& operator<<(std::ofstream&, const Stock_Type&);
friend std::ifstream& operator>>(std::ofstream&, Stock_Type&);
public:
//constructor overloads-----------------------------------------------------------------------------------------------------
Stock_Type(){};
Stock_Type(std::string sym, double a, double b, double c, double d, double e, int f, double g) :
stock_symbol(sym), opening_price(a), closing_price(b), high_price(c), low_price(d), prev_close(e), volume(f), percent_gain(g) {}
//default destructor--------------------------------------------------------------------------------------------------------
~Stock_Type(){};
//accessor functions--------------------------------------------------------------------------------------------------------
void set_Symbol(std::string x){stock_symbol = x;}
void set_Closing_Price(double x){closing_price = x;}
void set_High_Price(double x){high_price = x;}
void set_Low_Price(double x){low_price = x;}
void set_Prev_Close(double x){prev_close = x;}
void set_Volume(int x){volume = x;}
std::string get_Stock_Smybol(){return stock_symbol;}
double get_Opening_Price(){return opening_price;}
double get_Closing_Price(){return closing_price;}
double get_High_Price(){return high_price;}
double get_Low_Price(){return low_price;}
double get_Prev_Close(){return prev_close;}
int get_Volume(){return volume;}
double get_Percent_Gain_Loss(){return get_Closing_Price() - get_Opening_Price();}
//operations on Stock_Type-------------------------------------------------------------------------------------------------------
//operator functions--------------------------------------------------------------------------------------------------------------
bool operator==(const Stock_Type&)const;
bool operator!=(const Stock_Type&)const;
bool operator<(const Stock_Type&) const;
bool operator<=(const Stock_Type&)const;
bool operator>(const Stock_Type&)const;
bool operator>=(const Stock_Type&)const;
friend std::ostream& operator<<(std::ostream&, const Stock_Type&);
friend std::istream& operator>>(std::istream&, Stock_Type&);
const Stock_Type& operator=(const Stock_Type &right_operand);
Stock_Type& operator[](int elem);
const Stock_Type& operator[](int elem) const;
private:
std::string stock_symbol;//record data1
double opening_price, closing_price, high_price, low_price, prev_close;//record data2
int volume;//record data3
double percent_gain;//record data 4
Stock_Type *stock_pointer;
int array_size;
};
#endif
stock.cpp, i will only include the function for which an error is generated
std::ifstream& operator>>(std::ifstream& if_obj, Stock_Type& stock_obj)
{
if_obj >> stock_obj.stock_symbol
>> stock_obj.opening_price
>> stock_obj.closing_price
>> stock_obj.high_price
>> stock_obj.low_price
>> stock_obj.prev_close
>> stock_obj.volume
>> stock_obj.percent_gain;
return if_obj;
}
the error is that for all the attributes that are listed are "inaccessible"
I'd like to finish this assignment as I'd like to move on to another one that is due on exception handling.
Thank you all in advance, once again.
i apologize, I believe that the signatures in the definition and implementation were different, as pointed out by a previous comment, i declared a friend function twice and there i saw my error, i did not change the code after copy/pasting it. For all future readers: MAKE SURE DECALRATION SIGNATURES MATCH IMPLEMENTATION SIGNATURES!
There is a
friend std::ifstream& operator>>(std::ofstream&, Stock_Type&);
but you have access problems in
std::ifstream& operator>>(std::ifstream& if_obj, Stock_Type& stock_obj)
Adding another friend should help.
friend std::ifstream& operator>>(std::ifstream& if_obj, Stock_Type& stock_obj);
Actually, you might prefer to edit the existing friend, it very much looks like a typo or other kind of trivial error.

C++ namespaces, inner classes and operator resolution

In the C++ namespace myspace I have a class Outer which in turn has an inner class Inner. While I can declare and define a global friend operator QDataStream& operator<<(QDataStream& s, const myspace::Outer& o), I cannot see how to declare a global friend operator QDataStream& operator<<(QDataStream& s, const myspace::Outer::Inner& o). The commented out lines represent a failed attempt. I do not see how to declare the inner class without defining the outer.
namespace myspace {
class Outer;
//class Outer::Inner;
}
QDataStream& operator<<(QDataStream& s, const myspace::Outer& o);
//QDataStream& operator<<(QDataStream& s, const myspace::Outer::Inner& o);
namespace myspace {
class Outer {
friend QDataStream& (::operator <<)(QDataStream&, const Outer&);
class Inner {
//friend QDataStream& (::operator <<)(QDataStream&, const Inner&);
int i;
};
int o;
};
}
I have read
Namespaces and operator resolution, C++ Defining the << operator of an inner class, Accessing private class in operator<< in namespace
and Operator overloading, name resolution and namespaces, but none seem to work.
If I uncomment these lines, the first gives the error message "outer.h:7: error: 'Inner' in 'class myspace::Outer' does not name a type
class Outer::Inner;
^"
This seems to be the key. I cannot declare the inner class.
I am using C++ 11.
This question is not a duplicate of Forward declaration of nested types/classes in C++ if it can can be solved without forward reference.
Given the time lapse, I am posting the correct answer given by Andreas H.
namespace myspace {
class Outer {
class Inner {
friend QDataStream& operator<<(QDataStream&, const Inner&);
int i;
};
friend QDataStream& operator<<(QDataStream&, const Outer&);
friend QDataStream& operator<<(QDataStream&, const Inner&);
int o;
};
QDataStream& operator<<(QDataStream& s, const myspace::Outer& o) {
s << o.o;
return s;
}
QDataStream& operator<<(QDataStream& s, const myspace::Outer::Inner& i) {
s << i.i;
return s;
}
}

cannot access private members in friend ostream

I tried to make friend ostream function. The compiler say i cannot access private member of the class, even though i declared it as friend. I read a similar question and it say the problem is with the namespcaes.(the question: C++ friend function can't access private members)
My Code is below:
header:
#include <iostream>
#include <string>
//using namespace std;
namespace biumath
{
#ifndef BIUMATH_H
#define BIUMATH_H
class Assignment
{
private:
int **m_varArray;
int m_rowsOfVarArray;
public:
Assignment(); //1
Assignment(char symbol, double value); //2
bool containsValueFor(char symbol) const; //3
double valueOf(char symbol) const; //4
void add(char symbol, double val); //5
friend std::ostream& operator<< (std::ostream& out,
const Assignment& assignment);
};
}
#endif
cpp:
#include <iostream>
#include "biumath.h"
using namespace biumath;
using std::cout;
using std::endl;
ostream& operator<< (ostream& out,
const Assignment& assignment){
out<<assignment.m_rowsOfVarArray<<std::endl;
//return the stream. cout print the stream result.
return out;
}
or better yet, avoid all unnecessary friendships by deferring to a public utility method (this also has benefits when your Assignment is a polymorphic base class):
in header file:
namespace biumath
{
class Assignment
{
private:
int **m_varArray;
int m_rowsOfVarArray;
public:
Assignment(); //1
Assignment(char symbol, double value); //2
bool containsValueFor(char symbol) const; //3
double valueOf(char symbol) const; //4
void add(char symbol, double val); //5
void write(std::ostream& os) const; // <=== public helper
};
// free function overload which defers to helper.
// NOTE: it's in the biumath namespace so ADL will find it
inline std::ostream& operator<< (std::ostream& out,
const Assignment& assignment){
assignment.write(out);
return out;
}
}
in CPP file:
namespace biumath {
void Assignment::write(std::ostream& os) const {
os << m_rowsOfVarArray << std::endl;
}
}
You may define the operator in the enclosing namespace (in your case in the global namespace) but you have to use the qualified name.
Thus define the operator like
ostream& biumath::operator<< (ostream& out,
const Assignment& assignment){
out<<assignment.m_rowsOfVarArray<<std::endl;
//return the stream. cout print the stream result.
return out;
}
Only you also have at first to declare the operator in the same namespace where the class is defined.
If you want that the operator would be declared in the global namespace then you can do it the following way
namespace biumath
{
class Assignment;
}
std::ostream& operator<< ( std::ostream &,
const biumath::Assignment & );
namespace biumath
{
class Assignment
{
//...
friend std::ostream& ::operator<< (std::ostream& out,
const Assignment& assignment);
};
}
You've befriended an operator in your biumath namespace, but you don't define that; instead you define a separate operator in the global namespace.
Re-open the namespace in the source file, and put the definition inside it.

Can ostream overloading be a function member?

I have a class Counter and I want to overload operator << to output the data member of Counter. I tried to make the ostream overloading a member function:
Counter{
public:
std::ostream& operator<<(std::ostream& outStream, const Counter& c);
private:
int count_;
};
std::ostream& Counter::operator<<(std::ostream& outStream, const Counter& c){
outStream << c.count_;
return outStream;
}
But the g++ compiler always outputs the same error:
‘std::ostream& Counter::operator<<(std::ostream&, const Counter&)’ must take exactly one argument
However, if I changed the overloading function to be a friend of the class, it worked all well, like this:
Counter{
public:
friend std::ostream& operator<<(std::ostream& outStream, const Counter& c);
private:
int count_;
};
std::ostream& operator<<(std::ostream& outStream, const Counter& c){
outStream << c.count_;
return outStream;
}
Does this mean that the the stream operator overloading cannot be a member function of a class?
Add a public query method that returns the value of count_, then it does not have to be a friend:
Counter{
public:
int count() const { return count_; }
private:
int count_;
};
std::ostream& operator<<(std::ostream& outStream, const Counter& c){
outStream << c.count();
return outStream;
}
If you put the ostream operator in the class itself then it will not work the way you expect it to. It would be a member function meaning to invoke it one would have to do this: c.operator<<("output") which is obviously not what you mean to do. For it to work as you expect an ostream operator it must be outside the class. You can do this by making it a friend or just put it outside of the class and use getters (accessors) to output the data.
It doesn't have to be a friend, but it can't be a member. Member operators only work when they are inside the class which corresponds to the left-hand operand.
Unfortunately the useful overloads for the streaming output operators ( << ) cannot be class members, because the ostream& must be on the left in use and declaration. They do not need to be friends of the class you wish to stream unless they need access to protected or private members. This means that if you can implement a streaming operator using just public functions such as observers/accessors without declaring it a friend.
In your first Counter class you are declaring a member function of the class that does not seem valid. In the second example of the Counter class you are stating that your operator overload for << , which seems valid, has access to the private members. In the second example the function must still be declared outside the class.
Wikipedia Operators in C and C++ has a good list of possible operator overloads, including the in class << overloads even though they are not very useful. The in class overloads must be called backwards CounterInstance << cout; which is counterintuitive.

Enabling Classes for Use with boost::lexical_cast

Code snippet from lexical_cast:
class lexical_castable {
public:
lexical_castable() {};
lexical_castable(const std::string s) : s_(s) {};
friend std::ostream operator<<
(std::ostream& o, const lexical_castable& le);
friend std::istream operator>>
(std::istream& i, lexical_castable& le);
private:
virtual void print_(std::ostream& o) const {
o << s_ <<"\n";
}
virtual void read_(std::istream& i) const {
i >> s_;
}
std::string s_;
};
std::ostream operator<<(std::ostream& o,
const lexical_castable& le) {
le.print_(o);
return o;
}
std::istream operator>>(std::istream& i, lexical_castable& le) {
le.read_(i);
return i;
}
Based on document,
template<typename Target, typename Source>
Target lexical_cast(const Source& arg);
1> Returns the result of streaming arg into a standard library
string-based stream and then out as a Target object.
2> Source is OutputStreamable
3> Target is InputStreamable
Question1> For User Defined Type (UDT), should the OutputStreamable or InputStreamable always have to deal with std::string? For example, given a class containing a simple integer as member variable, when we define the operator<< and operator>>, what is the implementation code looks like? Do I have to convert the integer as a string? Based on my understanding, it seems that UDT always has to deal with std::string in order to work with boost::lexical_cast and boost::lexcial_cast needs the intermediate std::string to do the real conversion jobs.
Question2> Why the return value of operator<< or operator>> in above code is not reference to std::ostream& or std::istream& respectively?
To make your class usable with lexical_cast, just define the "stream" operators for it.
From Boost.LexicalCast Synopsis:
Source is OutputStreamable, meaning that an operator<< is defined that takes a std::ostream or std::wostream object on the left hand side and an instance of the argument type on the right.
Target is InputStreamable, meaning that an operator>> is defined that takes a std::istream or std::wistream object on the left hand side and an instance of the result type on the right.
Target is CopyConstructible [20.1.3].
Target is DefaultConstructible, meaning that it is possible to default-initialize an object of that type [8.5, 20.1.4].
:
// either inline friend, out-of-class friend, or just normal free function
// depending on whether it needs to access internel members
// or can cope with the public interface
// (use only one version)
class MyClass{
int _i;
public:
// inline version
friend std::ostream& operator<<(std::ostream& os, MyClass const& ms){
return os << ms._i;
}
// or out-of-class friend (friend declaration inside class only)
friend std::ostream& operator<<(std::ostream& os, MyClass const& ms);
// for the free function version
int get_i() const{ return _i; }
};
// out-of-class continued
std::ostream& operator<<(std::ostream& os, MyClass const& ms){
return os << ms._i;
}
// free function, non-friend
std::ostream& operator<<(std::ostream& os, MyClass const& ms){
return os << ms.get_i();
}
The same of course for operator>>.