Template function problem
Hi,
I'm working on an encryption program that compiled without errors with Visual C++ 6.0. I'm trying to compile the same program with Visual C++ 2005 and I keep getting these 'unresolved external symbol' errors. The functions that are causing the problem have the following prototypes:
template <int Size>
BigInt<Size> PowerMod(const BigInt<Size>& a, long e, const BigInt<Size>& n);
template <int Size>
BigInt<Size> PowerMod(const BigInt<Size>& a,const BigInt<Size>& e,const BigInt<Size>& n);
and are found in the file BigInt.h that also contains the declaration of the template class BigInt. The file BigInt.cpp (not contained in the project)contains the definitions of the PowerMod functions and the class BigInt. I have included this file at the end of the BigInt.h file. The errors occur in the file Encrypt.cpp where the template functions are used. I tried moving the definitions of the template functions to this file, but that didn't help. I've been stuck on this problem for some time now and I can't figure it out.
[1143 byte] By [
NetMaster] at [2007-11-19 22:48:40]

# 1 Re: Template function problem
I've been stuck on this problem for some time now and I can't figure it out.And neither will anyone else figure this out until you show real code and a real example that demonstrates the error. We do not know what context this code is in, what you've really done, etc.
And an English explanation is not enough. Too many posters have wrote essays on what they've tried, but when told to show the actual code, they never did what they claim they had done.
Regards,
Paul McKenzie
# 2 Re: Template function problem
Sorry, I thought I had explained enough for someone to spot a mistake I might have made. Including a lot of code often confuses more than it helps. The PowerMod funtion is used like this in the file Encrypt.cpp:
#include "StdAfx.h"
#include <time.h>
#include <string.h>
#include <ctype.h>
#include "BigInt.h"
#define INT_SIZE 35
#define MAX_ENCEXP_SIZE 32
#define MIN_ENCEXP_SIZE 16
static const BigInt<INT_SIZE> PrimeP = HexStringToBigInt<INT_SIZE>("154594281F272B8915820973631620D6376E6D3F02CA68B4C3C16AF11BEB7113");
static const BigInt<INT_SIZE> PrimeQ = HexStringToBigInt<INT_SIZE>("7B7F7CF1842463909414C5AD27FA005DF683A10AC5E7309992494CBE52E90D097");
static const BigInt<INT_SIZE> Phi = (PrimeP - 1)*(PrimeQ - 1);
static const BigInt<INT_SIZE> RSAModulus = PrimeP*PrimeQ;
static BigInt<INT_SIZE> EncExp = StringToBigInt("QWERTY");
static BigInt<INT_SIZE> DecExp = InvMod(EncExp, Phi);
void SetEncExp()
{
time_t Time;
BigInt<INT_SIZE> Tmp,Mask,Gcd;
Time = time(0);
Tmp = Time;
Mask = 1;
Mask = (Mask << MAX_ENCEXP_SIZE) - 1;
Tmp = PowerMod(Tmp, Time, Prime) & Mask;
Gcd = GCD(Tmp, Phi);
while (Gcd > 1)
{
Tmp /= Gcd;
Gcd = GCD(Tmp, Phi);
}
EncExp = Tmp;
DecExp = InvMod(EncExp, Phi);
}
bool Encrypt (
const char PassWord[],
char CipherText[],
size_t Size)
{
BigInt<INT_SIZE> PlainTextValue;
PlainTextValue = StringToBigInt(PassWord);
if (PlainTextValue >= RSAModulus)
return false;
else {
BigInt<INT_SIZE> CipherTextValue;
CipherTextValue = PowerMod(PlainTextValue, EncExp, RSAModulus);
BigIntToString(CipherTextValue, CipherText, Size);
MapChar(CipherText);
return true;
}
}
The error messages look like this:
error LNK2019: unresolved external symbol "class BigInt<35> __cdecl PowerMod(class BigInt<35> const &,long,class BigInt<35> const &)" (?PowerMod@@YA?AV?$BigInt@$0CD@@@ABV1@J0@Z) referenced in function "void __cdecl SetEncExp(void)" (?SetEncExp@@YAXXZ) Encrypt.obj
error LNK2019: unresolved external symbol "class BigInt<35> __cdecl PowerMod(class BigInt<35> const &,class BigInt<35> const &,class BigInt<35> const &)" (?PowerMod@@YA?AV?$BigInt@$0CD@@@ABV1@00@Z) referenced in function "bool __cdecl Encrypt(char const * const,char * const,unsigned int)" (?Encrypt@@YA_NQBDQADI@Z) Encrypt.obj
# 3 Re: Template function problem
The error messages look like this:
error LNK2019: unresolved external symbol "class BigInt<35> __cdecl PowerMod(class BigInt<35> const &,long,class BigInt<35> const &)" (?PowerMod@@YA?AV?$BigInt@$0CD@@@ABV1@J0@Z) referenced in function "void __cdecl SetEncExp(void)" (?SetEncExp@@YAXXZ) Encrypt.obj
error LNK2019: unresolved external symbol "class BigInt<35> __cdecl PowerMod(class BigInt<35> const &,class BigInt<35> const &,class BigInt<35> const &)" (?PowerMod@@YA?AV?$BigInt@$0CD@@@ABV1@00@Z) referenced in function "bool __cdecl Encrypt(char const * const,char * const,unsigned int)" (?Encrypt@@YA_NQBDQADI@Z) Encrypt.obj
Well those errors are obvious as to what they mean. The PowerMod function that takes a const reference to three BigInt<T> could not be found by the linker.
Try this:
http://www.dev-archive.com/forum/showthread.php?t=250284
Regards,
Paul McKenzie