关于template的入门级问题

blue_laser 2004-12-14 02:20:37
小弟初学GNUC++在按照书上的例子,如有有误。
class bifstream: public ifstream{
public:

bifstream(const char *fn):ifstream(fn, ios::in|ios::binary){};
void readBytes(void *,int cou);
template <class T>
bifstream &operator>>(T & d);
virtual ~bifstream();
};
bifstream::~bifstream()
{}
void bifstream::readBytes(void *p,int cou)
{
if(!p)return ;
if(cou<=0)return;
read((char*)p,cou);
}
template <class T>
bifstream &bifstream::operator>>(T & d)
{
readBytes(&d, sizeof(d));
return *this;
}
int main()
{
double d1,d2,d3 ;
d1 = 3.1415926;

bifstream bis("test.txt");
bis>>d1>>d2;//问题所在处 undefined referance .....
cout<<"d1"<<d1<<endl<<"d2"<<d2<<endl;
return 0;
}
...全文
205 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
blue_laser 2004-12-17
  • 打赏
  • 举报
回复
问题已经解决将<<endl去掉后通过,谢谢各位大虾。分不在多,心诚(谢意)则行!
drip 2004-12-17
  • 打赏
  • 举报
回复
改cout<<"d1"<<d1<<endl<<"d2"<<d2<<endl;
为cout<<"d1"<<d1<<endl<<"d2"<<d2;
liweiswin 2004-12-16
  • 打赏
  • 举报
回复
正常编译运行。WIN2K+ DEV C++
#include<iostream>
#include<fstream>
#include<string>
using namespace std;



class bifstream: public ifstream{
public:

bifstream(const char *fn):ifstream(fn, ios::in|ios::binary){};
void readBytes(void *,int cou);
template <class T>
bifstream &operator>>(T & d);
virtual ~bifstream();
};
bifstream::~bifstream()
{}
void bifstream::readBytes(void *p,int cou)
{
if(!p)return ;
if(cou<=0)return;
read((char*)p,cou);
}
template <class T>
bifstream &bifstream::operator>>(T & d)
{
readBytes(&d, sizeof(d));
return *this;
}
int main()
{
double d1,d2,d3 ;
d1 = 3.1415926;

bifstream bis("test.txt");
bis>>d1>>d2;//问题所在处 undefined referance .....
cout<<"d1"<<d1<<endl<<"d2"<<d2<<endl;
getchar();
return 0;
}
sharkhuang 2004-12-16
  • 打赏
  • 举报
回复
这个函数没有实现啊
blue_laser 2004-12-16
  • 打赏
  • 举报
回复
各位老大,我在VC下编译也不通过。是连接错误????
tsttemp.obj : error LNK2001: unresolved external symbol "public: class bofstream & __thiscall bofstream::operator<<(double const &)" (??6bofstream@@QAEAAV0@ABN@Z)
longtian_haidao 2004-12-16
  • 打赏
  • 举报
回复
没错阿
c200288c 2004-12-14
  • 打赏
  • 举报
回复
我在gcc上编译一个类似的例子是可以的,但在vc上不行. 应该是编译器的问题
class A
{
public:
int m_value;
A()
{
printf( "[C] A\n" );
m_value = 1;
};

template<class T> void p(T a);

virtual ~A()
{
printf( "[D] A\n" );
};

};

template<class T> void A::p(T a)
{
};

class B
{
public:
B(){};
};

int main(int argc, char* argv[])
{
A a;
B b;
a.p(b);
return 0;
}
leojay 2004-12-14
  • 打赏
  • 举报
回复
在VC6.0里,写在类的里面就对了。
class ... {
.
.
.

template <class T>
bifstream & operator >> (T& d)
{
readBytes(&d, sizeof(d));
return *this;
}
}
我也不知道为什么。
beyondtkl 2004-12-14
  • 打赏
  • 举报
回复
哦。 也可以不用實例華得。。
dongyuanzhang 2004-12-14
  • 打赏
  • 举报
回复
应该可以通过的!
yjh1982 2004-12-14
  • 打赏
  • 举报
回复
编译环境...gcc版本....
pacman2000 2004-12-14
  • 打赏
  • 举报
回复
可以通过的啊。。。

#include <iostream>
#include <fstream>
using namespace std;
drip 2004-12-14
  • 打赏
  • 举报
回复
没有把模版函数实例化

64,685

社区成员

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

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