使用extern声明类对象 [英] Using extern to declare class objects

查看:136
本文介绍了使用extern声明类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们...
我在一个cpp文件中声明一个类的对象,并使用该对象调用函数.现在,我想在另一个cpp文件中使用同一对象.我试图使用extern在另一个文件中声明该对象,但是链接器给我一个错误,该错误已定义对象.

第一个cpp文件:

Hi friends...
I am declaring object of a class in one cpp file and calling functions using that object. Now I want to use same object in another cpp file. I am trying to declare that object in another file using extern, but linker gives me an error that object is already defined.

First cpp file:

CClientSock client;
lv_status = client.startup();
if (lv_status != 0)
{
    MessageBox("Connection Failed");
    exit(1);
}



第二个cpp文件:



Second cpp file:

extern CClientSock client;
client.connectSocket();



请提出解决方案.请让我知道是否需要更多信息.谢谢!



Please suggest the solution. Kindly let me know if more info is required. Thank you!

推荐答案

嗨.我更改了对象"client"的名称&问题得到解决.不知道如何...:)
Hi .. I changed the name of object ''client'' & the problem got resolved . Dont know how ...:)


类不能是外部的.在头文件中声明类,并改用它们. :)

http://publib.boulder.ibm. com/infocenter/macxhelp/topic/com.ibm.vacpp6m.doc/language/ref/clrc03extsc.htm [
Classes cannot be extern. Declare classes in a header file and use them instead. :)

http://publib.boulder.ibm.com/infocenter/macxhelp/topic/com.ibm.vacpp6m.doc/language/ref/clrc03extsc.htm[^]


这篇关于使用extern声明类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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