CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Main CFD Forum (https://www.cfd-online.com/Forums/main/)
-   -   Accessing a private member from a different namespace and class to a template class (https://www.cfd-online.com/Forums/main/225709-accessing-private-member-different-namespace-class-template-class.html)

pankajadgjmp April 5, 2020 13:37

Accessing a private member from a different namespace and class to a template class
 
I am trying to access a private member from a class with radiation namespace, to a template class in Foam namespace. I tried replicating a similar (own c++ code ) and simple test code with multiple namespace and classes and was successful. But the moment I want to use that same method in OpenFOAM I get errors.


did anyone came across a similar problem or have a solution please help.



The error says "multiple definition of `Foam::radiation::accessd_(Foam::radiation::DEradi ativeIntensityRay)' "..


Although the error might not make sense without the program but it is something like this




friend int accessd_(DEradiativeIntensityRay obj);
}; // End of class
//DEradiativeIntensityRay user; //object to access d_



int accessd_(DEradiativeIntensityRay obj) // the line of error
{
int private_{0};
private_ = obj.test;
Info<<" The value of private is "<<private_ <<endl;
return private_;
}

t.teschner April 6, 2020 10:36

well, going by your incomplete code snippets it is virtually impossible to diagnose the problem. could you share the code which is working so that it is clear what you are trying to achieve? sometimes the approach may not be the best and there is a better solution available, maybe that is the case here?!

When you want to access private members, you would almost always want to do that through a getter method. I am not sure what is holding you back in this case to use a function of the form

int DEradiativeIntensityRay::get() { return test; }

(maybe you don't want to recompile the OpenFOAM codebase?!), so that information would help as well.

"Friends" don't really work well in an object orientated approach, much like a goto statement (at least in my opinion), but there are always legitimate reasons to do that, so a code example would help here.


All times are GMT -4. The time now is 19:50.