C++ Class program with overloading operators

I dont know what I'm doing wrong. The program is supposed to have three variables: string name, float weight, int age. It supposed to have 3 methods: a constructor, destructor, and void displayDog(in which the name,weight, and age of the dog should be displayed).
In main i am supposed to have two objects: myDog(Spot,5.5,3) & yourDog(Jack,4.5,3). Im supposed to overload these operators: >+, <, ==, <<. and overload the operator << so that cout << yourDog; works. I have met all these requirements and I feel like i'm doing it right so why wont my program run. Help please.
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
74
 // ==================================
// "Dog.h"
#include<iostream>
#include<string>
using namespace std;
#ifndef DOG_H
#define DOG_H
class Dog
{
private:
	string name;
	float weight;
	int age;
public:
	bool operator >=(Dog & dog)
	{
		if (age >=2)
			return true;
		else 
			return false;
	}
	bool operator <(Dog & dog)
	{
		if ( yourDog > myDog)
			return true;
		else 
			return false;
	}
	bool operator ==( Dog & dog)
	{
		if (yourDog == myDog)
			return true;
		else 
			return false;
	}
	bool operator <<(Dog & dog)
	{
		yourDog.displayDog();
	}
	Dog(string,float,int)
	{ weight = 0; age = 0; }
	~Dog();
	void displayDog()
	{ cout << "NAME: " << name << endl <<
	  cout << "WEIGHT: " << weight << endl <<
	  cout << "AGE: " << age << endl; }
};
#endif
// "main.cpp"
#include"Dog.h"

int main()
{
	Dog myDog("Spot",5.5, 3);
	myDog.displayDog();
	Dog yourDog("Jack",4.5,3);
	yourDog.displayDog();

	if (myDog >=2)
		cout << "The dog is at least 2 years old.\n\n";
	else
		cout << "The dog is less than 2 years old.";
	if (yourDog < myDog)
		cout << "Your dog weighs less than my dog.\n\n";
	if (myDog < yourDog)
		cout << "They have the same name.\n\n";
	else 
		cout << "They do not have the same name.\n\n";


	return 0;
}


these are the errors it shows:
------ Build started: Project: HW 9a, Configuration: Debug Win32 ------
main.cpp
c:\users\\documents\visual studio 2010\projects\hw 9a\hw 9a\dog.h(30): error C2065: 'yourDog' : undeclared identifier
c:\users\\documents\visual studio 2010\projects\hw 9a\hw 9a\dog.h(30): error C2065: 'myDog' : undeclared identifier
c:\users\\documents\visual studio 2010\projects\hw 9a\hw 9a\dog.h(37): error C2065: 'yourDog' : undeclared identifier
c:\users\suha\documents\visual studio 2010\projects\hw 9a\hw 9a\dog.h(37): error C2065: 'myDog' : undeclared identifier
c:\users\suha\documents\visual studio 2010\projects\hw 9a\hw 9a\dog.h(44): error C2065: 'yourDog' : undeclared identifier
c:\users\\documents\visual studio 2010\projects\hw 9a\hw 9a\dog.h(44): error C2228: left of '.displayDog' must have class/struct/union
type is ''unknown-type''
c:\users\\documents\visual studio 2010\projects\hw 9a\hw 9a\main.cpp(11): error C2679: binary '>=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
c:\users\suha\documents\visual studio 2010\projects\hw 9a\hw 9a\dog.h(21): could be 'bool Dog::operator >=(Dog &)'
while trying to match the argument list '(Dog, int)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The argument being passed to your overloaded operators is named 'dog', the variables you are using in the body of the operators are called 'yourDog' and 'myDog' neither of which are declared in the scope of those functions.

Your issue is with scope. Variables declared within a function (in your case "main()") are only visible to operations within that function.
so i how would i fix that? Isnt that why i say Dog & dog? because the smaller dog is referring to myDog and yourDog?
When you type 'Dog & dog' what you are doing is telling the compiler to crate the function so that it excepts a reference to a 'Dog' object. When that reference gets passed to that function it does not retain its original name, it is instead always referred to as "dog" with in the scope of that function. EDIT: Further more, because these are class member functions, any data members belonging to the instance of the class for which the function is called are accessible from that function. The guy who wrote the tutorial for this site actually does an infinitely better job at explaining this stuff then I do. If you have time I highly recommend giving it a read: http://www.cplusplus.com/doc/tutorial/

Honestly there is so much wrong with what you are trying to do that I'm not sure where to begin telling you how to fix it. Even if you got the scope of the variables fixed what you have written doesn't make any sense. What is it you are trying to compare here?
Last edited on
closed account (j3Rz8vqX)
In the scope of your method(class function): bool operator <(Dog & dog)

You only have access to these data/members:
1
2
3
4
5
6
private:
	string name;
	float weight;
	int age
//And
Dog & dog//Passed as a variable reference, which is fine. 


Problem occurs, as Computergeek01 suggested:
if ( yourDog > myDog)

What your compiler may be stating:
Who the [what] is youDog and myDog?

I do not have access to those variables or data members.
Your issue is with scope. Variables declared within a function (in your case "main()") are only visible to operations within that function.


You can no longer use the names not included in this class during this time; scope narrowing.

So, what do you do? You use the alias you've given them:
Your alias:Dog & dog

Get your dogs age, which you have access to, in your private data members list and compare it to the objects age; his dog age - or vice versa.

http://www.cplusplus.com/doc/tutorial/classes/

Same applies to your other three methods.

The below isn't correctly implemented in your code:
c:\users\\documents\visual studio 2010\projects\hw 9a\hw 9a\main.cpp(11): error C2679: binary '>=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
bool operator >=(Dog & dog)
Your method took a object called Dog, not an integer value of 2.

Good luck with the rest.
the two dog objects, myDog and yourDog, to see if they have the same name, to compare which dog weighs more, and to see if the dog is at least 2 years old. What else is wrong with it?
[code]
// Attached: HW_9a,9b, 9c
// ===================================
// File: HW_9a.cpp
// ==================================
// Programmer: Jim Smith
// Class: CMPR 121
// ==================================
// "Dog.h"
#include<iostream>
#include<string>
using namespace std;
#ifndef DOG_H
#define DOG_H
class Dog
{
private:
string name;
float weight;
int age;
public:
// this is supposed to overlaod the >= operator to see if the dog's age is at least two years old.
bool operator >=(Dog & dog)
{
if (age >=2)
return true;
else
return false;
}
// supposed to overload < operator to see if your dog weighs less than my dog.
bool operator <(Dog & dog)
{
if ( yourDog > myDog)
return true;
else
return false;
}
// supposed to overload == operator to if myDog and yourDog have the same name.
bool operator ==( Dog & dog)
{
if (yourDog == myDog)
return true;
else
return false;
}
// supposed to overload << operator so that it ouputs the object yourDog when it reads "cout << yourDog << endl;" in main()
bool operator <<(Dog & dog)
{
yourDog.displayDog();
}
// constructor with implementation seeting the values of weight and age equal to zero.
Dog(string,float,int)
{ weight = 0; age = 0; }
~Dog(); // desctructor
// when displayDog is called from main it should first output myDog's name, weight and age and then yourDogs name weight and age.
// Finally it should output yourDog's name, age, & weight when called by "cout << yourDog << endl;
void displayDog()
{ cout << "NAME: " << name << endl <<
cout << "WEIGHT: " << weight << endl <<
cout << "AGE: " << age << endl; }
};
#endif
// "main.cpp"
#include"Dog.h"

int main()
{
// setting the values variable name, age, & weight equal to Spot who weighs 5.5 lbs, and is 3 yrs old.
Dog myDog("Spot",5.5, 3);
// calls the displayDog function to output myDog's name, age, & weight
myDog.displayDog();
// setting the values variable name, age, & weight equal Jack who weighs 4.5 lbs, and is 3 years old.
Dog yourDog("Jack",4.5,3);
// calls the displayDog function to output yourDog's name, age, & weight
yourDog.displayDog();
// using the overload operators that were created in Dog,h to compare things about the two object myDog & yourDog
if (myDog >=2)
cout << "The dog is at least 2 years old.\n\n";
else
cout << "The dog is less than 2 years old.";
if (yourDog < myDog)
cout << "Your dog weighs less than my dog.\n\n";
if (myDog < yourDog)
cout << "They have the same name.\n\n";
else
cout << "They do not have the same name.\n\n";
// using the overloaded << operator so that it ouputs the object yourDog's name, age and weight
cout << yourDog << endl;


return 0;
}

[\code]
does that help any?
closed account (j3Rz8vqX)
Nope, and use the code tag:

Tip 1:
1
2
//Line 15:
if (dog./*Where is my get method/accessor*/ >=2)


Tip 2:
1
2
//Line 24:
if (dog./*Where is my get method/accessor*/ > age)


Tip 3:
1
2
3
4
//Line 15:
bool operator >=(Dog & dog)//This#1
//Line 59:
if (myDog >=2)//This#2 

This#1 and This#2 does not correlate.
// Attached: HW_9a,9b, 9c
// ===================================
// File: HW_9a.cpp
// ==================================
// Programmer: Jim Smith
// Class: CMPR 121
// ==================================
// "Dog.h"
#include<iostream>
#include<string>
using namespace std;
#ifndef DOG_H
#define DOG_H
class Dog
{
private:
string name;
float weight;
int age;
public:

Dog(string,float,int)
{ weight = 0; age = 0; }
~Dog();
void displayDog()
{ cout << "NAME: " << name << endl <<
cout << "WEIGHT: " << weight << endl <<
cout << "AGE: " << age << endl; }
bool operator >=(Dog & dog)
{
if (age >=2)
return true;
else
return false;
}
bool operator <(Dog & dog)
{
if ( weight > weight)
return true;
else
return false;
}
bool operator ==( Dog & dog)
{
if (name == name)
return true;
else
return false;
}

bool operator <<(Dog & dog)
{
{ cout << "NAME: " << name << endl <<
cout << "WEIGHT: " << weight << endl <<
cout << "AGE: " << age << endl; }
}

};
#endif

is that right?^
wait so you're saying for line 15 where i have
bool operator >=(Dog & dog)
i should be doing

bool operator >= (dog.myDog >=2)
closed account (j3Rz8vqX)
bool operator >= (int otherDogsAge)

Edit:
Refer to class templates/overloading operators:
http://www.cplusplus.com/doc/tutorial/templates/
Last edited on
Topic archived. No new replies allowed.