首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++模板的引用有关问题,

2013-01-11 
C++模板的引用问题,求助!/* * Base.h * *Created on: 2012-12-27 *Author: root */#ifndef BASE_H_#define

C++模板的引用问题,求助!

/*

 * Base.h

 *

 *  Created on: 2012-12-27

 *      Author: root

 */

#ifndef BASE_H_

#define BASE_H_

template<class A>

class Base {

public:

    Base();

    virtual ~Base();

    int set();

    Base<A> static config;

};

#endif /* BASE_H_ */


/*

 * Base.cpp

 *

 *  Created on: 2012-12-27

 *      Author: root

 */

#include "Base.h"

template<class A> Base<A> Base<A>::config = new Base<int>();

template<class A>

Base<A>::Base() {

    // TODO Auto-generated constructor stub

}

template<class A>

Base<A>::~Base() {

    // TODO Auto-generated destructor stub

}

template<class A> int Base<A>::set() {

    return 0;

}


int main() {

//    DataBean* b = new DataBean();

    //cout << typeid(b).name() << endl;

    Base<int>::config.set();

}


编译连接时报:

Building target: Test

Invoking: GCC C++ Linker

g++  -o "Test"  ./src/Base.o ./src/Bean.o ./src/Test.o   

./src/Test.o: In function `main':

Test.cpp:(.text.startup+0xc): undefined reference to `Base<int>::config'

Test.cpp:(.text.startup+0x11): undefined reference to `Base<int>::set()'

collect2: ld returned 1 exit status

make: *** [Test] 错误 1

想不明白为什么我已经有new一个config,怎么还会说不声明呢?怎么改进可以通过编译呢?


[解决办法]
模板的所有代码都必须写在头文件里

热点排行