I have a header file with a struct called Vector4.
我有一个头文件,有一个叫做Vector4的struct。
(vector4.h)
(vector4.h)
struct Vector4
{
float Values[4];
// ..... methods .....
};
I have a header file with a struct called Matrix4.
我有一个头文件,它有一个名为Matrix4的结构。
(matrix4.h)
(matrix4.h)
struct Matrix4
{
float Values[16];
// ..... methods .....
};
I want to add a member function to them both, say multiply() which takes the other struct as a parameter.
我想要给它们添加一个成员函数,比如乘法()将另一个struct作为参数。
I can easily add matrix4.h to vector4.h and add the method multiply().
我可以很容易地加上矩阵。vector4 h。并添加方法乘以()。
If I then try and add vector4.h to matrix4.h I (obviously) will get a circular reference.
如果我尝试加向量4。matrix4 h。h I(显然)将得到一个循环引用。
How can I get around this please, if at all?
我怎么才能避开这个麻烦呢?
Edit:
编辑:
@MikeSeymour, I have written what I assumed you meant, but it doesn't work.
@MikeSeymour,我已经写了我认为你的意思,但它不起作用。
bar.h
bar.h
#ifndef BAR_H
#define BAR_H
template <typename T>
struct Foo;
template <typename T>
struct Bar
{
T t;
T multiply(const Foo<T>& foo) const;
};
#endif // BAR_H
foo.h
foo。
#ifndef FOO_H
#define FOO_H
template <typename T>
struct Bar;
template <typename T>
struct Foo
{
T t;
T multiply(const Bar<T>& bar) const;
};
#endif // FOO_H
bar.cpp
bar.cpp
#include "foo.h"
#include "bar.h"
Bar<T>::multiply(const Foo<T>& foo) const
{
return t * foo.t;
}
foo.cpp
foo.cpp
#include "foo.h"
#include "bar.h"
Foo<T>::multiply(const Bar<T>& bar) const
{
return t * bar.t;
}
1 个解决方案
#1
1
You'll need to declare each class before the class that needs to refer to it:
您需要在需要引用的类之前声明每个类:
// vector4.h
struct Matrix4;
struct Vector4
{
// ...
void multiply(Matrix4);
};
// matrix4.h
struct Vector4;
struct Matrix4
{
// ...
void multiply(Vector4);
};
The function definitions, which need full definitions of both classes, will have to be outside the class definitions.
函数定义(需要两个类的完整定义)必须在类定义之外。
If the classes are actually templates, then those function definitions will have to be in headers, structured so that they appear after both class definitions. One approach is to include each header from the other, after that header's definition:
如果类实际上是模板,那么这些函数定义将必须位于header中,以便在两个类定义之后出现。一种方法是将每个头从另一个中包含进来,在该头的定义之后:
#ifndef BAR_H
#define BAR_H
template <typename T>
struct Foo;
template <typename T>
struct Bar
{
T t;
T multiply(const Foo<T>& foo) const;
};
#include "foo.h"
template <typename T>
T Bar<T>::multiply(const Foo<T>& foo) const
{
return t * foo.t;
}
#endif // BAR_H
Or you might define the functions in separate headers; they'll only be available when you include those headers
或者你可以在单独的标题中定义函数;只有在包含这些头信息时,它们才可用。
// bar_multiply.h
#include "foo.h"
#include "bar.h"
template <typename T>
T Bar<T>::multiply(const Foo<T>& foo) const
{
return t * foo.t;
}
Or, since the classes are so tightly coupled, you might consider defining both in the same header.
或者,由于类是紧密耦合的,所以您可以考虑在同一个标题中定义它们。
#1
1
You'll need to declare each class before the class that needs to refer to it:
您需要在需要引用的类之前声明每个类:
// vector4.h
struct Matrix4;
struct Vector4
{
// ...
void multiply(Matrix4);
};
// matrix4.h
struct Vector4;
struct Matrix4
{
// ...
void multiply(Vector4);
};
The function definitions, which need full definitions of both classes, will have to be outside the class definitions.
函数定义(需要两个类的完整定义)必须在类定义之外。
If the classes are actually templates, then those function definitions will have to be in headers, structured so that they appear after both class definitions. One approach is to include each header from the other, after that header's definition:
如果类实际上是模板,那么这些函数定义将必须位于header中,以便在两个类定义之后出现。一种方法是将每个头从另一个中包含进来,在该头的定义之后:
#ifndef BAR_H
#define BAR_H
template <typename T>
struct Foo;
template <typename T>
struct Bar
{
T t;
T multiply(const Foo<T>& foo) const;
};
#include "foo.h"
template <typename T>
T Bar<T>::multiply(const Foo<T>& foo) const
{
return t * foo.t;
}
#endif // BAR_H
Or you might define the functions in separate headers; they'll only be available when you include those headers
或者你可以在单独的标题中定义函数;只有在包含这些头信息时,它们才可用。
// bar_multiply.h
#include "foo.h"
#include "bar.h"
template <typename T>
T Bar<T>::multiply(const Foo<T>& foo) const
{
return t * foo.t;
}
Or, since the classes are so tightly coupled, you might consider defining both in the same header.
或者,由于类是紧密耦合的,所以您可以考虑在同一个标题中定义它们。