0
//A.h
class A{
friend bool compareEntry_sumPct_nonMega(Entry arg1, Entry arg2);
}

//A.cpp
#include "A.h"
bool A::compareEntry_sumPct_nonMega(Entry arg1, Entry arg2)
{
    bool b = arg1.sumOfNonMegaEntryPct(numbers) < arg2.sumOfNonMegaEntryPct(numbers);
    return b;
}

I get the error compareEntry_sum_Pct_nonMega is not a member of A.

Tommy Saechao
  • 1,099
  • 4
  • 17
  • 28
  • 1
    Is compareEntry_sumPct_nonMega inside of a class declaration? If not, then it's not part of class A – kcraigie Oct 31 '15 at 05:08
  • May help you http://geeksquiz.com/friend-class-function-cpp/ – cm161 Oct 31 '15 at 05:21
  • @cm161 that shows how to create a friend function inside one file, I want to declare the friend function in my header file and define it in my .cpp file, which includes my header. – Tommy Saechao Oct 31 '15 at 05:23
  • A friend function is a friend, not a member, so you don't define it with the `A::` qualifier that means a member. – Jonathan Wakely Oct 31 '15 at 08:54
  • You need to ask better questions with complete code examples. This question shows an incomplete snippet of code, e.g. you have not shown the `numbers` member of `A`, and so the question should be closed as off-topic, or you should edit it to improve it. – Jonathan Wakely Oct 31 '15 at 08:57
  • @Jonathan The complete code is 10 source files, it would be too long and tedious to go through all of them to answer a simple question. – Tommy Saechao Oct 31 '15 at 18:56
  • 1
    @TommySaechao, poor excuse, you need to read http://stackoverflow.com/help/mcve and http://kera.name/articles/2013/10/nobody-writes-testcases-any-more/ and learn to ask better questions. – Jonathan Wakely Nov 04 '15 at 12:11

3 Answers3

3
bool A::compareEntry_sumPct_nonMega(Entry arg1, Entry arg2)

A friend function of a class is not its member function. So, A:: is telling the compiler that it should treat this function as a member function but it's clearly not a member function.

so, take out A:: ie scope resolution.

A function cannot be both member function of a class T and friend function of T. If it is already a member function, then, what's the point in making it friend of that class.

If you were intending to use one class function as a friend of another class, then you can use it as below:

class B
{
public:
    void fB(A& a); 
    void fB2(A& a); 
};

class A
{
public:
    friend void B::fB(A& a);
    void fA(){}
};
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
basav
  • 1,475
  • 12
  • 20
1

Since you have defined "friend bool compareEntry_sumPct_nonMega(Entry arg1, Entry arg2);" as a friend function, you are telling the compiler that the function compareEnty_sumPct_nonMega is a non-member function which should have access to private methods and variables of the class "A". But when you define the function in A.cpp, you are defining it as a member function of class "A" since your definition has "bool **A::**compareEntry_sumPct_nonMega(Entry arg1, Entry arg2)". Remove the A:: in A.cpp and try compiling.

Change your A.cpp to -

//A.cpp
#include "A.h"
bool compareEntry_sumPct_nonMega(Entry arg1, Entry arg2)
{
    bool b = arg1.sumOfNonMegaEntryPct(numbers) < arg2.sumOfNonMegaEntryPct(numbers);
    return b;
}
buchipper
  • 606
  • 4
  • 16
  • When I take out the **A::** I no longer have access to **numbers** member because **numbers** is a member of type class **A** My goal here is to declare the friend function in the .h file and then define it in the .cpp file. – Tommy Saechao Oct 31 '15 at 07:48
  • @TommySaechao, to access the `numbers` member variable it needs to be a member function ... so why are you defining it as a friend? A friend is not a member. If you want a member function there is a simple solution: use a member function. – Jonathan Wakely Oct 31 '15 at 08:55
1
//A.h
class A{
friend bool compareEntry_sumPct_nonMega(A& obj, Entry arg1, Entry arg2); <-- CHANGE done here
}

//A.cpp
#include "A.h"
bool compareEntry_sumPct_nonMega(A& obj, Entry arg1, Entry arg2) <-- CHANGE done here
{
    // Using 'obj', all members (private as well) of class A can be accessed <-- CHANGE done here
    bool b = arg1.sumOfNonMegaEntryPct(numbers) < arg2.sumOfNonMegaEntryPct(numbers);
    return b;
}
cm161
  • 492
  • 3
  • 6