自定义类时,不知该包含什么头文件解决办法
自定义类时,不知该包含什么头文件
定义了一个Item_base类,有默认构造函数,和3个复制控制,还一个print()。
其他经过测试都没问题,只是print这个报错。
//Item_base.h
#ifndef ITEM_BASE_H
#define ITEM_BASE_H
#include <iostream>
class Item_base
{
public:
Item_base(const std::string &book="",
double sales_price=0.0):isbn(book),price(sales_price)
{
std::cout<<"Item_base constructor invoked"<<std::endl;
}
Item_base(const Item_base&);
Item_base& operator=(const Item_base&);
~Item_base();
virtual void print()
{
std::cout<<"No discount ISBN: "<<isbn<<"\t"<<"Price: "<<price<<std::endl;
}
protected:
double price;
std::string isbn;
};
#endif
报错
d:\c++\item_base.h(20) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)
Error executing cl.exe.
------解决方案--------------------
定义了一个Item_base类,有默认构造函数,和3个复制控制,还一个print()。
其他经过测试都没问题,只是print这个报错。
//Item_base.h
#ifndef ITEM_BASE_H
#define ITEM_BASE_H
#include <iostream>
class Item_base
{
public:
Item_base(const std::string &book="",
double sales_price=0.0):isbn(book),price(sales_price)
{
std::cout<<"Item_base constructor invoked"<<std::endl;
}
Item_base(const Item_base&);
Item_base& operator=(const Item_base&);
~Item_base();
virtual void print()
{
std::cout<<"No discount ISBN: "<<isbn<<"\t"<<"Price: "<<price<<std::endl;
}
protected:
double price;
std::string isbn;
};
#endif
报错
d:\c++\item_base.h(20) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)
Error executing cl.exe.
------解决方案--------------------
- C/C++ code
添加#include<string> /*********/ Item_base(const Item_base&); Item_base& operator=(const Item_base&); ~Item_base() {} //丢了{}
------解决方案--------------------
你的程序中不是使用了string了吗,需要它的头文件。
------解决方案--------------------
包含string头文件
------解决方案--------------------
你用了string的,string又不是基本类型
------解决方案--------------------
"< <" 应该是 << 中间有空格!!
------解决方案--------------------
说错了,没包含<string>就不存在string isbn,自然cout<<操作对它不成立!
std::cout <<"No discount ISBN: " <<isbn <<"\t" < <"Price: " <<price <<std::endl;
------解决方案--------------------
使用了string 类,却不包含其头文件,是有点粗心了哈!