在模板类中定义友元函数

发布于 2024-10-04 12:58:52 字数 1956 浏览 3 评论 0原文

我使用三个不同的文件来定义模板类。类声明位于 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与往事干杯 2024-10-11 12:58:52

我认为这应该可行

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;
};

,也就是说,您需要先声明模板,然后才能将其(或其实例)设为友元。

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.

白云悠悠 2024-10-11 12:58:52

我想我解决了这个问题:

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< /strong>

#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);

实际上,您只需在 MyClass 定义之前声明模板函数即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文