I use three different files to define a templated class. The class declaration is in a .h
file, the implementation in a .cpp
file and explicit instantiations are included in a .inc
file.
I'm trying to define a friend function that it's able to access private data members of a templated class.
As in the case of templated classes, the function will be defined, implemented, and instantiated in 3 separate files.
When I try the call the function, I get the following error messages:
myclass.h:error: ‘doSomething’ is neither function nor member function; cannot be declared friend
myclass.h:error: expected ‘;’ before ‘<’ token
mymethod.h: error: ‘friend’ used outside of class
Does anybody has any suggestion on how to solve this issue? I tried to simplify the code below.
myclass.h
template<class T>
class MyClass{
friend T doSomething<T>( MyClass<T> *, T, int);
friend MyClass<T> * doSomethingElse<T>( const MyClass<T> *, const MyClass<T> *);
public:
...
private:
T *m_pMyMatrix;
};
mymethod.h
#include <myclass.h>
template <class T> friend T doSomething( MyClass<T> *, T, int);
template <class T> MyClass<T>* doSomethingElse(const MyClass<T>*, const MyClass<T>*);
mymethod.cpp
#include <mymethod.h>
template <class T>
T doSomething( MyClass<T> * pData, T val, int index){
// the actual code does sth. more complex than the code below.
pData->m_pMyMatrix[index]+=val;
return pData->m_pMyMatrix[index];
}
template <class T>
MyClass<T>* doSomethingElse(const MyClass<T> * pData1, const MyClass<T> * pData2){
...
T res1 = doSomething(pData1, val1, index1);
...
}
#include "mymethod-impl.inc"
mymethod-impl.inc
template float doSomethi开发者_Go百科ng( MyClass<float> *, float, int);
template double doSomething( MyClass<double> *, double, int);
template MyClass<float>* doSomethingElse(const MyClass<float>*, const MyClass<float>*);
template MyClass<double>* doSomethingElse(const MyClass<double>*, const MyClass<double> *);
I think this should worK
template<class T>
class MyClass;
template <class T>
T doSomething( const MyClass<T> *, T, int);
template<class T>
class MyClass {
friend T doSomething<T>( const MyClass<T> *, T, int);
public:
...
private:
T *m_pMyMatrix;
};
That is, you need to declare the template before you can make it (or an instance thereof) a friend.
I think I solved this:
mymethod.h
template<class T> class MyClass;
template<class T>
T doSomething<T>(const MyClass<T>* pData, T val, int index);
myclass.h
#include "mymethod.h"
template<class T>
class MyClass {
friend T doSomething<T>(const MyClass<T>* pData, T val, int index);
public:
// ...
private:
T *m_pMyMatrix;
};
mymethod.cpp
#include "myclass.h"
template<class T>
T doSomething(const MyClass<T>* pData, T val, int index)
{
pData->m_pMyMatrix[index]+=val;
return pData->m_pMyMatrix[index];
}
template<> float doSomething( const MyClass<float> *, float, int);
template<> double doSomething( const MyClass<double> *, double, int);
In practice, only thing you need is to declare the template function before MyClass definition.
精彩评论