我刚刚开始学习cpp.我创建了一个包含2个构造函数的类.当我在ubuntu上运行此命令时: g++ -Wall -g main.cpp car.cpp -o a
我收到错误按摩:
> "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){ "
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我收到此错误信息,我的代码有什么问题?请帮我.
这是我的代码:
这是一个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;
};
Run Code Online (Sandbox Code Playgroud)
这是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;
}
Run Code Online (Sandbox Code Playgroud)
这是我的主要文件:
#include <iostream>
#include "car.h"
int main(){
car c;
std::cout <<c.get_brand()<< std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
string在命名空间std中,所以你必须使用std :: string.
并且#pragma一次必须在第一行!
#pragma once
#include <iostream>
#include <string>
class car{
public:
car();
car(std::string, std::string, int);
int get_cost();
std::string get_brand();
std::string get_color();
private:
std::string newbrand;
std::string newcolor;
int newcost;
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
190 次 |
| 最近记录: |