试着写了个class template,编译错误,说是库文件里有错误,我总不能帮borland debug吧,挺纳闷儿的,请大家看看。

kaitree 2003-08-12 05:57:50
试着写了个class template,编译错误,说是库文件里有错误,我总不能帮borland debug吧,挺纳闷儿的,请大家看看。

我的代码:
//test.h
#include <iostream.h>

template <class T>
class Test
{
private:
T total;
public:
set(T t1, T t2){total = t1 + t2;}
};

编译情况:
D:\C++\code\friends>bcc32 test.h
Borland C++ 5.6 for Win32 Copyright (c) 1993, 2002 Borland
test.h:
Error E2257 C:\Program Files\Borland\CBuilder6\Include\stl/_mbstate.h
52: , expected
Error E2453 C:\Program Files\Borland\CBuilder6\Include\stl/type_traits
.h 64: Size of the type '__true_type' is unknown or zero
Error E2453 C:\Program Files\Borland\CBuilder6\Include\stl/type_traits
.h 65: Size of the type '__false_type' is unknown or zero
Error E2141 C:\Program Files\Borland\CBuilder6\Include\stl/type_traits
.h 68: Declaration syntax error
Error E2141 C:\Program Files\Borland\CBuilder6\Include\stl/type_traits
.h 70: Declaration syntax error
Error E2141 C:\Program Files\Borland\CBuilder6\Include\stl/type_traits
.h 73: Declaration syntax error
Error E2141 C:\Program Files\Borland\CBuilder6\Include\stl/type_traits
.h 77: Declaration syntax error

...........

.h 235: Declaration syntax error
Error E2228 C:\Program Files\Borland\CBuilder6\Include\stl/type_traits
.h 235: Too many error or warning messages
*** 26 errors in Compile ***
...全文
39 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaitree 2003-08-12
  • 打赏
  • 举报
回复
嗯,看来一定要有个main在.h文件里面,才能正常编译。
greenspan 2003-08-12
  • 打赏
  • 举报
回复
增加#include <string>后
把Button1Click中的代码改为
Test<std::string> t;
t.set(std::string("I love "),std::string("you!"));
Caption=t.Total().c_str();
也正常

模板的错误是有点怪的,不过一般是由于你的实例化错误引起的
greenspan 2003-08-12
  • 打赏
  • 举报
回复
CB5下我试了一下,没错,我的代码
File1.h中
///////////////////////////////////
#include <iostream.h>

template <class T>
class Test
{
private:
T total;
public:
set(T t1, T t2){total = t1 + t2;}
T Total()const{return total;}
};
Form1中
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include <iostream.h>
#include "Unit1.h"
#include "File1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{

Test<int> t;
t.set(10,20);
Caption=t.Total();

}
//---------------------------------------------------------------------------
输出也正常
layman2008 2003-08-12
  • 打赏
  • 举报
回复
vc 6.0下没问题
kaitree 2003-08-12
  • 打赏
  • 举报
回复
自己发现只要光编译.h的文件就会有以上错误信息,但是为什么呢?
.h文件也许不能单独被编译,可错误信息也很怪呀,如何导致这样的信息的勒?
kaitree 2003-08-12
  • 打赏
  • 举报
回复
如果注释掉第一行中的 iostream.h 如下:
//#include <iostream.h>

template <class T>
class Test
{
private:
T total;
public:
set(T t1, T t2){total = t1 + t2;}
};

则编译情况如下:

D:\C++\code\friends>bcc32 test.h
Borland C++ 5.6 for Win32 Copyright (c) 1993, 2002 Borland
test.h:
Error E2141 test.h 3: Declaration syntax error
*** 1 errors in Compile ***
===============
到底是怎么稿第嘛?
fangrk 2003-08-12
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;


template <class T>
class Test
{
private:
T total;
public:
set(T t1, T t2){total = t1 + t2;}
};

int main()
{

}

编译没有问题
kaitree 2003-08-12
  • 打赏
  • 举报
回复
噢,我找到问题了,谢谢各位,看来大家都喜欢用GUI的IDE哈。

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧