c ++模板函数未解析的外部符号 [英] c++ template function unresolved external symbol

查看:91
本文介绍了c ++模板函数未解析的外部符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是为什么我的代码无效。我把它改成了命名空间下的一个类,它没有解析模板化的函数。



这里是FastMemoryAllocation.cpp

Hi, I am not sre why my code is not working. I changed it into a class under a namespace and it does not resolve the templated fuunctions.

Here is FastMemoryAllocation.cpp

#include "FastMemoryAllocation.h"

namespace TSD
{
	FastMemoryAllocation::FastMemoryAllocation()
	{
	}

	FastMemoryAllocation::FastMemoryAllocation(DWORD Size) : Size(Size)
	{
	}

	template<class T> forceinline T FastMemoryAllocation::DataAs()
	{
		return (T)(Start + sizeof(FastMemoryAllocation));
	}

	template<class T> forceinline T FastMemoryAllocation::DataAs(int Offset)
	{
		return (T)(Start + sizeof(FastMemoryAllocation) + Offset);
	}

	forceinline BYTE* FastMemoryAllocation::DataAsLPByte()
	{
		return DataAs<BYTE*>();
	}

	forceinline BYTE* FastMemoryAllocation::DataAsLPByte(int Offset)
	{
		return DataAs<BYTE*>(Offset);
	}

	template<class T> forceinline T FastMemoryAllocation::DataFromEnd()
	{
		return (T)(Start + sizeof(FastMemoryAllocation) + Size - 1);
	}

	template<class T> forceinline T FastMemoryAllocation::DataFromEnd(int Offset)
	{
		return (T)(Start + sizeof(FastMemoryAllocation) + Size - 1 - Offset);
	}

	forceinline BYTE* FastMemoryAllocation::DataFromEndLPByte()
	{
		return DataFromEnd<BYTE*>();
	}

	forceinline BYTE* FastMemoryAllocation::DataFromEndLPByte(int Offset)
	{
		return DataFromEnd<BYTE*>(Offset);
	}
}





这是FastMemoryAllocation.h



Here is FastMemoryAllocation.h

#pragma once

#ifndef _FastMemoryAllocation_
#define _FastMemoryAllocation_

#include "forceinline.h"

namespace TSD
{
	class FastMemoryAllocation
	{
	public:
		ULONG Size;
		BYTE* Start;
		ULONG ActualSize;
		FastMemoryAllocation* Next;
		BYTE  AllocatedFor;
		BYTE  Internal_NeverRemove[3 + DataTypeSize];

	public:
		FastMemoryAllocation();
		FastMemoryAllocation(DWORD Size);
		template<class T> forceinline T DataAs();
		template<class T> forceinline T DataAs(int Offset);
		forceinline BYTE* DataAsLPByte();
		forceinline BYTE* DataAsLPByte(int Offset);
		template<class T> forceinline T DataFromEnd();
		template<class T> forceinline T DataFromEnd(int Offset);
		forceinline BYTE* DataFromEndLPByte();
		forceinline BYTE* DataFromEndLPByte(int Offset);
	};
}

#endif





这里是forceinline.h



And here is forceinline.h

#pragma once

#ifndef _forceinline_
#define _forceinline_

#include <Windows.h>

#ifndef forceinline
#define forceinline inline
#endif

#ifdef  _WIN64
#define FastMemAlignSize 16
#define DataType __int64
#define DataTypeSize sizeof(__int64)
#define DataTypeBits 16
#define offsetof(s,m)   (size_t)( (ptrdiff_t)&reinterpret_cast<const volatile char&>((((s *)0)->m)) )
#else
#define FastMemAlignSize 8
#define DataType DWORD
#define DataTypeSize sizeof(DWORD)
#define DataTypeBits 8
#define offsetof(s,m)   (size_t)&reinterpret_cast<const volatile char&>((((s *)0)->m))
#endif

#endif





错误在第二行,这里。



The error is on the second line, here.

TSD::FastMemoryAllocation fma;
fma.DataAs<char>();





有没有人知道为什么?



Does anyone have any idea why?

推荐答案

看起来他们的解释相当简单。函数DataAs的定义驻留在.cpp文件中。然而,在您到达报告错误的行之前,编译器需要看到它。



尝试将您的全部内容放在FastMemoryAllocation.cpp中将文件放入FastMemoryAllocation.h文件中。然后它应该工作。
Look like their is a rather simple explanation. The definition of the function DataAs resides in a .cpp file. It needs however to be seen by the compiler before you arrive at the line at which the error is reported.

Try to put that entire stuff you have in FastMemoryAllocation.cpp file into the FastMemoryAllocation.h file instead. Then it should work.


大量的谷歌搜索和测试,我终于解决了它。



对于我所有的.cpp文件我创建我去了属性,然后从构建中排除。



然后我添加了所有#includexxx.h头文件

然后我添加了所有#includexxx.cpp代码文件

进入我的主程序。



就像把它们写在一个大的文件,但仍然模块化。



有点痛苦。

不是我想要的,但我现在可以继续了。



如果有人有其他解决方案,我感兴趣。
Lots of googling and testing and I finally solved it.

For all my .cpp files I created I went to properties and Excluded then from the build.

I then added all my #include "xxx.h" header files
Then I added all my #include "xxx.cpp" code files
Into my main program.

Just like writting them in one large file, but still modula enough.

Bit of a pain.
Not what I was after but I can now continue.

I am interested if anyone has another solution.


这篇关于c ++模板函数未解析的外部符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆