C ++中的构造函数错误

时间:2017-03-17 07:53:01

标签: c++

我刚开始学习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){  "

我不明白为什么我收到此错误消息,我的代码出了什么问题? 请帮帮我。

这是我的代码:

这是一个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;
}

1 个答案:

答案 0 :(得分:5)

string在命名空间std中,所以你必须使用std :: string。

#pragma once 必须在第一行!

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.

com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536