-2

I just started to learn cpp. I created a class with 2 constructors. When I run this command on ubuntu: g++ -Wall -g main.cpp car.cpp -o a I'm getting an error massage:

> "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){  "

I don't understand why Im getting this error message, what is wrong with my code? Please help me.

This is my code:

This is an h file

#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;

};

This is car.cpp file:

#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;
} 

This is my main file:

#include <iostream>
#include "car.h"

int main(){
     car c;
     std::cout <<c.get_brand()<< std::endl;
    return 0;
}
Hubi
  • 1,629
  • 15
  • 20
Elior Sastiel
  • 1,074
  • 2
  • 10
  • 21

1 Answers1

5

string is in namespace std, so you have to use std::string.

And #pragma once must be in the first line!

#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;

};
Hubi
  • 1,629
  • 15
  • 20