这个错误如何修改?
这行显示以下错误:p->show();
[Error] passing 'const Stock' as 'this' argument of 'void Stock::show()' discards qualifiers [-fpermissive]
代码如下:
程序代码:
#include<iostream> #include<string> class Stock{ private: std::string co; double gat; int total; public: Stock(const std::string &s,int i=0,double j=0.0); const Stock &val(const Stock &s)const; void show(); }; void Stock::show(){ std::cout<<co<<std::endl<<total<<std::endl<<gat; } Stock::Stock(const std::string &s,int i,double j){ co=s; total=i; gat=j; } const Stock &Stock::val(const Stock &s)const{ if(s.total>total) return s; else return *this; } int main(){ Stock stock1[4]={ Stock("skg",4,7.7), Stock("skgjg",5,3.9), Stock("irgnj",33,5.7), Stock("eiur",4,5.7) }; const Stock *p=&stock1[0]; for(int i=1;i<4;i++){ p=&p->val(stock1[1]); } p->show(); return 0; }