inheritance wont work << "Pet not a struct or class name " <<endl

I continue to receive and error in the base class declaration of "Pet" not being a class or struct, when i have included the parent classes header file.
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

#include <iostream>
using namespace std;

class Pet { 
private:
	int age;
	int weight;
	int id_number;
	string species;
	string name;
	string color;
public:
	void setAge(int new_age);
	int getAge();

	void setWeight(int new_weight);
	int getWeight();

	void setIdNumber(int new_id_number);
	int getIdNumber();

	void setSpecies(string new_species);
	string getSpecies();

	void setName(string new_name);
	string getName();

	void setColor(string new_color);
	string getColor();
};
	
	void Pet::setAge(int new_age) {
		age = new_age;
	}
	int Pet::getAge() {
		return age;
	}

	void Pet::setWeight(int new_weight) {
		weight = new_weight;
	}
	int Pet::getWeight() {
		return weight;
	}

	void Pet::setIdNumber(int new_id) {
		id_number = new_id;
	}
	int Pet::getIdNumber() {
		return id_number;
	}

	void Pet::setSpecies(string new_species) {
		species = new_species;
	}
	string Pet::getSpecies() {
		return species;
	}

	void Pet::setName(string new_name) {
		name = new_name;
	}
	string Pet::getName() {
		return name;
	}

	void Pet::setColor(string new_color) {
		color = new_color;
	}
	string Pet::getColor() {
		return color;
	}


1
2
3
4
5
6
7
8

#include "Pet.h"

class Dog: public Pet { // ERROR: PET NOT A STRUCT OR CLASS NAME ???
                               


}
If that is the full code, adding a ; after your Dog class might fix some problems.
hi James, updated code, & still no luck . I don't have to write the class declaration for dog, inside the pet class declaration do i? I thought that was whole point in having #include <ParentName.h>.
This code looks right, you must be doing something else wrong. Are you sure your files are in the same directory? It does not complain about #include "Pet.h" itself?

Also, it's generally a bad idea to say "using namespace" inside a header file, since any file which includes that header will be forced to use that namespace as well.
Thx BlackSheep, i'm using Visual C++ and the code is stored in "Projects". I have included the Pet.h header file inside the same project as the Dog.h file. I will re look at the Pet.h and i will cleanup "namespace" problem re-post code. GOING CRAZY, so simple it hurts !
Ok so i confirmed that if i put the Dog class declaration inside Pet.h, outside of the Pet class declaration everything works fine. The problem is why doesn't the proper way work ?? BTW "BlackSheep", if i remove the "namespace" i get an error on all string variables as being undefined. I still need help
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
using namespace std;

class Pet { 
private:
	int age;
	int weight;
	int id_number;
	string species;
	string name;
	string color;
public:
	void setAge(int new_age);
	int getAge();

	void setWeight(int new_weight);
	int getWeight();

	void setIdNumber(int new_id_number);
	int getIdNumber();

	void setSpecies(string new_species);
	string getSpecies();

	void setName(string new_name);
	string getName();

	void setColor(string new_color);
	string getColor();
};

	void Pet::setAge(int new_age) {
		age = new_age;
	}
	int Pet::getAge() {
		return age;
	}

	void Pet::setWeight(int new_weight) {
		weight = new_weight;
	}
	int Pet::getWeight() {
		return weight;
	}

	void Pet::setIdNumber(int new_id) {
		id_number = new_id;
	}
	int Pet::getIdNumber() {
		return id_number;
	}

	void Pet::setSpecies(string new_species) {
		species = new_species;
	}
	string Pet::getSpecies() {
		return species;
	}

	void Pet::setName(string new_name) {
		name = new_name;
	}
	string Pet::getName() {
		return name;
	}

	void Pet::setColor(string new_color) {
		color = new_color;
	}
	string Pet::getColor() {
		return color;
	}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 #include "Pet.h"
#include <iostream> 

using namespace std;

class Dog: public Pet { // ERROR PET NOT A STRUCT OR CLASS NAME ?????
private:
	string breed;
public:
	string getBreed();
	string talk();
};

	string Dog::getBreed() {
		return breed;
	}
	
	string Dog::talk() {
		cout << "Woof Woof" << endl;
	}
	
FIXED !! The Pet.h was saved in a different project, i corrected directory and WHALAA :-)
You're sure your compiler didn't complain about #include "Pet.h" ? MVC++ usually warns you when it can't find a header file, and if it did find it Pet should have been declared.
Also, about the namespaces, just prepend std:: to all the objects or functions that come from the std namespace:
1
2
std::string s;
std::cout<<std::endl;
Thanks for more knowledge :-0 i'll take as much as you can give :-))
Topic archived. No new replies allowed.