Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 793531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:10:04+00:00 2026-05-14T22:10:04+00:00

I’m working on a program from my C++ textbook, and this this the first

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:10:05+00:00Added an answer on May 14, 2026 at 10:10 pm

    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)
    
    {
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.