13

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.

reallythecrash
  • 133
  • 1
  • 1
  • 5
  • 3
    Im quite jealous you get to use an ide to learn c++ or coding in general – Woot4Moo May 20 '10 at 22:55
  • I'm simply using the current tools of the time that are available to me, just as I'm sure you did when you learned. I can imagine it was difficult learning this without a program pointing out your errors. I imagine it would be equal to climbing Mt. Everest before boots were available. – reallythecrash May 21 '10 at 04:27
  • Also, consider changing the declaration from `Car::Car(int inputYear, string inputMake)` to `Car::Car(const int inputYear, const std::string & inputMake)`. See [here](http://stackoverflow.com/questions/2878972/why-do-we-have-to-send-the-const-type-reference-instead-of-just-the-types-nam/2880783#2880783) for an explanation. – utnapistim May 21 '10 at 09:15
  • +1 for a well asked question with all the necessary information :) – jcoder Nov 13 '12 at 15:36

6 Answers6

12

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)

{

}
radman
  • 17,675
  • 11
  • 42
  • 58
  • Thank you radman. I spent all day trying to figure this out. Fully qualifying the data type with std::string and adding #include to the Car.h file did the trick. The book I'm using never used strings in any of the example classes or constructors, yet they ask a review question using string. I haven't read anything about initializer lists, but there is a chapter called "more about classes and object-oriented programming" later in the book. Maybe I will find it there. Thanks again. – reallythecrash May 21 '10 at 04:14
9

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

sth
  • 222,467
  • 53
  • 283
  • 367
  • Thanks for answering. Yes, both are needed. Though I'm still unclear as to why. Maybe my book will explain it in more detail later. I'm only at page 388 of 1095. :) – reallythecrash May 21 '10 at 04:20
4

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

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • Thank you Dan. Using #include in the header file worked, but as a side note I still had to fully qualify any usage of the string data type with std::string. I appreciate your input. – reallythecrash May 21 '10 at 04:18
1

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

Sabri Meviş
  • 2,231
  • 1
  • 32
  • 38
0

you need to add these 2 lines:

#include < string >;

using std::string;

jetpack
  • 169
  • 1
  • 9
  • Seems your point is included in [the accepted answer](https://stackoverflow.com/a/2878687/5376789), why do you post another answer? – xskxzr Apr 23 '19 at 04:30
-3

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:

#include < string >;

using namespace std;

And you're good to go!

il_guru
  • 8,383
  • 2
  • 42
  • 51
mike
  • 21
  • Welcome to StackOverflow! Please use the "answer" space for answers; this is a comment. You wil be able to post comments once you have gained enough reputation points. – S.L. Barth is on codidact.com Nov 13 '12 at 14:55
  • I think it's worth pointing out that this isn't such a good idea when writing library code, or a massive codebase that might have it's own class called `string` somewhere. – hcarver Nov 13 '12 at 15:16
  • Gotta love how brutal stack overflow is! :/ – Ian Wise May 02 '16 at 15:32