Virtual functions problem when compiling

This the code that makes up my program. There are 3 header files and a single .cpp file. When I build the program however I get the errors:

I have 6 errors for the '<<' operator that you will see if you compile it also. I was wondering what I am doing wrong that is causing these errors. Much appreciated

//Car.h
#ifndef Car_h
#define Car_h
#include "CarbonFootprint.h"
#include <iostream>

class car : public CarbonFootprint
{
public:
int carbonfootprint(void)
{
return miles * mpg;
}

};

#endif /* Car_h */

//Building.h
#ifndef Building_h
#define Building_h
#include "CarbonFootprint.h"
#include <iostream>

class building : public CarbonFootprint
{
public:
int carbonfootprint(void)
{
return elecUsed * gasUsed;
}

};

#endif /* Building_h */

//Bicycle.h
#ifndef Bicycle_h
#define Bicycle_h
#include "CarbonFootprint.h"
#include <iostream>

class bicycle : public CarbonFootprint
{
public:
int carbonfootprint(void)
{
return fuelUsed * milesCovered;
}

};

#endif


//CarbonFootprint.h
#ifndef CarbonFootprint_h
#define CarbonFootprint_h
#include <iostream>
using namespace std;

class CarbonFootprint
{
protected:
int miles;
int mpg;

int elecUsed;
int gasUsed;

int fuelUsed;
int milesCovered;

public:
void set_values_car(int a, int b)
{
miles = a; mpg = b;
}

void set_values_building(int a, int b)
{
elecUsed = a; gasUsed = b;
}

void set_values_bicycle(int a, int b)
{
fuelUsed = a; milesCovered = b;
}

virtual int carbonfootprint(void) = 0;

void printcar()
{
cout << this->printcar() << '\n';
}

void printbicycle()
{
cout << this->printbicycle() << '\n';
}

void printbuilding()
{
cout << this->printbuilding() << '\n';
}


};

#endif


// Journal Task 8 Final.cpp

#include "stdafx.h"
#include "Car.h"
#include "Bicycle.h"
#include "Building.h"
#include "CarbonFootprint.h"
#include <iostream>

int main()
{
car Car1;
bicycle Bicycle1;
building Building1;

CarbonFootprint * CarbonF1 = &Car1;
CarbonFootprint * CarbonF2 = &Bicycle1;
CarbonFootprint * CarbonF3 = &Building1;

CarbonF1->set_values_car(5, 20);
CarbonF2->set_values_bicycle(5, 2);
CarbonF3->set_values_building(5, 10);

CarbonF1->printcar();
CarbonF2->printbicycle();
CarbonF3->printbuilding();

return 0;
}

Last edited on
I'm not sure what you mean to have happen in the print functions. It looks like you're trying to output the result of a function (and the same function you've originally called) that is void and doesn't return anything. Did you want to print out some of the values of the data members specific to a car?

Edit: or call the carbonfootprint function?

1
2
3
4
void printcar()
{
cout << this->printcar() << '\n';
}
Last edited on
OP: Seems you are carrying on irregardless since previous thread (http://www.cplusplus.com/forum/general/203113/) despite a strong case for not having public inheritance, particularly with virtual functions, at all in this situation

Quoting Stephen Prata, C++ Primer Plus (5th ed)
Nothing in the C++ language prevents you from using public inheritance to model has-a, is implemented-as-a, or uses-a relationships. However, doing so usually leads to programming problems


In your case the best approach would be to use containment i.e.
1
2
3
4
5
6
7
8
9
class CarbonFootprint {...};
class Car
{
	private:
		CarbonFootprint cfCar;
		...
	...
};
//and so on for the other classes  

https://isocpp.org/wiki/faq/private-inheritance#priv-inherit-vs-compos

Topic archived. No new replies allowed.