Join 136,106 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,720 people online right now. Registration is fast and FREE... Join Now!
Hello! I'm new to C++ and taking a level two course on OOD. I've written a program that asks for the name, breed, and age of a dog in a method and prints the name, age, breed, and license fee of the dog in another method all in a class named Dog. I've got most of the code, I think, but I'm getting an error about overloading a couple functions and since we haven't learned that was wondering if anyone knew any other way to do this. Here are my errors:
CODE
(45) : error C2511: 'void Dog::setDogInfo(void)' : overloaded member function not found in 'Dog' (18) : see declaration of 'Dog' (87) : error C2761: 'void Dog::printDogInfo(void) const' : member function redeclaration not allowed (88) : error C2447: '{' : missing function header (old-style formal list?) (20) : error C2761: 'bool Dog::done' : member function redeclaration not allowed (20) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Here is my header file (Dog.h):
CODE
#ifndef H_Dog #define H_Dog
#include <iostream>
using namespace std;
class Dog //Dog class { public: static int licenseFee; //static variable for license fee char answer; //variable for verification of dog information bool done; //variable for exception throw char dName; char dBreed; int dAge;
void Dog::setDogInfo(char dName, char dBreed, int dAge); //Function prototype to set dog's name, age, and breed //Postcondition: dogName=dName dogBreed=dBreed dogAge=dAge void Dog::printDogInfo() const; //Function to print "Your dog's name is dogName. The dog's breed //is dogBreed. Age is dogAge years. The license fee is $12.25."
private: char dogName; char dogBreed; int dogAge; };
void Dog::setDogInfo() //Function to enter dog's name, breed, and age { do { try { cout << "Please enter your dog's name and press enter: "; cin >> dName; cout << endl;
cout << "Please enter your dog's breed and press enter: "; cin >> dBreed; cout << endl;
cout << "Please enter your dog's age in years and press enter: "; cin >> dAge; cout << endl;
cout << "Is the following information correct?" << endl; cout << endl; cout << "Your dog's name is " << dName << ". The dog's breed is " << dBreed << ". Age is " << dAge << " years. " << endl; cout << "Enter Y for Yes or N for No: "; cin >> answer;
void displayMenu(); //Function prototype to display main menu
int main() { bool Dog::done = false; //variable for exception throw bool done = false; //variable for menu choice selection verification char Dog::answer = "Y"; //variable for verification of information int choice = 0; //variable to hold menu choice double Dog::licenseFee = 12.25; //static variable declaration for license fee string str = "Please enter again."; //string variable to hold error message char Dog::dName = "Rex"; char Dog::dBreed = "Beagle"; int Dog::dAge = 5;
if (choice == 1) { Dog::setDogInfo(); } else if (choice == 2) { Dog::printDogInfo(); } else if (choice == 3) { system("cls"); } system ("pause");
return 0; }
void displayMenu() //Function to display main menu { cout << "Welcome to the Dog Program" << endl; cout << "Please enter the number of your selection" << endl; cout << "1) Enter dog information" << endl; cout << "2) Print dog information" << endl; cout << "3) Exit the program" << endl; }
Any help would be greatly appreciated. Been working on this one quite awhile. Thanks!
First of all, check out your dog class. You will see you don't have a function with the function signature void Dog::setDogInfo(void). You have one which takes 2 chars and an int, but not one that is void. So that solves error number one.
Secondly where you define printDogInfo() you have a semicolon after the "const" which shouldn't be there. That will probably clear up the 2447 error and I think the first 2761 error about redeclaration.
Lastly take a look at your variable "done" you are redefining it meaning you are declaring it again when it is already declared.
First of all, check out your dog class. You will see you don't have a function with the function signature void Dog::setDogInfo(void). You have one which takes 2 chars and an int, but not one that is void. So that solves error number one.
Secondly where you define printDogInfo() you have a semicolon after the "const" which shouldn't be there. That will probably clear up the 2447 error and I think the first 2761 error about redeclaration.
Lastly take a look at your variable "done" you are redefining it meaning you are declaring it again when it is already declared.
So that should take care of a few of them.
Decided to take out the exception throwing...we didn't have to have that anyway. Still getting a couple errors that I'm not understanding though. Says that local function definitions are illegal...where do I put them then? Sorry to sound so dumb. Still not very experienced in this. Any help would be appreciated.
CODE
#ifndef H_Dog #define H_Dog
#include <iostream>
using namespace std;
class Dog //Dog class { public: static int licenseFee; //static variable for license fee char dName; char dBreed; int dAge; char answer; void Dog::setDogInfo(char dName, char dBreed, int dAge); //Function prototype to set dog's name, age, and breed //Postcondition: dogName=dName dogBreed=dBreed dogAge=dAge void Dog::printDogInfo() const; //Function to print "Your dog's name is dogName. The dog's breed //is dogBreed. Age is dogAge years. The license fee is $12.25."
private: char dogName; char dogBreed; int dogAge; };
void Dog::setDogInfo(char dName, char dBreed, int dAge) //Function to enter dog's name, breed, and age {
cout << "Please enter your dog's name and press enter: "; cin >> dName; cout << endl;
cout << "Please enter your dog's breed and press enter: "; cin >> dBreed; cout << endl;
cout << "Please enter your dog's age in years and press enter: "; cin >> dAge; cout << endl;
cout << "Is the following information correct?" << endl; cout << endl; cout << "Your dog's name is " << dName << ". The dog's breed is " << dBreed << ". Age is " << dAge << " years. " << endl; cout << "Enter Y for Yes or N for No: "; cin >> answer;
void displayMenu(); //Function prototype to display main menu
int main() { bool done = false; //variable for menu choice selection verification int choice = 0; //variable to hold menu choice double Dog::licenseFee = 12.25; //static variable declaration for license fee string str = "Please enter again."; //string variable to hold error message char Dog::dName = "Rex"; char Dog::dBreed = "Beagle"; int Dog::dAge = 5;
if (choice == 1) { Dog::setDogInfo(); } else if (choice == 2) { Dog::printDogInfo(); } else if (choice == 3) { system("cls"); } system ("pause");
return 0; }
void displayMenu() //Function to display main menu { cout << "Welcome to the Dog Program" << endl; cout << "Please enter the number of your selection" << endl; cout << "1) Enter dog information" << endl; cout << "2) Print dog information" << endl; cout << "3) Exit the program" << endl; }
you're missing a closing brace at the end of your setDogInfo() function. think that may be it - as it stands, you're defining a function printDogInfo() locally within setDogInfo().
You are still trying to call the method Dog::setDogInfo(); in Main.cpp and you haven't defined this. It is not the same thing as the one definition where you have two chars and an int in the signature. The call must match the signature exactly. You have to pass values to setDogInfo(). You can't just call the function without giving it data. You have defined it to take a name, breed and age.
You also took out the error catching, but you are still attempting to throw the error. So you might want to correct that.
You are also missing a closing curly brace for void Dog::setDogInfo(char dName, char dBreed, int dAge)
You are still trying to call the method Dog::setDogInfo(); in Main.cpp and you haven't defined this. It is not the same thing as the one definition where you have two chars and an int in the signature. The call must match the signature exactly. You have to pass values to setDogInfo(). You can't just call the function without giving it data. You have defined it to take a name, breed and age.
You also took out the error catching, but you are still attempting to throw the error. So you might want to correct that.
You are also missing a closing curly brace for void Dog::setDogInfo(char dName, char dBreed, int dAge)
So correct those and we can continue.
Trying to simplify this a bit...I understand now what you were saying about the method calling. Seems like I make that mistake a lot when I code I've noticed. I think I cleared this up.
void displayMenu(); //Function prototype to display main menu
int main() { int choice = 0; //variable to hold menu choice double Dog::licenseFee = 12.25; //static variable declaration for license fee string str = "Please enter again."; //string variable to hold error message char Dog::dName = "Rex"; char Dog::dBreed = "Beagle"; int Dog::dAge = 5;
if (choice == 1) { Dog::setDogInfo(Rex, Beagle, 5); } else if (choice == 2) { Dog::printDogInfo(); } else if (choice == 3) { system("cls"); } system ("pause");
return 0; }
void displayMenu() //Function to display main menu { cout << "Welcome to the Dog Program" << endl; cout << "Please enter the number of your selection" << endl; cout << "1) Enter dog information" << endl; cout << "2) Print dog information" << endl; cout << "3) Exit the program" << endl; }
header file:
CODE
#ifndef H_Dog #define H_Dog
#include <iostream>
using namespace std;
class Dog //Dog class { public: static int licenseFee; //static variable for license fee char dName; char dBreed; int dAge; void Dog::setDogInfo(char dName, char dBreed, int dAge); //Function prototype to set dog's name, age, and breed //Postcondition: dogName=dName dogBreed=dBreed dogAge=dAge void Dog::printDogInfo() const; //Function to print "Your dog's name is dogName. The dog's breed //is dogBreed. Age is dogAge years. The license fee is $12.25."
private: char dogName; char dogBreed; int dogAge; };
void Dog::setDogInfo(char dName, char dBreed, int dAge) //Function to enter dog's name, breed, and age { cout << "Please enter your dog's name and press enter: "; cin >> dName; cout << endl;
cout << "Please enter your dog's breed and press enter: "; cin >> dBreed; cout << endl;
cout << "Please enter your dog's age in years and press enter: "; cin >> dAge; cout << endl;
cout << "Your dog's name is " << dName << ". The dog's breed is " << dBreed << ". Age is " << dAge << " years. " << endl;
Now I have to ask, are you wanting to access this dog object statically or are you attempting to create a dog, give it a name and such? The reason I ask is because some of your variables in your Dog class is created statically and some are just public variables, but then you have these private variables.
Is this an assignment? If so, can you post the assignment details here?
Now I have to ask, are you wanting to access this dog object statically or are you attempting to create a dog, give it a name and such? The reason I ask is because some of your variables in your Dog class is created statically and some are just public variables, but then you have these private variables.
Is this an assignment? If so, can you post the assignment details here?
I already submitted what progress I've made with this (prof won't take late work, but gives credit for what we do have done if incomplete), but I still want to figure the whole thing out. I just signed up for tutoring, so hopefully that will help me, but yes, this is for an assignment.
"3. Write a class called Dog which keeps track of the dog's name, breed, age, and license fee. The license fee will be a set amount: $12.25, since you only need one copy of this in memory, make it static. Include a method which allows you to set the dog's name, breed and age. Include another method which displays the dog's name, breed, age and license fee. Write code to test your class."
I'm going to go back over the chapter and see if I can't figure this out (and then call it a night...my 2-yr-old has run me ragged today!). Thanks for all of your help!