Simplification of the code to express the concept:
class classA
{
public:
private:
int a;
seta(int x);
};
//local prototype
void somefunction();
int main()
{
classA object; //create an object of the class
somefunction(object);
return 0;
}
void somefunction(classA &object)
{
object.seta(5);
}
}
If I move seta() to the public section of the class there is no error and it executes.
However, if I move seta() to the private section, I get the following error:
error C2248:
‘anonymous-namespace'::classA::seta' : cannot accessanonymous-namespace’::classA’
private member declared in class '
If the function is private and only this class is calling it, why is there an issue?
EDIT I am passing the object from main to the local function
The function that is calling
seta()is not part ofclassA, so this is an error; that’s the very definition ofprivate. If you were to makesomefunction()a member ofclassA, or declare it to be afriendofclassA, then this would work.