模板类,操作符“=”重载的问题

Coder李海波 2008-05-24 03:46:06
.hpp
--------------------------------------------------------------------------
template <class type>
class BaseString
{
public:
BaseString <type>& operator=(const BaseString <type>& str)
}

.cpp
--------------------------------------------------------------------------
template <class type>
BaseString <type>& BaseString <type>::operator=(const BaseString <type>& str)
{
......
}
-------------------------------------------------------------------------
BaseString <char> str1;
BaseString <char> str2("12");
str1 = str2;
link的时候,提示:undefined reference to `BaseString <char>::operator=(BaseString <char> const&)'
我百思不得其解。
...全文
124 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Coder李海波 2008-05-24
  • 打赏
  • 举报
回复
我明白了,模板的实现必须和头文件放在一起。害得我弄了一个来小时,寒一个。
感谢各位大力支持~
  • 打赏
  • 举报
回复
google 模板分离编译
就能知道满意的答案
Coder李海波 2008-05-24
  • 打赏
  • 举报
回复
to 村少
为什么需要加到hpp中?我才开始学模板,不是很熟悉。
  • 打赏
  • 举报
回复

template <class type>
class BaseString
{
public:
BaseString <type>& operator=(const BaseString <type>& str) ;
BaseString()
{}
BaseString(type* )
{
}
};


template <class type>
BaseString<type>& BaseString<type>::operator=(const BaseString<type>& str)
{

return (*this);
}


int main()
{
BaseString <char> str1;
BaseString <char> str2("12");
str1 = str2;
return 0;
}


我这样的代码就没问题
  • 打赏
  • 举报
回复
把模板的实现放到.h中
cunsh 2008-05-24
  • 打赏
  • 举报
回复
.cpp
--------------------------------------------------------------------------
template <class type>
BaseString <type>& BaseString <type>::operator=(const BaseString <type>& str)
{
......
}




=======
把这个 .cpp 中的加到 .hpp中.
effective_person 2008-05-24
  • 打赏
  • 举报
回复
template <typename type>
void operator =(const BaseString<type>&str)
{
//this-> 成员=str.成员; 应该是这样的吧!
]
Zhentiwei 2008-05-24
  • 打赏
  • 举报
回复
BaseString <type>& operator=(const BaseString <type>& str)
BaseString <char>::operator=(BaseString <char> const&)
const的位置怎么不一样了?

64,654

社区成员

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

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