cannot convert 'this' pointer from 'const class Rational' to 'class Rational &' 是什么意思
2012-05-24 22:41
549 查看
#include<iostream>
using namespace std;
class Item_base
{
public:
Item_base( const string &book=" ni",double sales_price=0.0):isbn(book),price(sales_price){cout<<"构造函数执行"<<endl;}
string book() const//必须加const或者会出现错误'book' : cannot convert 'this' pointer from 'const class Item_base' to 'class Item_base &'
{
return isbn;
}
virtual double net_price(size_t n)const
{
return n*price;
}
virtual ~Item_base(){cout<<"析构函数执行"<<endl;}
private:
string isbn;
protected:
double price;
};
class Bulk_item:public Item_base
{
public:
double net_price(size_t)const;
private:
size_t min_qty;
double discount;
};
double Bulk_item::net_price(size_t cnt)const
{
if(cnt>=min_qty)
return cnt*(1-discount)*price;
else
return cnt*price;
}
void print_total(ostream &os, const Item_base &item,size_t n)
{
os<<"ISBN:"<<item.book()<<"\tnumber sold: "<<n<<"\ttotal price:"<<item.net_price(n)<<endl;
}
int main()
{
Item_base A;
Bulk_item B;
print_total(cout,A,10);
print_total(cout,B,10);
}
这个程序需要加#include<string>或者会出现编译错误因为要用到 string类 要不然会出现binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<c这样的错误
(2)有关const问题 在book ()函数后面要加上const 或者会出现
复制搜索
复制搜索
using namespace std;
class Item_base
{
public:
Item_base( const string &book=" ni",double sales_price=0.0):isbn(book),price(sales_price){cout<<"构造函数执行"<<endl;}
string book() const//必须加const或者会出现错误'book' : cannot convert 'this' pointer from 'const class Item_base' to 'class Item_base &'
{
return isbn;
}
virtual double net_price(size_t n)const
{
return n*price;
}
virtual ~Item_base(){cout<<"析构函数执行"<<endl;}
private:
string isbn;
protected:
double price;
};
class Bulk_item:public Item_base
{
public:
double net_price(size_t)const;
private:
size_t min_qty;
double discount;
};
double Bulk_item::net_price(size_t cnt)const
{
if(cnt>=min_qty)
return cnt*(1-discount)*price;
else
return cnt*price;
}
void print_total(ostream &os, const Item_base &item,size_t n)
{
os<<"ISBN:"<<item.book()<<"\tnumber sold: "<<n<<"\ttotal price:"<<item.net_price(n)<<endl;
}
int main()
{
Item_base A;
Bulk_item B;
print_total(cout,A,10);
print_total(cout,B,10);
}
这个程序需要加#include<string>或者会出现编译错误因为要用到 string类 要不然会出现binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<c这样的错误
(2)有关const问题 在book ()函数后面要加上const 或者会出现
Error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’
Error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’这样的错误这是因为const对象在调用成员函数是会将this指针强制转换成const
this指针,他调用成员函数是会去调用相应的const *成员函数而编译器又无法将非const*类型的成员函数转换为const*类型的成员函数所以会出现编译错误。
复制搜索复制搜索
相关文章推荐
- error C2440: 'static_cast' : cannot convert from 'void (__thiscall CChatDlg::* )(WPARAM,LPARAM)' to
- VC++ 出现错误:cannot convert from const char *' to 'LPCWSTR' 或者'const char [7]' to 'LPCWSTR' 的解决方法
- error C2440: 'static_cast' : cannot convert from 'void (__thiscall CChatDlg::* )(WPARAM,LPARAM)' to
- error C2440:cannotconvertfrom 'void (__thiscall CMainFrame::*)'to'LRESULT(__thiscall CWnd::*)
- error C2440: 'static_cast' : cannot convert from 'void (__thiscall CMainFrame::* )(void)' to 'LRESUL
- Error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’
- 在UNICODE编码格式下使用cvSaveImage出现cannot convert from 'CString' to 'const char *
- cannot convert parameter 1 from 'CString' to 'const char *'
- Error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’的解决办法
- cannot convert parameter 1 from 'const char *' to 'LPCWSTR
- error C2440: 'static_cast' : cannot convert from 'LRESULT (__thiscall CchatDlg::* )(CPoint)' to 'UI
- cannot convert parameter from 'class CStdioFile' to 'class CStdioFile'
- 报错 error C2664: 'CreateWindowExA' : cannot convert parameter 2 from 'unsigned short [10]' to 'const char *'
- error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’
- error C2440: 'static_cast' : cannot convert from 'void (__thiscall CMainFrame::* )(void)' to 'LRESUL
- LoadCursorA' : cannot convert parameter 2 from 'const int' to 'const char *
- Error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’的解决办法
- vs2008下cannot convert parameter 1 from 'LPCTSTR' to 'const char *'的解决方法
- Error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’
- 'LoadLibraryW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR' 解决方案