Forum

> > Off Topic > C++/C, anyone?
Forums overviewOff Topic overviewLog in to reply

English C++/C, anyone?

6 replies
To the start Previous 1 Next To the start

old C++/C, anyone?

oxytamine
User Off Offline

Quote
Hello, friend is not getting private members. Can anyone help me? Google does not help at all. Following errors pop out.
error: 'int DG::Packet::_state' is private
error: 'DG::Packet::PacketType DG::Packet::_type' is private


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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);
};
}
	
#include "packet.h"
using namespace DG;
QDataStream& operator<<(QDataStream& stream, const Packet& packet){
    stream << packet._state << packet._type;
    return packet.serialize(stream);
edited 1×, last 26.06.12 10:34:36 am

old Re: C++/C, anyone?

Flacko
User Off Offline

Quote
You don't need to declare public members on C++ structs. They all are public by default.
I can't see the prototype for Packet::serialize. Make sure it's declared using the const keyword. Otherwise, a call to that function through a const instance should throw a compiler error.

I'm guessing you're using the friend keyword the wrong way? It's supposed to grant access to the class's protected members to the functions you listed there. private members are restricted to their class so QDataStream::operator<< and operator>> can't access Packet::_type and _state.

old Re: C++/C, anyone?

oxytamine
User Off Offline

Quote
user Flacko has written
You don't need to declare public members on C++ structs. They all are public by default.
I can't see the prototype for Packet::serialize. Make sure it's declared using the const keyword. Otherwise, a call to that function through a const instance should throw a compiler error.

Thanks for the information. Yes, everything's declared.
user Flacko has written
I'm guessing you're using the friend keyword the wrong way? It's supposed to grant access to the class's protected members to the functions you listed there. private members are restricted to their class so QDataStream::operator<< and operator>> can't access Packet::_type and _state.

That was the question - friend cannot access private members, what do?

old Re: C++/C, anyone?

ohaz
User Off Offline

Quote
Well, actually, you declared both _type and _state as private.

old Re: C++/C, anyone?

oxytamine
User Off Offline

Quote
Well because they're supposed to be private (in my opinion, of course, I can be wrong), the only problem I have is that friend is also supposed to get the private members, but it cannot.

old Re: C++/C, anyone?

Flacko
User Off Offline

Quote
Declare them as protected instead
Private members can't and shouldn't be accessed outside the class they belong to.

old Re: C++/C, anyone?

oxytamine
User Off Offline

Quote
user Flacko has written
Declare them as protected instead

Woohoo, that worked! Thank you!
user Flacko has written
Private members can't and shouldn't be accessed outside the class they belong to.

Thanks, I did not know that.
To the start Previous 1 Next To the start
Log in to replyOff Topic overviewForums overview