C++ class is not recognizing string data type

You have forgotten to #include the string header and you need to fully qualify your usage of string to std::string, the amended code should be.

// File Car.h -- Car class specification file
#ifndef CAR_H
#define CAR_H

#include <string>

class Car
{
private:
    int year;
    std::string make;
    int speed;
public:
    Car(int, string);
    int getYear();
    std::string getMake();
    int getSpeed();
};
#endif


// File Car.cpp -- Car class function implementation file
#include "Car.h"

// Default Constructor
Car::Car(int inputYear, std::string inputMake)
{
    year = inputYear;
    make = inputMake;
    speed =  0;
}

// Accessors
int Car::getYear()
{
    return year;
}

You could put using namespace std; at the top of Car.cpp and that would let you use string without the std:: qualifier in that file. However DON'T put one of these in the header because it is very bad mojo.

As a note you should always include everything that the class declaration needs in the header before the class body, you should never rely on a client source file including a file (like <string>) before it includes your header.

With regard to this part of your task:

Constructor. The constructor should accept the car's year and make as arguments and assign these values to the object's year and make member variables. The constructor should initialize the speed member variable to 0.

The best practice is to use an initializer list in the constructor, like so:

// Default Constructor
Car::Car(int inputYear, string inputMake)
  : year(inputYear),
    make(inputMake),
    speed(0)

{

}

I suspect you need your #include <string> at the top of the file, above where you use the string type.


#include <string> does NOT work. You should put using namespace std ; above the code.


You should use the fully qualified name std::string, or you forgot to include the <string> header. Or both.