1st time w/ classes: conversion and identifier error (plz Help!)

This is a discussion on 1st time w/ classes: conversion and identifier error (plz Help!) within the C++ Programming forums, part of the General Programming Boards category; Decided to take out the exception throwing...we didn't have to have that anyway. Still getting a couple errors that I'm ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    16

    Exclamation 1st time w/ classes: conversion and identifier error (plz Help!)

    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 << "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
    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;
    }
    Last edited by ibleedart; 10-03-2007 at 09:24 PM. Reason: changed code

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    first of all, answer is a char and you're comparing it to a char*
    it should be

    Code:
    if (answer == 'N')
    with single quotes

    I'm looking through the rest of your code right now

    edit1:
    i see lots of exceptions in your code... Are you by any chance a java programmer?
    exceptions should be avoided in most cases in C++ as they're slow (there's probably other reasons but this is the only one i know of)
    in your case:
    1. there isn't anything worth using exceptions for
    2. you're using exceptions like gotos

    the following code
    Code:
    		try
    		{
    			cin >> choice;
    			if (choice < 1 || choice > 3)
    				throw str;
    			done = true;
    		}
    		catch (string messageStr)
    		{
    			cout << messageStr << endl;
    			cin.clear();
    			cin.ignore(100, '\n');
    		}
    can be reduced to
    Code:
    			cin >> choice;
    			if (!(choice < 1 || choice > 3))
    			        done = true;
                            else
    		       {
    		              	cout << messageStr << endl;
    		              	cin.clear();
    			        cin.ignore(100, '\n');
    		       }
    EDIT2:

    redundant code in Dog::setDogInfo()
    why not store it directly in dogName, dogBreed, etc?
    Code:
    			dogName = dName;
    			dogBreed = dBreed;
    			dogAge = dAge;
    			allDone = true;
    I know I can't initialize a variable when I declare it in the class. How do I go about this then?
    You mean like change the value of dogName, dogBreed and dogAge from outside the class?
    use getters and setters
    Code:
    void SetDogName(char c) { dogName = c; }
    char GetDogName() { return dogName; }
    btw, dogName is a char. chars only store a single character and i doubt anyone would name their dog with a single character
    Last edited by h_howee; 10-03-2007 at 09:36 PM.

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    16

    Sorry

    Sorry...didn't know you replied until after I changed the code...just trying to make sense of it all...I'm still getting these errors:

    error C2655: 'Dog::licenseFee' : definition or redeclaration illegal in current scope
    error C2371: 'licenseFee' : redefinition; different basic types
    error C2761: 'char Dog::dName' : member function redeclaration not allowed

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    16

    Smile

    "i see lots of exceptions in your code... Are you by any chance a java programmer?
    exceptions should be avoided in most cases in C++ as they're slow (there's probably other reasons but this is the only one i know of)"

    No, I just didn't know any other way to prompt the user to re-enter, lol. I wasn't aware they were slow, but thanks for letting me know. My C++ class is not the best in the world, lol, and the book leaves something to be desired. I took out all of the exceptions and edited my first post. Still getting some errors I'm unsure about. I can't figure out how to declare a variable for a class and then initialize it without getting an error.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Code:
    void Dog::setDogInfo(char dName, char dBreed, int dAge)
    you're reusing the same names as your class members

    I've never used static members before so I wouldn't know if something else could be causing that error but what I do know for sure is this:
    Code:
    double Dog::licenseFee = 12.25;
    you're declaring Dog::licenseFee here

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Within class declarations member functions are declared like this:
    Code:
    //void Dog::setDogInfo(char dName, char dBreed, int dAge);
    void setDogInfo(char dName, char dBreed, int dAge);
    To use the class, you'll need to declare a Dog instance:
    Code:
    Dog myDog;
    myDog.dName = "Rex";
    myDog.printDogInfo();
    There are numerous other errors.

    For example you can't represent a string as a char (one character) - use std::string.

    The public members dName, answer etc have no purpose in the class, and the setDogInfo function could be rewritten as
    Code:
    //void Dog::setDogInfo(char dName, char dBreed, int dAge)
    void Dog::setDogInfo() //parameters not needed
    					 //Function to enter dog's name, breed, and age
    {
    			cout << "Please enter your dog's name and press enter: ";
    			cin >> dogName;
    			cout << endl;
    
    			cout << "Please enter your dog's breed and press enter: ";
    			cin >> dogBreed;
    			cout << endl;
    
    			cout << "Please enter your dog's age in years and press enter: ";
    			cin >> dogAge;
    			cout << endl;
    
    			cout << "Your dog's name is " << dogName << ". The dog's breed is "
    				<< dogBreed << ". Age is " << dogAge << " years. " << endl;
    
    			//dogName = dName;
    			//dogBreed = dBreed;
    			//dogAge = dAge;
    }
    Last edited by anon; 10-04-2007 at 12:52 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21