Basic Inheritance Class 1 ERROR Code Frustrating !

Whats up guys ,
I have to design a a motorbike class today using inheritance from a vehicle class that i had previously designed. My goals are
1) to design a vehicle class with speed and turn rate - DONE
2) From this basic class design a motorbike with Speed (Inherited from motor class) and Type of bike (Char type)


I keep getting one error in my .cpp file in line 18 saying "Motorbike was not declared in this scope"

here is my Vehicle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;

class vehicle {

public: //declare functions

	int getSpeed();
	double getTurnTime();
	vehicle (); //contructor

private:
	int Speed; //declare variables
	double TurnTime;
};


vehicle::vehicle(){ //constructor
	Speed = 0; //initialize  variables
	TurnTime = 1.3;

}

int vehicle::getSpeed(){
	return Speed;
}

double vehicle::getTurnTime(){
	return TurnTime;
}




 


here is my motorbike.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#ifndef VEHICLE_H
#define VEHICLE_H
#include <iostream>
#include "vehiclee.h"

#define SIZEOFNAME 7


using namespace std;

class motorbike : public vehicle {

public: //declare functions

	int getWheels();
    char* getBikeType(); //function
    char BikeType[7]; //array of bike types

	motorbike (); //contructor

private:
	int Wheels; //declare variables
    char* BikeType[7];
};



motorbike::motorbike(){ //constructor
	Wheels = 2; //initialize  variables
	BikeType = "Suzuki";

}

int motorbike::getWheels(){
	return Wheels;
}

 char* motorbike::getBikeType(){
	return BikeType;
}

#endif // VEHICLE_H


i know for sure theres something wrong with line 22 were i try to display the bike name

finally here is my .cpp file
1
2

Updated code below 


Last edited on
Your include guards are messed up.

vehicle.h does not have include guards.
motorbike.h has the wrong include guards.
Your .cpp files should NOT have include guards.

motorbike.h lines 18, 24: BikeTypes is declared twice (differently).

motorbike.h line 32: You can't assign a C string like this.

.cpp line 11: Your use of using is inconsistent. You have using namespace std; in your header files. This line is useless.

.cpp line 15: Your class and object names are the same.

.cpp lines 19-20: You refer to an object named motorbike, but that's the name of your class. You have no motorbike object.

are you saying i need this
1
2
3
4
5
#ifndef VEHICLE_H
#define VEHICLE_H
#include <iostream>
#include "vehiclee.h"


in my vehiclee.h file ?
In my cpp file line 15 vehicle is an object

why does this not work in line 19-20 ? Could you please refer to my motorbike.h and show me how ive no motorbike object ?
You have a motorbike class defined in motorbike.h but there's no motorbike object created in your main.cpp. Line 15 is trying to create a vehicle object, not a motorbike object. Motorbikes may be vehicles and inherit vehicle properties, but vehicles are not necessarily motorbikes.

You don't need to #include "vehicle.h" in your vehicle.h file, just the #ifndef, #define and #endif. I don't see that you need to include iostream anywhere besides the main.cpp where you using the output functions.
Cheers for the help guys!

ok so i changed me main.cpp to
 
See below 




Im currently having trouble with my character array and i thought i will learn that before going on to the tougher stuff. i keep getting a "Invalid array assignment" error in my motorbike.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef MOTORBIKE_H
#define MOTORBIKE_H

#include <iostream>
#include "vehiclee.h"

#define SIZEOFNAME 7


using namespace std;

class motorbike : public vehicle {

public: //declare functions

	int getWheels();
    char getBikeType(); //function

    motorbike (); //contructor

private:
	int Wheels; //declare variables
    char BikeType[7];
};



motorbike::motorbike(){ //constructor
	Wheels = 2; //initialize  variables
    BikeType = "Suzuki";

}

int motorbike::getWheels(){
	return Wheels;
}

 char motorbike::getBikeType(){
	return BikeType;
}

#endif // MOTORBIKE_H


Could someone show me the correct way of assigning an array ?
Last edited on
main.cpp line 9-10: vehicle is not instantiated.

As for assigning an array. BikeType is currently a C style string, so you would need to use a C library call to copy a value to it.
 
  strcpy (BikeType, "Suzuki");


It's best not to mix C and C++ styles.
The better way to do this is:
1
2
3
4
5
6
7
8
9
10
11
12
#include <string>  // Use the C++ string library
...
    // Replace line 23 in header with: 
   string BikeType;
...
  // Replace line 30 with
  BikeType = "Suzuki";
...
//  And your getter needs to change
string motorbike::getBikeType()
{   return BikeType;
}



Thank you AbstractionAnon, after a bit of reading up i now understand the string way to display characters.

In line 9-10 i instantiated vehicle but im still getting errors. There is something very wrong here could someone show me how to fix this.

Please excuse me i am new to C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "vehiclee.h" //include the declaration of the class
#include "Motorbike.h"
#include <iostream>
using namespace std;

int main() {

	vehicle vehicle ; //object of the class vehicle
	cout << "\nThe speed is: " << vehicle.getSpeed(); //shows speed on screen
	cout << "\nThe turn time is: " << vehicle.getTurnTime();

	motorbike motorbike;
	cout << "\n The Number Of Wheels is: " << motorbike.getWheels;
	cout << "\n The Bike Type is: " << motorbike.getBikeType();

	return 0;
}
line 13 - just missing the () for the getWheels function
Hey Guys ! appreciate you`s helping me in my journey of learning c++ programming.

One more question however . Lets say i wanted the user to be able to enter there own number for the motorbike attributes. How would i go about doing this ? I haven't tried it yet myself im just trying to get knowledge beforehand .

Would it be as simple as changing the get functions into ask
1
2
3
4
5
6
7
8
class motorbike : public vehicle {

public: //declare functions

	int askWheels();
    char askBikeType(); //function

    motorbike (); //contructor 


my question is were would i put my
1
2
cout << "\nEnter The Number Of Wheels is: " <<endl;
cin << motorbike.Wheels << endl;


Any suggestions on how i should go about modifying this code ?
Last edited on
You created an askWheels function prototype. This code goes in your implementation of that function.

A few of problems with line 2 in your second snippet.
1) The cin operator is the wrong direction. Input operations use >>.
2) You don't use endl with input operations.
3) Since this code goes in a member function, you don't qualify access to the member variable.

1
2
3
4
5
int motorbike::askWheels()
{   cout << "\nEnter The Number Of Wheels: " <<endl;
    cin >> Wheels;
    return Wheels;
}



Last edited on
Topic archived. No new replies allowed.