Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ class is not recognizing string data type

I'm working on a program from my C++ textbook, and this this the first time I've really run into trouble. I just can't seem to see what is wrong here. Visual Studio is telling me Error: identifier "string" is undefined.

I separated the program into three files. A header file for the class specification, a .cpp file for the class implementation and the main program file. These are the instructions from my book:

Write a class named Car that has the following member variables:

year. An int that holds the car's model year.

make. A string that holds the make of the car.

speed. An int that holds the car's current speed.

In addition, the class should have the following member functions.

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.

Accessors. Appropriate accessor functions should be created to allow values to be retrieved from an object's year, make and speed member variables.

There are more instructions, but they are not necessary to get this part to work.

Here is my source code:

// File Car.h -- Car class specification file
#ifndef CAR_H
#define CAR_H
class Car
{
private:
    int year;
    string make;
    int speed;
public:
    Car(int, string);
    int getYear();
    string getMake();
    int getSpeed();
};
#endif


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

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

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

string Car::getMake()
{
    return make;
}

int Car::getSpeed()
{
    return speed;
}


// Main program
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;

int main()
{

}

I haven't written anything in the main program yet, because I can't get the class to compile. I've only linked the header file to the main program. Thanks in advance to all who take the time to investigate this problem for me.

like image 473
reallythecrash Avatar asked May 20 '10 22:05

reallythecrash


People also ask

Is string a built in type in C++?

This is not a built-in type, but it behaves like one in its most basic usage. String values must be surrounded by double quotes: To use strings, you must include an additional header file in the source code, the <string> library: You will learn more about strings, in our C++ Strings Chapter.

Is there a way to write a string without doing std::string?

Doing std::string will work but is rather cumbersome to write each time. To make it work for all string without doing std:: each time, simply put these two lines of code at the top of your header file:

How do I use strings in C++?

String values must be surrounded by double quotes: To use strings, you must include an additional header file in the source code, the <string> library: You will learn more about strings, in our C++ Strings Chapter.

Can I put a string in the header of a class?

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.


1 Answers

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)

{

}
like image 158
radman Avatar answered Sep 28 '22 10:09

radman