C++ C++;使用标头定义的模板类乘以定义的符号
我正在VisualStudio2005中处理一个带有DLL和EXE的项目。DLL的代码中有一个可扩展数组类的模板:C++ C++;使用标头定义的模板类乘以定义的符号,c++,templates,visual-studio-2005,C++,Templates,Visual Studio 2005,我正在VisualStudio2005中处理一个带有DLL和EXE的项目。DLL的代码中有一个可扩展数组类的模板: template <class Type> class GArray { Type *p; uint32 len; uint32 alloc; protected: bool fixed; public: /// Constructor GArray(int PreAlloc = 0) { p
template <class Type>
class GArray
{
Type *p;
uint32 len;
uint32 alloc;
protected:
bool fixed;
public:
/// Constructor
GArray(int PreAlloc = 0)
{
p = 0;
len = 0;
fixed = false;
alloc = PreAlloc;
if (alloc)
{
int Bytes = sizeof(Type) * alloc;
p = (Type*) malloc(Bytes);
if (p)
{
memset(p, 0, Bytes);
}
else
{
alloc = 0;
}
}
}
/// Destructor
~GArray()
{
Length(0);
}
/// Returns the number of used entries
uint32 Length() const
{
return len;
}
/// Sets the length of available entries
bool Length(uint32 i)
{
if (i > 0)
{
if (i > len && fixed)
return false;
uint nalloc = alloc;
if (i < len)
{
// Shrinking
}
else
{
// Expanding
int b;
for (b = 4; (1 << b) < i; b++)
;
nalloc = 1 << b;
LgiAssert(nalloc >= i);
}
if (nalloc != alloc)
{
Type *np = (Type*)malloc(sizeof(Type) * nalloc);
if (!np)
{
return false;
}
if (p)
{
// copy across common elements
memcpy(np, p, min(len, i) * sizeof(Type));
free(p);
}
p = np;
alloc = nalloc;
}
if (i > len)
{
// zero new elements
memset(p + len, 0, sizeof(Type) * (i - len));
}
len = i;
}
else
{
if (p)
{
int Length = len;
for (uint i=0; i<Length; i++)
{
p[i].~Type();
}
free(p);
p = 0;
}
len = alloc = 0;
}
return true;
}
GArray<Type> &operator =(const GArray<Type> &a)
{
Length(a.Length());
if (p && a.p)
{
for (int i=0; i<len; i++)
{
p[i] = a.p[i];
}
}
return *this;
}
/// \brief Returns a reference a given entry.
///
/// If the entry is off the end of the array and "fixed" is false,
/// it will grow to make it valid.
Type &operator [](uint32 i)
{
static Type t;
if
(
i < 0
||
(fixed && i >= len)
)
{
ZeroObj(t);
return t;
}
#if 0
if (i > 15000000)
{
#if defined(_DEBUG) && defined(_MSC_VER)
LgiAssert(0);
#endif
ZeroObj(t);
return t;
}
#endif
if (i >= alloc)
{
// increase array length
uint nalloc = max(alloc, GARRAY_MIN_SIZE);
while (nalloc <= i)
{
nalloc <<= 1;
}
// alloc new array
Type *np = (Type*) malloc(sizeof(Type) * nalloc);
if (np)
{
// clear new cells
memset(np + len, 0, (nalloc - len) * sizeof(Type));
if (p)
{
// copy across old cells
memcpy(np, p, len * sizeof(Type));
// clear old array
free(p);
}
// new values
p = np;
alloc = nalloc;
}
else
{
static Type *t = 0;
return *t;
}
}
// adjust length of the the array
if (i + 1 > len)
{
len = i + 1;
}
return p[i];
}
/// Delete all the entries as if they are pointers to objects
void DeleteObjects()
{
for (uint i=0; i<len; i++)
{
DeleteObj(p[i]);
}
Length(0);
}
/// Delete all the entries as if they are pointers to arrays
void DeleteArrays()
{
for (int i=0; i<len; i++)
{
DeleteArray(p[i]);
}
Length(0);
}
/// Find the index of entry 'n'
int IndexOf(Type n)
{
for (uint i=0; i<len; i++)
{
if (p[i] == n) return i;
}
return -1;
}
/// Returns true if the item 'n' is in the array
bool HasItem(Type n)
{
return IndexOf(n) >= 0;
}
/// Deletes an entry
bool DeleteAt
(
/// The index of the entry to delete
uint Index,
/// true if the order of the array matters, otherwise false.
bool Ordered = false
)
{
if (p && Index >= 0 && Index < len)
{
// Delete the object
p[Index].~Type();
// Move the memory up
if (Index < len - 1)
{
if (Ordered)
{
memmove(p + Index, p + Index + 1, (len - Index - 1) * sizeof(Type) );
}
else
{
p[Index] = p[len-1];
}
}
// Adjust length
len--;
return true;
}
return false;
}
/// Deletes the entry 'n'
bool Delete
(
/// The value of the entry to delete
Type n,
/// true if the order of the array matters, otherwise false.
bool Ordered = false
)
{
int i = IndexOf(n);
if (p && i >= 0)
{
return DeleteAt(i, Ordered);
}
return false;
}
/// Appends an element
void Add
(
/// Item to insert
const Type &n
)
{
(*this)[len] = n;
}
/// Appends multiple elements
void Add
(
/// Items to insert
Type *s,
/// Length of array
int count
)
{
if (!s || count < 1)
return;
int i = len;
Length(len + count);
Type *d = p + i;
while (count--)
{
*d++ = *s++;
}
}
/// Inserts an element into the array
bool AddAt
(
/// Item to insert before
int Index,
/// Item to insert
Type n
)
{
// Make room
if (Length(len + 1))
{
if (Index < len - 1)
{
// Shift elements after insert point up one
memmove(p + Index + 1, p + Index, (len - Index - 1) * sizeof(Type) );
}
else if (Index >= len)
{
// Add at the end, not after the end...
Index = len - 1;
}
// Insert item
p[Index] = n;
return true;
}
return false;
}
/// Sorts the array
void Sort(int (*Compare)(Type*, Type*))
{
typedef int (*qsort_compare)(const void *, const void *);
qsort(p, len, sizeof(Type), (qsort_compare)Compare);
}
/// \returns a reference to a new object on the end of the array
Type &New()
{
return (*this)[len];
}
/// Returns the memory held by the array and sets itself to empty
Type *Release()
{
Type *Ptr = p;
p = 0;
len = alloc = 0;
return Ptr;
}
};
模板
加雷级
{
*p型;
uint32透镜;
uint32-alloc;
受保护的:
布尔固定;
公众:
///建造师
加里(int PreAlloc=0)
{
p=0;
len=0;
固定=假;
alloc=前alloc;
如果(alloc)
{
int Bytes=sizeof(Type)*alloc;
p=(类型*)malloc(字节);
如果(p)
{
memset(p,0,字节);
}
其他的
{
alloc=0;
}
}
}
///析构函数
~GArray()
{
长度(0);
}
///返回已使用的项目数
uint32长度()常量
{
回程透镜;
}
///设置可用项的长度
布尔长度(uint32 i)
{
如果(i>0)
{
如果(i>len&&fixed)
返回false;
uint-nalloc=alloc;
如果(i=alloc)
{
//增加数组长度
uint nalloc=最大值(alloc,加里最小尺寸);
而(nalloc=0);
int64 StartTs=lgiccurrenttime();
int Del=min(Len,a.Length()-Start);
如果(Del>0)
{
int Remain=a.Length()-Start-Del;
如果(保持>0)
{
memmove(&a[Start],&a[Start+Del],Remain);
MoveBytes+=剩余;
a、 长度(开始+保留);
}
否则a.长度(开始);
}
int64 End=LgiCurrentTime();
DeleteTime+=结束-开始时间;
}
编译和链接正常…但在FrameStore.cpp中:
void Scan()
{
if (!Init)
{
Init = true;
GAutoString Path = FrameFile::GetPath();
GAutoPtr<GDirectory> Dir(FileDev->GetDir());
GArray<char*> k;
int64 Size = 0;
for (bool b = Dir->First(Path); b; b = Dir->Next())
{
if (!Dir->IsDir())
{
char *e = LgiGetExtension(Dir->GetName());
if (e && !stricmp(e, "mjv"))
{
char p[MAX_PATH];
Dir->Path(p, sizeof(p));
k.Add(NewStr(p));
Size += Dir->GetSize();
}
}
}
GAutoPtr<Prog> p(new Prog(Size));
for (int i=0; i<k.Length(); i++)
{
Files.Add(new FrameFile(k[i], p));
}
k.DeleteArrays();
}
}
void Scan()
{
if(!Init)
{
Init=真;
GAutoString Path=FrameFile::GetPath();
GAutoPtr Dir(FileDev->GetDir());
加雷k;
int64大小=0;
对于(bool b=Dir->First(Path);b;b=Dir->Next())
{
如果(!Dir->IsDir())
{
char*e=LgiGetExtension(Dir->GetName());
if(e&&!stricmp(e,“mjv”))
{
charp[MAX_PATH];
Dir->Path(p,sizeof(p));
k、 加(NewStr(p));;
Size+=Dir->GetSize();
}
}
}
GAutoPtr p(新项目(尺寸));
对于(int i=0;i如果您使用的是Visual Studio 6,请确保设置了以下选项:
项目->设置->C/C++->代码生成->使用运行时库===>调试多线程/多线程
编辑:
在VS 2005中,情况基本相同
项目->属性->配置属性->C/C++->代码生成->运行时库->多线程/多线程调试作为旁注,您可能希望拆分声明和定义,因此它没有那么难看:
template <class Type>
class GArray
{
Type *p;
uint32 len;
uint32 alloc;
protected:
bool fixed;
public:
GArray(int PreAlloc = 0);
/// Destructor
~GArray() {Length(0);}
/// Returns the number of used entries
int Length() {return len;}
/// Sets the length of available entries
bool Length(uint32 i);
// etc...
};
template <class Type>
GArray<Type>::GArray(int PreAlloc = 0)
{
p = 0;
len = 0;
fixed = false;
alloc = PreAlloc;
if (alloc)
{
int Bytes = sizeof(Type) * alloc;
p = (Type*) malloc(Bytes);
if (p)
{
memset(p, 0, Bytes);
}
else
{
alloc = 0;
}
}
}
template <class Type>
bool GArray<Type>::Length(uint32 i);
{
if (i > 0)
{
if (i > len && fixed)
return false;
uint nalloc = alloc;
if (i < len)
{
// Shrinking
}
else
{
// Expanding
int b;
for (b = 4; (1 << b) < i; b++)
;
nalloc = 1 << b;
LgiAssert(nalloc >= i);
}
if (nalloc != alloc)
{
Type *np = (Type*)malloc(sizeof(Type) * nalloc);
if (!np)
{
return false;
}
if (p)
{
// copy across common elements
memcpy(np, p, min(len, i) * sizeof(Type));
free(p);
}
p = np;
alloc = nalloc;
}
if (i > len)
{
// zero new elements
memset(p + len, 0, sizeof(Type) * (i - len));
}
len = i;
}
else
{
if (p)
{
int Length = len;
for (uint i=0; i<Length; i++)
{
p[i].~Type();
}
free(p);
p = 0;
}
len = alloc = 0;
}
return true;
}
模板
加雷级
{
*p型;
uint32透镜;
uint32-alloc;
受保护的:
布尔固定;
公众:
加里(int PreAlloc=0);
///析构函数
~GArray(){Length(0);}
///返回已使用的项目数
int Length(){return len;}
///设置可用项的长度
布尔长度(uint32 i);
//等等。。。
};
模板
加里::加里(int PreAlloc=0)
{
p=0;
len=0;
固定=假;
alloc=前alloc;
如果(alloc)
{
int Bytes=sizeof(Type)*alloc;
p=(类型*)malloc(字节);
如果(p)
{
memset(p,0,字节);
}
其他的
{
alloc=0;
}
}
}
模板
布尔加里:长度(uint32 i);
{
如果(i>0)
{
如果(i>len&&fixed)
返回false;
uint-nalloc=alloc;
如果(i2>lgi8d.lib(lgi8d.dll):错误LNK2005:“public:int\u thiscall GArray::Length(void)”(?Length@$GArray@PAD@@QAEHXZ)已在FrameStore.obj中定义
2> D:\Home\matthew\network\u camera\src\vod\u test\Debug\vod\u test.exe:致命错误LNK1169:找到一个或多个多重定义符号
lgi8d.lib
和vod_test.exe
是两个独立的二进制文件。问题可能在于.lib
已经定义了.exe再次定义的符号。您可以尝试将declspec(dllexport)添加到DLL中的类和exe中的declspec(dllimport)。例如
#if !defined(MYDLLEXPORT)
// We are users of, and *importing* the library routines...
#define MYLIB_SPEC __declspec(dllimport)
#else
// We are building and exporting the library routines...
#define MYLIB_SPEC __declspec(dllexport)
#endif
// ...
template<typename Type>
class MYLIB_SPEC GArray // ...
#如果!已定义(MydLexPort)
//我们是和*导入*库例程的用户。。。
#定义MYLIB\u规范\uuu declspec(dllimport)
#否则
//我们正在构建和导出库例程。。。
#定义MYLIB_SPEC__declspec(dllexport)
#恩迪夫
// ...
模板
MYLIB_等级规范GArray/。。。
然后确保MYDLLEXPORT是在构建DLL的项目中定义的,而不是为EXE定义的
好吧,你通常不需要这个模板
template <class Type>
class GArray
{
Type *p;
uint32 len;
uint32 alloc;
protected:
bool fixed;
public:
GArray(int PreAlloc = 0);
/// Destructor
~GArray() {Length(0);}
/// Returns the number of used entries
int Length() {return len;}
/// Sets the length of available entries
bool Length(uint32 i);
// etc...
};
template <class Type>
GArray<Type>::GArray(int PreAlloc = 0)
{
p = 0;
len = 0;
fixed = false;
alloc = PreAlloc;
if (alloc)
{
int Bytes = sizeof(Type) * alloc;
p = (Type*) malloc(Bytes);
if (p)
{
memset(p, 0, Bytes);
}
else
{
alloc = 0;
}
}
}
template <class Type>
bool GArray<Type>::Length(uint32 i);
{
if (i > 0)
{
if (i > len && fixed)
return false;
uint nalloc = alloc;
if (i < len)
{
// Shrinking
}
else
{
// Expanding
int b;
for (b = 4; (1 << b) < i; b++)
;
nalloc = 1 << b;
LgiAssert(nalloc >= i);
}
if (nalloc != alloc)
{
Type *np = (Type*)malloc(sizeof(Type) * nalloc);
if (!np)
{
return false;
}
if (p)
{
// copy across common elements
memcpy(np, p, min(len, i) * sizeof(Type));
free(p);
}
p = np;
alloc = nalloc;
}
if (i > len)
{
// zero new elements
memset(p + len, 0, sizeof(Type) * (i - len));
}
len = i;
}
else
{
if (p)
{
int Length = len;
for (uint i=0; i<Length; i++)
{
p[i].~Type();
}
free(p);
p = 0;
}
len = alloc = 0;
}
return true;
}
2>lgi8d.lib(Lgi8d.dll) : error LNK2005: "public: int __thiscall GArray<char *>::Length(void)" (?Length@?$GArray@PAD@@QAEHXZ) already defined in FrameStore.obj
2>D:\Home\matthew\network_camera\src\vod_test\Debug\vod_test.exe : fatal error LNK1169: one or more multiply defined symbols found
#if !defined(MYDLLEXPORT)
// We are users of, and *importing* the library routines...
#define MYLIB_SPEC __declspec(dllimport)
#else
// We are building and exporting the library routines...
#define MYLIB_SPEC __declspec(dllexport)
#endif
// ...
template<typename Type>
class MYLIB_SPEC GArray // ...
#ifdef LGI_DLL
#define LgiClass __declspec(dllexport)
#else
#define LgiClass __declspec(dllimport)
#endif
class LgiClass GToken : public GArray<char*>
{
/// stuff...
};