5

I am getting the following error when I try to access bins private member of the GHistogram class from within the extractHistogram() implementation:

error: 'QVector<double> MyNamespace::GHistogram::bins' is private
error: within this context

Where the 'within this context' error points to the extractHistogram() implementation. Does anyone knows what's wrong with my friend function declaration?

Here's the code:

namespace MyNamespace{

class GHistogram
{

public:
    GHistogram(qint32 numberOfBins);
    qint32 getNumberOfBins();

    /**
     * Returns the frequency of the value i.
     */
    double getValueAt(qint32 i);
    friend GHistogram * MyNamespace::extractHistogram(GImage *image, 
                                                      qint32 numberOfBins);

private:
    QVector<double> bins;
};

GHistogram * extractHistogram(GImage * image, 
                              qint32 numberOfBins);

} // End of MyNamespace
Alceu Costa
  • 9,733
  • 19
  • 65
  • 83

4 Answers4

6

According to my GCC the above code does not compile because the declaration of extractHistogram() appears after the class definition in which it is friended. The compiler chokes on the friend statement, saying that extractHistogram is neither a function nor a data member. All works well and bins is accessible when I move the declaration to before the class definition (and add a forward declaration class GHistogram; so that the return type is known to the compiler). Of course the code for extractHistogram() should be written inside the namespace, either by

namesapce MyNameSpace {
// write the function here
}

or

GHistogram *MyNameSpace::extractHistogram( //....
Ari
  • 3,460
  • 3
  • 24
  • 31
  • Thank you! I had tried to declare extractHistogram() before the class declaration, but it didn't work because it was missing the namespace scope in the function implementation -- I tought that I just had to use the 'using namespace' clause... – Alceu Costa Mar 23 '10 at 15:57
2

Try just:

friend GHistogram *extractHistogram(GImage *image, qint32 numberOfBins);
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • Sorry for my mistake. The correct is MyNamespace instead of Gbdi int he code. When I remove the MyNamespace I get the same error. – Alceu Costa Mar 23 '10 at 14:39
1

You declare Gbdi::extractHistogram to be a friend to GHistogram, but you declare a function named extractHistogram and expect it to friend with GHistogram. extractHistogram should be a member of Gbdi.

Randolpho
  • 55,384
  • 17
  • 145
  • 179
1

I guess you meant:

 friend GHistogram * extractHistogram(GImage *image, qint32 numberOfBins);

This is probably not the reason but a suggestion:

One more thing, you don't need to declare a function out of a class if it is such class's friend and you already declared it within the class.

In other words:

//Your.h
class Foo
{
friend void m();
}
void m(); //This is totally unnecessary

//Your.cpp
void m()
{
}
Anzurio
  • 16,780
  • 3
  • 39
  • 49
  • But if I do that, when I try to invoke m() from, say int main() in 'main.cpp' I will get an error that tells me that 'm() was not declared in this scope' even if I include the 'Your.h' in my 'main.cpp'. – Alceu Costa Mar 23 '10 at 14:47
  • You're totally right. It slipped my mind because I normally don't do that. I will deleting that. Sorry. – Anzurio Mar 23 '10 at 14:48