我使用智能指针做全句变量,在文件"autoptr.h"中定义,
定义如下: auto_ptr<TServ> pServ;
然后在使用的文件中用 extern "C" auto_ptr<TServ> pServ,编译后出现如下错误:
tstandard.obj : error LNK2005: "class std::auto_ptr<class TServ>  pServ" (?pServ@@3V?$auto_ptr@VTServ@@@std@@A) already defined in debug_main.obj但是如果我在定义 pServ的前面加 static 关键字,即
static auto_ptr<TServ> pServ; 就没问题,加static 的变量只能在包含的文件中使用,因为我有多个文件,是不能做全句变量的。
请问各位大侠,怎么解决???

解决方案 »

  1.   

    在.cpp中定义如下: auto_ptr<TServ> pServ;其他.cpp中 extern auto_ptr<TServ> pServ;
      

  2.   

    #line expected a line number, found 'token'A #line directive lacked the required line-number specification.
      

  3.   

    auto_ptr<TServ> pServ;
    将解释为C++ symbol
     extern "C" auto_ptr<TServ> pServ,
    将其转换为了C symbol
    改为 extern auto_ptr<TServ> pServ,
      

  4.   

    extern "C" auto_ptr<TServ> pServ,上面的把extern "C" 去掉,没有必要像上面那么定义,extern "C" 是用在DLL中声明导出函数的,是为了防止C++编译器改变导出函数的名称.声明全局变量的时候只要extern auto_ptr<TServ> pServ 就可以了!如果不想把他定义成全局变量的话,可以将他定义成一个类的静态变量,在引用他的时候用类名称来引导一下,并用extern 声明一下就可以如:
    class YourClass
    {
    ......
    public:
        static auto_ptr<TServ> pServ;
    .......
    }在引用他的时候如下
     YourClass::auto_ptr<TServ> pServ;
    即可希望对你有所帮助!