Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29403
  • 博文数量: 9
  • 博客积分: 492
  • 博客等级: 下士
  • 技术积分: 110
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-08 22:02
文章分类
文章存档

2010年(9)

我的朋友

分类: C/C++

2010-11-06 19:51:34

今天看了看C++中的模板,写了个简单的代码,然后分别用CodeBlocks和GNU/Linux上调试。在GNU/Linux上调试的时候犯了一个“超级低级”的错误,对自己无语了...废话不多讲了,直接上code:
@ header file queue.h
#ifndef QUEUE_H
#define QUEUE_H

#include
using namespace std;

template class Queue
{
public:
Queue();
~Queue();
void set(Type parm);

private:
Type m_parm;
};

template Queue::Queue()
{
cout << "ctor" << endl;
}

template Queue::~Queue()
{
cout << "dtor" << endl;
}

template void Queue::set(Type parm)
{
m_parm = parm;
cout << m_parm << endl;
}

#endif //QUEUE_H

@ source file: main.cpp
#include "queue.h"

int main(void)
{
Queue qi;
qi.set(10);

Queue qs;
qs.set("Hello World!");
return 0;
}

代码完全没有问题的啊。不过后来编译的时候用错了编译器,郁闷....
$ gcc -g main.cpp -o main
结果,出来了一大堆错误,崩溃啊
/cygdrive/c/DOCUME~1/meego/LOCALS~1/Temp/ccQWEhNY.o: In function `__static_initialization_and_destruction_0':
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/iostream:77: undefined reference to `std::ios_base::Init::Init()'
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/iostream:77: undefined reference to `std::ios_base::Init::~Init()'
/cygdrive/c/DOCUME~1/meego/LOCALS~1/Temp/ccQWEhNY.o: In function `main':
/cygdrive/e/SourceCode/C++/ClassTemplate/main.cpp:9: undefined reference to `std::allocator::allocator()'
/cygdrive/e/SourceCode/C++/ClassTemplate/main.cpp:9: undefined reference to `std::basic_string, std::allocator >::basic_string(char const*, std::allocator const&)'
/cygdrive/e/SourceCode/C++/ClassTemplate/main.cpp:9: undefined reference to `std::basic_string, std::allocator >::~basic_string()'
/cygdrive/e/SourceCode/C++/ClassTemplate/main.cpp:9: undefined reference to `std::basic_string, std::allocator >::~basic_string()'
/cygdrive/e/SourceCode/C++/ClassTemplate/main.cpp:9: undefined reference to `std::allocator::~allocator()'
/cygdrive/e/SourceCode/C++/ClassTemplate/main.cpp:9: undefined reference to `std::allocator::~allocator()'
/cygdrive/c/DOCUME~1/meego/LOCALS~1/Temp/ccQWEhNY.o: In function `Queue':
/cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:20: undefined reference to `std::cout'
/cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:20: undefined reference to `std::basic_ostream >& std::operator<< >(std::basic_ostream >&, char const*)'
 cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:20: undefined reference to `std::basic_ostream >& std::endl >(std::basic_ostream >&)'
 cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:20: undefined reference to `std::basic_ostream >::operator<<(std::basic_ostream >& (*)(std::basic_ostream >&))'
 cygdrive/c/DOCUME~1/meego/LOCALS~1/Temp/ccQWEhNY.o: In function `~Queue': cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:25: undefined reference to `std::cout'
 cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:25: undefined reference to `std::basic_ostream >& std::operator<< >(std::basic_ostream >&, char const*)'
 cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:25: undefined reference to `std::basic_ostream >& std::endl >(std::basic_ostream >&)'
 cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:25: undefined reference to `std::basic_ostream >::operator<<(std::basic_ostream >& (*)(std::basic_ostream >&))'
 cygdrive/c/DOCUME~1/meego/LOCALS~1/Temp/ccQWEhNY.o:/cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:30: undefined reference to `std::basic_string, std::allocator >::operator=(std::basic_string, std::allocator > const&)'
 cygdrive/c/DOCUME~1/meego/LOCALS~1/Temp/ccQWEhNY.o:/cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:31: undefined reference to `std::cout'
 cygdrive/c/DOCUME~1/meego/LOCALS~1/Temp/ccQWEhNY.o:/cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:31: undefined reference to `std::basic_ostream >& std::operator<< , std::allocator >(std::basic_ostream >&, std::basic_string, std::allocator > const&)'
 cygdrive/c/DOCUME~1/meego/LOCALS~1/Temp/ccQWEhNY.o:/cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:31: undefined reference to `std::basic_ostream >& std::endl >(std::basic_ostream >&)'
 cygdrive/c/DOCUME~1/meego/LOCALS~1/Temp/ccQWEhNY.o:/cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:31: undefined reference to `std::basic_ostream >::operator<<(std::basic_ostream >& (*)(std::basic_ostream >&))'

 cygdrive/c/DOCUME~1/meego/LOCALS~1/Temp/ccQWEhNY.o: In function `Queue': cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:18: undefined reference to `std::basic_string, std::allocator >::basic_string()' cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:20: undefined reference to `std::cout'
 cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:20: undefined reference to `std::basic_ostream >& std::operator<< >(std::basic_ostream >&, char const*)'
 cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:20: undefined reference to `std::basic_ostream >& std::endl >(std::basic_ostream >&)'
 cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:20: undefined reference to `std::basic_ostream >::operator<<(std::basic_ostream >& (*)(std::basic_ostream >&))'
 cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:20: undefined reference to `std::basic_string, std::allocator >::~basic_string()'
 cygdrive/c/DOCUME~1/meego/LOCALS~1/Temp/ccQWEhNY.o: In function `~Queue': cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:25: undefined reference to `std::cout' cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:25: undefined reference to `std::basic_ostream >& std::operator<< >(std::basic_ostream >&, char const*)'
 cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:25: undefined reference to `std::basic_ostream >& std::endl >(std::basic_ostream >&)'
 cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:25: undefined reference to `std::basic_ostream >::operator<<(std::basic_ostream >& (*)(std::basic_ostream >&))'
 cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:25: undefined reference to `std::basic_string, std::allocator >::~basic_string()'
 cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:25: undefined reference to `std::basic_string, std::allocator >::~basic_string()'
 cygdrive/c/DOCUME~1/meego/LOCALS~1/Temp/ccQWEhNY.o:/cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:31: undefined reference to `std::cout'
 cygdrive/c/DOCUME~1/meego/LOCALS~1/Temp/ccQWEhNY.o:/cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:31: undefined reference to `std::basic_ostream >::operator<<(int)'
 cygdrive/c/DOCUME~1/meego/LOCALS~1/Temp/ccQWEhNY.o:/cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:31: undefined reference to `std::basic_ostream >& std::endl >(std::basic_ostream >&)'
 cygdrive/c/DOCUME~1/meego/LOCALS~1/Temp/ccQWEhNY.o:/cygdrive/e/SourceCode/C++/ClassTemplate/queue.h:31: undefined reference to `std::basic_ostream >::operator<<(std::basic_ostream >& (*)(std::basic_ostream >&))'
 cygdrive/c/DOCUME~1/meego/LOCALS~1/Temp/ccQWEhNY.o:main.cpp:(.eh_frame+0x12): undefined reference to `___gxx_personality_v0'
          ld returned 1 exit status


仔细看了看,发现原因了。原来我正在用C语言编译器来编译C++的代码,真是令人崩溃。让人意想不到的错误。



阅读(2055) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~