[英]How to organize classes containing mixture of templates and friends?
I have two C++ classes such that: 我有两个这样的C ++类:
Given that, my question is: how do I organize the sources/headers/forward declarations for this situation? 鉴于此,我的问题是:如何组织这种情况的来源/标题/转发声明? Whatever I tried, it just doesn't compile to an object file.
无论我尝试什么,它都不会编译为目标文件。
One sequence is this: 一个序列是这样的:
class Class2;
class Class1
{
Class2 * c2;
public:
template<typename T> T DoSomething(T& X)
{
c2->Func();
return X;
};
void FuncFromClass1();
};
class Class2
{
int data;
public:
Class2() : data(0) {};
void Func();
friend void Class1::FuncFromClass1();
};
void Class2::Func()
{
int i;
}
void Class1::FuncFromClass1()
{
int j;
c2 = new Class2;
c2->data = 1;
}
Barks invalid use of incomplete type 'class Class2'
because it doesn't recognize c2->Func();
禁止
invalid use of incomplete type 'class Class2'
因为它无法识别c2->Func();
. 。 The other one is:
另一个是:
class Class1;
class Class2
{
int data;
public:
Class2() : data(0) {};
void Func();
friend void Class1::FuncFromClass1();
};
class Class1
{
Class2 * c2;
public:
template<typename T> T DoSomething(T& X)
{
c2->Func();
return X;
};
void FuncFromClass1();
};
void Class2::Func()
{
int i;
}
void Class1::FuncFromClass1()
{
int j;
c2 = new Class2;
c2->data = 1;
}
Doesn't recognize friend void Class1::FuncFromClass1();
无法识别
friend void Class1::FuncFromClass1();
. 。
The compilation is tried as g++ -c -std=c++11 -Wall test.cpp
. 尝试将其编译为
g++ -c -std=c++11 -Wall test.cpp
。
Note I'd rather not make Class1
as entire friend, rather want to keep only one of its methods as a friend to Class2
, if at all possible. 注意,我不希望让
Class1
成为整个朋友,而是尽可能地仅将其方法之一作为Class2
的朋友。
Also, I haven't tried the exact same example in Visual Studio in Windows, but saw an entirely isomorphic situation like the one described (within a bigger project) and no complaints came from VS as far as I recall. 另外,我还没有在Windows的Visual Studio中尝试过完全相同的示例,但是看到了一个完全同构的情况,如所描述的(在一个更大的项目中),据我所知,没有来自VS的抱怨。 Is it unique to g++?
它是g ++独有的吗?
Move the implementation of the member function template where definition of Class2
is known. 将成员函数模板的实现
Class2
已知Class2
定义的位置。
class Class2;
class Class1
{
private:
Class2 * c2;
public:
// Delcare, don't define
template<typename T> T DoSomething(T& X);
void FuncFromClass1();
};
class Class2
{
private:
int data;
public:
Class2() : data(0) {};
void Func();
friend void Class1::FuncFromClass1();
};
// Define
template<typename T>
T Class1::DoSomething(T& X)
{
c2->Func();
return X;
};
Note that the proposed solution is simple if both classes are defined in one .h file. 请注意,如果两个类都在一个.h文件中定义,则建议的解决方案很简单。 If the classes are defined in separate .h files, things get a little bit more complex.
如果这些类是在单独的.h文件中定义的,则情况会变得有些复杂。 You'll have to make sure that the .h file where
Class1::DoSomething()
is defined is #include
d in every .cpp file where you want to use Class1::DoSomething()
. 您必须确保在要使用
Class1::DoSomething()
每个.cpp文件中,定义了Class1::DoSomething()
的.h文件是#include
d。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.