-2
我刚开始学习cpp。我用2个构造函数创建了一个类。 当我运行在Ubuntu这个命令:g++ -Wall -g main.cpp car.cpp -o a
我得到一个错误按摩:C++中的构造函数错误
> "In file included from main.cpp:2:0: car.h:10:15:
error: expected ‘)’ before ‘,’ token car(string,string,int);
In file included from car.cpp:1:0: car.h:10:15:
error: expected ‘)’ before ‘,’ token car(string,string,int);
car.cpp:10:9: error: expected constructor, destructor, or type conversion before ‘(’ token car::car(string brand, string color, int cost){ "
我不明白为什么林收到此错误信息,什么是错我的代码? 请帮帮我。
这是我的代码:
这是一个.h文件
#include <iostream>
#include <string>
#pragma once
class car{
public:
car();
car(string,string,int);
int get_cost();
std::string get_brand();
std::string get_color();
private:
std::string newbrand;
std::string newcolor;
int newcost;
};
这是car.cpp文件:
#include "car.h"
car::car(){
this->newcost=0;
this->newbrand="No Brand";
this->newcolor="No color";
}
car::car(string brand, string color, int cost){
newbrand=brand;
newcolor=color;
newcost=cost;
}
int car:: get_cost(){
return newcost;
}
std::string car:: get_brand(){
return newbrand;
}
std::string car:: get_color(){
return newcolor;
}
这是我的主要文件:
#include <iostream>
#include "car.h"
int main(){
car c;
std::cout <<c.get_brand()<< std::endl;
return 0;
}
什么是'string'?你的意思是'std :: string'? –
我将它包含在h文件中,在这里我将这个类放入其中。 –
如果你正在学习C++,你可能会对我们的[很好的C++书籍](http://stackoverflow.com/q/388242/1782465)列表感兴趣。 – Angew