Welcome to Dream.In.Code
Getting C++ Help is Easy!

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!




Function overload error

 
Reply to this topicStart new topic

Function overload error

ibleedart
3 Oct, 2007 - 05:48 PM
Post #1

New D.I.C Head
*

Joined: 3 Oct, 2007
Posts: 6


My Contributions
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;

            if (answer == "N")
                throw str;

            dogName = dName;
            dogBreed = dBreed;
            dogAge = dAge;
            done = true;
        }
        catch (string messageStr)
        {
            cout << messageStr << endl;
            cin.clear();
            cin.ignore(100, '\n');
        }
    }
    while (!done);
}

void Dog::printDogInfo() const;            //Function to print dog's name, age, breed, license fee
{
    cout << "Your dog's name is " << dogName << ". The dog's breed is "
        << dogBreed << ". Age is " << dogAge << " years."
        << "The license fee is $" << licenseFee << "." << endl;
}

#endif


And finally, my main.cpp file:
CODE

#include <iostream>
#include <string>
#include "Dog.h"

using namespace std;

    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;

    displayMenu();

    do
    {
        try
        {
            cin >> choice;
            if (choice < 1 || choice > 3)
                throw str;
            done = true;
        }
        catch (string messageStr)
        {
            cout << messageStr << endl;
            cin.clear();
            cin.ignore(100, '\n');
        }
    }
    while (!done);

    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. smile.gif Thanks!

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Function Overload Error
3 Oct, 2007 - 06:25 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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. smile.gif
User is online!Profile CardPM
+Quote Post

ibleedart
RE: Function Overload Error
3 Oct, 2007 - 07:16 PM
Post #3

New D.I.C Head
*

Joined: 3 Oct, 2007
Posts: 6


My Contributions
QUOTE(Martyr2 @ 3 Oct, 2007 - 07:25 PM) *

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. smile.gif


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. smile.gif

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;

            dogName = dName;
            dogBreed = dBreed;
            dogAge = dAge;


void Dog::printDogInfo() const        //Function to print dog's name, age, breed, license fee
{
    cout << "Your dog's name is " << dogName << ". The dog's breed is "
        << dogBreed << ". Age is " << dogAge << " years."
        << "The license fee is $" << licenseFee << "." << endl;
}

#endif


main.cpp:
CODE

#include <iostream>
#include <string>
#include "Dog.h"

using namespace std;

    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;

    displayMenu();

    do
    {
        try
        {
            cin >> choice;
            if (choice < 1 || choice > 3)
                throw str;
            done = true;
        }
        catch (string messageStr)
        {
            cout << messageStr << endl;
            cin.clear();
            cin.ignore(100, '\n');
        }
    }
    while (!done);

    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;
}




User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Function Overload Error
3 Oct, 2007 - 07:25 PM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
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().

-jjh
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Function Overload Error
3 Oct, 2007 - 07:28 PM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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. smile.gif



User is online!Profile CardPM
+Quote Post

ibleedart
RE: Function Overload Error
3 Oct, 2007 - 07:44 PM
Post #6

New D.I.C Head
*

Joined: 3 Oct, 2007
Posts: 6


My Contributions
QUOTE(Martyr2 @ 3 Oct, 2007 - 08:28 PM) *

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. smile.gif


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.

CODE

#include <iostream>
#include <string>
#include "Dog.h"

using namespace std;

    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;

    displayMenu();
    cin >> choice;
    if (choice < 1 || choice > 3)
        cout << "Incorrect selection" << endl;

    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;

            dogName = dName;
            dogBreed = dBreed;
            dogAge = dAge;
}


void Dog::printDogInfo() const        //Function to print dog's name, age, breed, license fee
{
    cout << "Your dog's name is " << dogName << ". The dog's breed is "
        << dogBreed << ". Age is " << dogAge << " years."
        << "The license fee is $" << licenseFee << "." << endl;
}

#endif

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Function Overload Error
3 Oct, 2007 - 08:01 PM
Post #7

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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?
User is online!Profile CardPM
+Quote Post

ibleedart
RE: Function Overload Error
3 Oct, 2007 - 08:19 PM
Post #8

New D.I.C Head
*

Joined: 3 Oct, 2007
Posts: 6


My Contributions
QUOTE(Martyr2 @ 3 Oct, 2007 - 09:01 PM) *

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! smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:19PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month