改为std::string & co这样后,有新的问题,
这行出现错误:
显示以下错误信息:
[Error] no matching function for call to 'Stock::acquire(const char [10], int, double)'
这行出现错误:
显示以下错误信息:
[Note] void Stock::acquire(std::string&, int, double)
[Note] no known conversion for argument 1 from 'const char [10]' to 'std::string& {aka std::basic_string<char>&}'
如果改为const std::string & co就可以,
为什么不能改为std::string & co?一定要加上const?
代码如下:
这行出现错误:
asd.acquire("NanoSmart",20,12.50);
显示以下错误信息:
[Error] no matching function for call to 'Stock::acquire(const char [10], int, double)'
这行出现错误:
void Stock::acquire(std::string &co,int n,double pr){
显示以下错误信息:
[Note] void Stock::acquire(std::string&, int, double)
[Note] no known conversion for argument 1 from 'const char [10]' to 'std::string& {aka std::basic_string<char>&}'
如果改为const std::string & co就可以,
为什么不能改为std::string & co?一定要加上const?
代码如下:
程序代码:
#include<iostream> #include<string> class Stock{ private: int shares; std::string company; double share_val; double total_val; void set_tot(){ total_val=shares*share_val; } public: void acquire(std::string &co,int n,double pr); void buy(int num,double price); void sell(int num,double price); void update(double price); void show(); }; void Stock::acquire(std::string &co,int n,double pr){ company=co; if(n<0){ std::cout<<"Number of shares can not be negative;"<<company<<"shares set to 0.\n"; shares=0; } else shares=n; share_val=pr; set_tot(); } void Stock::show(){ std::cout<<"company:"<<company <<" Shares:"<<shares<<'\n' <<"share price:$"<<share_val <<" total worth:$"<<total_val<<'\n'; } int main() { Stock asd; asd.acquire("NanoSmart",20,12.50); asd.show(); return 0; }
[此贴子已经被作者于2020-12-16 16:41编辑过]