注册 登录
编程论坛 C++教室

构造函数放外面错误原因

倾听心跳 发布于 2016-10-19 17:54, 2254 次点击
程序代码:
#include<iostream>
using namespace std;
const int size=100;
class Stock{
public:
    Stock(){
        strcpy(stockcode," ");
    }
    Stock(char na[],int q=1000,double p=8.98);/*{
        strcpy(stockcode,na);
        quan=q;
        price=p;
    }
*/
    void show();
private:
    char stockcode[size];
    int quan;
    double price;
};
Stock::Stock(char na[], int q=1000, double p=8.98){
    strcpy(stockcode,na);
    quan=q;
    price=p;
}
void Stock::show(){
    cout<<this->stockcode<<" "<<this->quan<<" "<<this->price<<endl;
}
int main(){
    Stock s1("600001",3000,5.67);
    s1.show();
    Stock s2("600002");
    s2.show();
    return 0;
}
2 回复
#2
rjsp2016-10-19 21:50
贴出编译器给出的错误信息,比如叫你不要重复定义缺省值
#3
lin_jun_cai2016-10-20 16:15
在外面的构造函数中,把Stock::Stock(char na[], int q=1000, double p=8.98) 改为 Stock::Stock(char na[], int q, double p)
1