求教模板声明与实现

哥其实真的很淡定 2011-04-07 01:08:19
template<class T>
class Checkbook
{
public:
void setBalance(float amount);
bool writeCheck(T amount);
void deposit(float amount);
float getBalance();
T getLastCheck();
float getLastDeposit();
private:
float balance;
T lastCheck;
float lastDeposit;
};
//#include"check.cpp"
template<class T>
void Checkbook<T>::setBalance(float amount)
{
blance=amount;
}
template<class T>
bool Checkbook<T>::writeCheck(T amount)
{
if(amount>balance)
return false;
balance-=amount;
lastCheck=amount;
return true;
}
template<class T>
void Checkbook<T>::deposit(float amount)
{
balance+=amount;
lastDesoit=amount;
}
template<class T>
float Checkbook<T>::getBalance()
{
return balance;
}
template<class T>
T Checkbook<T>::getLastCheck()
{
return lastCheck;
}
template<class T>
float Checkbook<T>::getLastCheck()
{
return lastDeposit;
}
为什么编译器总是报:“Checkbook<T>::getLastCheck”: 无法将函数定义与现有的声明匹配
请高手指教
...全文
114 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wesleyluo 2011-04-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sallan 的回复:]
你应该定义一个float getLastCheck()吧。
在类里没有找到,你仔细看看。
[/Quote]
就这个.
blackstar8888 2011-04-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 quwei197874 的回复:]

模板声明与定义要都放在头文件里.
[/Quote]
++
模板不支持分离编译。
quwei197874 2011-04-07
  • 打赏
  • 举报
回复
模板声明与定义要都放在头文件里.
herry-Li 2011-04-07
  • 打赏
  • 举报
回复

template<class T>
class Checkbook
{
public:
void setBalance(float amount);
bool writeCheck(T amount);
void deposit(float amount);
float getBalance();
T getLastCheck();
float getLastCheck();//少了一个getLastCheck函数的声明
float getLastDeposit();
private:
float balance;
T lastCheck;
float lastDeposit;
};
//#include"check.cpp"
template<class T>
void Checkbook<T>::setBalance(float amount)
{
blance=amount;
}
template<class T>
bool Checkbook<T>::writeCheck(T amount)
{
if(amount>balance)
return false;
balance-=amount;
lastCheck=amount;
return true;
}
template<class T>
void Checkbook<T>::deposit(float amount)
{
balance+=amount;
lastDesoit=amount;
}
template<class T>
float Checkbook<T>::getBalance()
{
return balance;
}
template<class T>
T Checkbook<T>::getLastCheck()
{
return lastCheck;
}
template<class T>
float Checkbook<T>::getLastCheck()
{
return lastDeposit;
}
sallan 2011-04-07
  • 打赏
  • 举报
回复
你应该定义一个float getLastCheck()吧。
在类里没有找到,你仔细看看。

64,648

社区成员

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

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