FBHSIE
FBHSIE

Reputation: 55

Getting many errors in C++ header and not sure why

I'm making a car design program. For this program, we are making a class in a header file, and then we will use that header file in our main program by including the file using #include "name of header".

This is my professors header. This is the code in his header. We were instructed to do the header like he is.

/*
Program 14-2 
author - Ray Warren, modified from Language Companion
last updated - 19 July 2013
*/

#include <string>
using namespace std;

class CellPhone 
{ 
  private: 
  // Field declarations 
  string manufacturer; 
  string modelNumber; 
  double retailPrice; 

  public: 
  // Constructor 
  CellPhone(string manufact, string modNum, double retail) 
  {
    manufacturer = manufact; 
    modelNumber = modNum; 
    retailPrice = retail; 
  } 

  // Member functions 
  void setManufacturer(string manufact) 
  { 
    manufacturer = manufact; 
  } 

  void setModelNumber(string modNum) 
  { 
    modelNumber = modNum; 
  } 

  void setRetailPrice(double retail) 
  { 
    retailPrice = retail; 
  } 

  string getManufacturer() 
  { 
    return manufacturer; 
  }

  string getModelNumber() 
  { 
    return modelNumber; 
  } 

  double getRetailPrice() 
  {
    return retailPrice; 
  } 
};  //end class

This is the program he used the header file in (as you see he included the header file).

/*
Program14-2
author - Ray Warren, modified from Language Companion
last updated - 19 July 2013
*/

#include <iostream>
#include <cstdlib>

#include "CellPhone.hpp"
using namespace std;


int main()
{
  // Create a CellPhone object and initialize its
  // fields with values passed to the constructor.
  CellPhone myPhone("Motorola", "M1000", 199.99);

  // Display the values stored in the fields.
  cout << "The manufacturer is "
  << myPhone.getManufacturer() << endl;
  cout << "The model number is "
  << myPhone.getModelNumber() << endl;
  cout << "The retail price is "
  << myPhone.getRetailPrice() << endl;

  system("Pause");
  return 0;
} //end main

This is my header file containing my class.

#include <string>
using namespace std;

Car(int ym, string mk)
{
    yearModel=ym;
    make=mk;
    speed=0;
}

void setYearModel(int ym)
{
    yearModel=ym;
}

void setMake (string mk)
{
    make=mk;
}

int returnYearModel()
{
    return yearModel;
}

string returnMake()
{
    return make;
}

int returnSpeed()
{
   return speed;
}

void accelerate()
{
    speed += 5;
}

void brake()
{

This is my main program with me attempting to include the header file into my main program. When I tried to compile this, my header file popped up in a new code blocks IDE tab, and gave me this list of errors.

http://prntscr.com/bzu4x5 <--------- list of errors

I have no idea what I'm doing wrong. From what I see, I copied my professor exactly as he told us to, and I'm still getting errors.

Does anyone have any ideas of what's causing this massive list of errors?

#include <string>
#include <iostream>
#include "Car header.h"

int main()
{

}

Upvotes: 2

Views: 99

Answers (2)

Duly Kinsky
Duly Kinsky

Reputation: 996

You did not actually define a class in you header file. Notice that in your teacher's header file, he has:

class Cellphone // -> this is what you do not have
{

    private: 

    // Field declarations 
    string manufacturer; 
    string modelNumber; 
    double retailPrice;

    public:
    //constructor function
    CellPhone(string manufact, string modNum, double retail); 

    // Member functions 
    void setManufacturer(string manufact); 
    void setModelNumber(string modNum); 
    void setRetailPrice(double retail); 
    string getManufacturer(); 
    string getModelNumber(); 
    double getRetailPrice(); 
}

You should have you fields and member functions inside your class. Hope that helps

Upvotes: 2

Your "header file" contains the definitions of the class member functions. It should actually be a .cpp file. What you are missing is a definition of the class itself, and declarations of the member functions.

Note that your professor's sample header file defines the member functions inside the class definition. This is actually poor practise, but he may not have got round to teaching you how do define functions out of line yet.

If you are going to define the functions out of line, you will also need to change the functions to some thing like:

std::string Car::returnMake()
{
    return make;
}

Note: "using namespace std" is also poor practise. Professional C++ code tends to be explicit, and use the "std::" prefix. Just get in the habit and save yourself hours of pain.

Upvotes: 2

Related Questions