Implementierung einer friend Methode für ein e private Subklasse - warum geht das nicht so?
Von: Robert Hartmann (robert_hartmann@gmx.net) [Profil]
Datum: 07.10.2009 09:37
Message-ID: <4ACC4535.7000404@gmx.net>
Newsgroup: de.comp.lang.iso-c++
Datum: 07.10.2009 09:37
Message-ID: <4ACC4535.7000404@gmx.net>
Newsgroup: de.comp.lang.iso-c++
Hallo zusammen,
Folgendes Konstrukt macht mir Probleme, sobald ich die
Implementierung der friend Methode aus der H-Datei in die CPP-Datei
verlagere. Wenn die Implementierung der friend Methode
in der Headerdatei jedoch verbleibt kompiliert der vollständige
Code ohne Fehler und Warnungen im g++ (mingw).
Folgende Fehlermeldung erhalte ich,
wenn ich die Implementierung statt direkt in der Headerdatei
in einer cpp-Datei vornehme:
g++ -DDEBUG -DWIN32 -D_DEBUG -URELEASE -O0 -g3 -p -pg -Wall -c
-fmessage-length=0 -oBitfield.o ..\Bitfield.cpp
..\/Bitfield.h: In function `std::ostream& operator<<(std::ostream&,
const Bitfield::BitReference&)':
..\/Bitfield.h:77: error: `class Bitfield::BitReference' is private
..\Bitfield.cpp:83: error: within this context
Build error occurred, build is stopped
Time consumed: 906 ms.
So geht es: (für wie es nicht geht siehe weiter unten)
/**************************************/
class Bitfield{
private:
class BitReference{
private:
Bitfield &bf;
size_t index;
public:
BitReference(Bitfield& BF, size_t IDX);
friend std::ostream& operator<<(std::ostream& s, const
Bitfield::BitReference& bRef){
if (bRef.bf.getBit(bRef.index))
s << "1";
else
s << "0";
return s;
};
//[....]
};
//[....]
public:
Bitfield::BitReference operator[](const int x);
Bitfield::BitReference operator[](const size_t x);
bool operator[](const int x) const;
bool operator[](const size_t x) const;
//[....]
};
/**************************************/
Warum aber geht das so nicht?
/**************************************/
//bitfield.h
class Bitfield{
private:
class BitReference{
private:
Bitfield &bf;
size_t index;
public:
BitReference(Bitfield& BF, size_t IDX);
friend std::ostream& operator<<(std::ostream& s, const
Bitfield::BitReference& bRef);
//[....]
};
//[....]
public:
Bitfield::BitReference operator[](const int x);
Bitfield::BitReference operator[](const size_t x);
bool operator[](const int x) const;
bool operator[](const size_t x) const;
//[....]
};
/**************************************/
//bitfield.cpp
std::ostream& operator<<(std::ostream& s, const Bitfield::BitReference&
bRef) {
if (bRef.bf.getBit(bRef.index))
s << "1";
else
s << "0";
return s;
}
/**************************************/
[ Auf dieses Posting antworten ]Antworten
- Markus Schaaf (07.10.2009 15:05)
- Olaf Krzikalla (07.10.2009 16:24)
- Daniel_Krügler (07.10.2009 15:32)
- Robert Hartmann (08.10.2009 10:26)
