模板类对象作为其他类的成员 [英] Template class object as member of other class

查看:131
本文介绍了模板类对象作为其他类的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我为链接列表制作了一个模板类. List.h具有类定义,而List.cpp具有用于插入节点,删除节点等的所有函数成员.

当我编译项目(VS2010)时,出现链接器错误"LNK1120 unresolved externals"

我发现由于编译器问题,必须在头文件中全部声明模板类.

我这样做了,再加上在我的main.cpp文件中添加了#includeList.h.我编译了它,一切顺利.

但是现在,我想在不同的头文件newheader.h中创建一个新类或结构,并且该结构将List对象作为私有成员.因此,在我的新头文件的开头,我声明#include List.h,然后声明此结构,然后编译我的代码,出现错误"LNK1169:找到一个或多个乘法定义的符号".

显然这是因为我在新的头文件中添加了#include List.h,链接器看到我的List成员函数在main.cpp和newclass.h
中声明了.
我找不到解决方案.任何帮助将不胜感激

谢谢.

Ivan

Hi,

I made a template class for a linked list. List.h has the class definition and List.cpp has all the function members for inserting a node, deleting them, etc.

When I compile my project (VS2010) I got the linker error "LNK1120 unresolved externals"

I found that due compiler issues, template classes have to be declared all in the header file.

I did this, plus I added #include List.h to my main.cpp file. I compiled it and it went OK.

But now, I want to create a new class or struct in a diferent header file newheader.h, and that struct has a List Object as a private member. So, at the begining of my new header file, I declare #include List.h and after that I declare this struct and then I compile my code, I got the error "LNK1169: one or more multiply defined symbols found"

This is obviously because I added #include List.h in my new header file and the linker sees that my List member functions are declared in main.cpp and newclass.h

I can''t figure a solution for this. Any help would be apreciated

Thanks.

Ivan

推荐答案

听起来我有一个标题块定义问题,尽管我不能不说
任何源代码. List.h中的所有源代码是否都封装有类似

It sounds like you have a header block define problem,although i cannot tell without
any source code. Is all the source code in List.h encapsulated with something like

#ifndef LIST_H
#define LIST_H
 //source code here
#endif



使用这样的define宏将有助于确保编译器仅构建
的符号 您的源代码一次,即使您在多个文件中引用标头也是如此,否则
我认为它将每次都尝试构建模板库.



Using define macros like this will help ensure the compiler only builds the symbols of
your source code once even if you reference the header in more than one file, otherwise
I think it will attempt to build the template library each time.


此错误不是由于您的模板类,而是由于全局变量 gsThreadArgs

该对象通过多个编译单元可见.

解决此问题的一种方法是,在包含 GSALG.h
的任何地方将其声明为 extern
很有可能会解决您的问题.
This error is not because of your template class, but because of the global variable gsThreadArgs

This object is visible through multiple compilation units.

The one way you can solve this is by , declare it extern wherever you have included the GSALG.h

Most probably this will solve your problem.


这篇关于模板类对象作为其他类的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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