C++ error: multiple definition of a member function specialized in template class, but I really defined it only once
C++ error: multiple definition of a member function specialized in template class, but I really defined it only once
问题来自一个计算机图形 C++ 项目,我想在其中计算比例场和 3D 向量场的梯度。我们知道它们的梯度是不同的:scale field 有 3D-vector 梯度,而 3D-vector field 有 3x3 matrix gradient。由于所有其他代码都相同,因此我使用模板来重用代码。但是我在专门化成员函数时遇到了一个问题,它有不同的代码来计算不同数据类型的梯度。最小化代码如下:
//======== Main.cpp ========
#include "Render.h"
int main() {}
//======== Render.cpp ========
#include "Render.h"
//======== Render.h ========
#ifndef __RENDER_H__
#define __RENDER_H__
#include "VolumeGrid.h"
#endif
//======== VolumeGrid.h ========
#ifndef __VOLUMEGRID_H__
#define __VOLUMEGRID_H__
#include "Volume.h"
template < typename U >
class _Grid {
public:
const typename GradType<U>::GType grad(const Vector& x) const;
U * values = nullptr;
};
template <>
const Vector _Grid<float>::grad(const Vector& x) const {
return Vector();
}
template <>
const Matrix _Grid<Vector>::grad(const Vector& x) const {
return Matrix();
}
#endif
//======== Volumn.h ========
#ifndef __VOLUME_H__
#define __VOLUME_H__
#include "Vector.h"
#include "Matrix.h"
template <typename U>
struct GradType {
typedef int GType;
};
template<>
struct GradType<float> {
typedef Vector GType;
};
template<>
struct GradType<Vector> {
typedef Matrix GType;
};
template< typename U >
class Volume {
public:
typedef U volumeDataType;
typedef typename GradType<U>::GType volumeGradType;
};
#endif
//======== Vector.h ========
#ifndef __VECTOR_H__
#define __VECTOR_H__
class Vector {
public:
float xyz[3] = { 0,0,0 };
};
#endif
//======== Matrix ========
#ifndef __MATRIX_H__
#define __MATRIX_H__
class Matrix {
public:
float m[3][3];
};
#endif
错误信息是:
build/Debug/GNU-Linux/Render.o: In function `Vector::Vector()':
/home/CppApplication_1/VolumeGrid.h:19:
multiple definition of `_Grid<float>::grad(Vector const&) const'
build/Debug/GNU-Linux/Main.o:/home/CppApplication_1/VolumeGrid.h:19:
first defined here
build/Debug/GNU-Linux/Render.o: In function
`_Grid<Vector>::grad(Vector const&) const':
/home/CppApplication_1/VolumeGrid.h:24:
multiple definition of `_Grid<Vector>::grad(Vector const&) const'
build/Debug/GNU-Linux/Main.o:/home/CppApplication_1/VolumeGrid.h:24:
first defined here
从代码中可以看出,对应不同数据类型的两个特化grad
函数在VolumeGrid.h中只定义了一次,作为class的成员函数[=13] =] 和 Grid<Vector>
。但是错误消息说它们有多个定义。该代码使用 g++ 4.8.4 编译,并在 ubuntu 14.04 64 位上启用了 C++11(在 Visual Studio 2015 上编译良好)。上面的代码被最小化了,因为删除任何行,例如 Main.cpp 中的 #include "Render.h"
,都会使错误消失。 header 包含结构和class 继承层次不应该改变,因为它们在实际项目中使用。那么你能告诉我特化 grad
函数的问题在哪里以及如何解决吗?非常感谢您的帮助。
显式函数模板特化(没有模板参数)并不像实际模板那样隐式inline
。
要么将定义移动到 *.cpp 文件,要么将它们标记为 inline
。
如果将它们移动到*.cpp 文件中,则应在头文件中声明它们,如
template <>
const Vector _Grid<float>::grad(const Vector& x) const;
问题来自一个计算机图形 C++ 项目,我想在其中计算比例场和 3D 向量场的梯度。我们知道它们的梯度是不同的:scale field 有 3D-vector 梯度,而 3D-vector field 有 3x3 matrix gradient。由于所有其他代码都相同,因此我使用模板来重用代码。但是我在专门化成员函数时遇到了一个问题,它有不同的代码来计算不同数据类型的梯度。最小化代码如下:
//======== Main.cpp ========
#include "Render.h"
int main() {}
//======== Render.cpp ========
#include "Render.h"
//======== Render.h ========
#ifndef __RENDER_H__
#define __RENDER_H__
#include "VolumeGrid.h"
#endif
//======== VolumeGrid.h ========
#ifndef __VOLUMEGRID_H__
#define __VOLUMEGRID_H__
#include "Volume.h"
template < typename U >
class _Grid {
public:
const typename GradType<U>::GType grad(const Vector& x) const;
U * values = nullptr;
};
template <>
const Vector _Grid<float>::grad(const Vector& x) const {
return Vector();
}
template <>
const Matrix _Grid<Vector>::grad(const Vector& x) const {
return Matrix();
}
#endif
//======== Volumn.h ========
#ifndef __VOLUME_H__
#define __VOLUME_H__
#include "Vector.h"
#include "Matrix.h"
template <typename U>
struct GradType {
typedef int GType;
};
template<>
struct GradType<float> {
typedef Vector GType;
};
template<>
struct GradType<Vector> {
typedef Matrix GType;
};
template< typename U >
class Volume {
public:
typedef U volumeDataType;
typedef typename GradType<U>::GType volumeGradType;
};
#endif
//======== Vector.h ========
#ifndef __VECTOR_H__
#define __VECTOR_H__
class Vector {
public:
float xyz[3] = { 0,0,0 };
};
#endif
//======== Matrix ========
#ifndef __MATRIX_H__
#define __MATRIX_H__
class Matrix {
public:
float m[3][3];
};
#endif
错误信息是:
build/Debug/GNU-Linux/Render.o: In function `Vector::Vector()':
/home/CppApplication_1/VolumeGrid.h:19:
multiple definition of `_Grid<float>::grad(Vector const&) const'
build/Debug/GNU-Linux/Main.o:/home/CppApplication_1/VolumeGrid.h:19:
first defined here
build/Debug/GNU-Linux/Render.o: In function
`_Grid<Vector>::grad(Vector const&) const':
/home/CppApplication_1/VolumeGrid.h:24:
multiple definition of `_Grid<Vector>::grad(Vector const&) const'
build/Debug/GNU-Linux/Main.o:/home/CppApplication_1/VolumeGrid.h:24:
first defined here
从代码中可以看出,对应不同数据类型的两个特化grad
函数在VolumeGrid.h中只定义了一次,作为class的成员函数[=13] =] 和 Grid<Vector>
。但是错误消息说它们有多个定义。该代码使用 g++ 4.8.4 编译,并在 ubuntu 14.04 64 位上启用了 C++11(在 Visual Studio 2015 上编译良好)。上面的代码被最小化了,因为删除任何行,例如 Main.cpp 中的 #include "Render.h"
,都会使错误消失。 header 包含结构和class 继承层次不应该改变,因为它们在实际项目中使用。那么你能告诉我特化 grad
函数的问题在哪里以及如何解决吗?非常感谢您的帮助。
显式函数模板特化(没有模板参数)并不像实际模板那样隐式inline
。
要么将定义移动到 *.cpp 文件,要么将它们标记为 inline
。
如果将它们移动到*.cpp 文件中,则应在头文件中声明它们,如
template <>
const Vector _Grid<float>::grad(const Vector& x) const;