在模板类中定义友元函数
我使用三个不同的文件来定义模板类。类声明位于 .h
文件中,实现位于 .cpp
文件中,显式实例化包含在 .inc
文件中。 我试图定义一个友元函数,它能够访问模板类的私有数据成员。 与模板化类的情况一样,函数将在 3 个单独的文件中定义、实现和实例化。 当我尝试调用该函数时,收到以下错误消息:
myclass.h:error: 'doSomething' 既不是函数也不是成员函数;无法声明为朋友
myclass.h:error:预期';'在“<”之前token
mymethod.h:错误:“friend”在课堂外使用
有人对如何解决此问题有任何建议吗?我尝试简化下面的代码。
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 doSomething( 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 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 doSomething( 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> *);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这应该可行
,也就是说,您需要先声明模板,然后才能将其(或其实例)设为友元。
I think this should worK
That is, you need to declare the template before you can make it (or an instance thereof) a friend.
我想我解决了这个问题:
mymethod.h
myclass.h
mymethod.cpp< /strong>
实际上,您只需在 MyClass 定义之前声明模板函数即可。
I think I solved this:
mymethod.h
myclass.h
mymethod.cpp
In practice, only thing you need is to declare the template function before MyClass definition.