这个错误如何解决?
该项目在类的成员函数的实现部分Stock.cpp文件中出现如下错误:[Error] prototype for 'void Stock::acquire(const string&, long int, double)' does not match any in class 'Stock'
在头文件Stock.h文件中出现如下错:
[Error] candidate is: void Stock::acquire(const char*, int, double)
并显示:
Makefile.win recipe for target 'Stock.o' failed
Stock.h文件如下:
程序代码:
#ifndef STOCK_H_ #define STOCK_H_ class Stock{ private: int shares; char company[30]; double share_val; double total_val; void set_tot(){ total_val=shares*share_val; } public: void acquire(const char * co,int n,double pr); void buy(int num,double price); void sell(int num,double price); void update(double price); void show(); }; #endif
Stock.cpp文件如下:
程序代码:
#include<iostream> #include"Stock.h" void Stock::acquire(const std::string & co,long 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'; }
main.cpp文件如下:
程序代码:
#include <iostream> #include"Stock.h" /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { Stock asd; asd.acquire("NanoSmart",20,12.50); asd.show(); return 0; }