0
#include<iostream>
#include<string>
#include <ctime>

using namespace std;




class Dog
{
protected:
    string name;
    char gender;
public:
    Dog(string theName, char theGender) /*constructor*/
    {
        name = theName;
        gender = theGender;
    }
    ~Dog()
    {
        cout << "good bye: " << name << endl;
    }
    void bark(int n)
    { cout << name << ":";
    for (int i = 1; i <= n; i++)
        cout << "rff" << endl;
    }
    void sleep()
    {
        cout << name << ": zzzz, zzzz" << endl;
    }
    void eat()
    {
        cout << name << ": slurp" << endl;
    }
    bool operator< (const Dog &other)
    {   
        if(strcmp(this->name, 
            other.name) < 0)
            return true;
        else 
            return false;
    }




};

class Lab: public Dog
    {
    public: 
        Lab();
    private: 
        string color;
        string getColor()
        { return color;}
        void setColor()
        {
            int i;
            cout << "1. YELLOW" << endl << "2. CHOCOLATE" << endl << "3. WHITE" << endl << "4. BLACK" << endl << "Enter the number of the color desired: ";
            cin >> i;
            switch(i)
            {
                case1: 
                color = "YELLOW";
                break;
                case2: 
                color = "CHOCOLATE";
                break;
                case3: 
                color = "WHITE";
                break;
                case4: 
                color = "BLACK";
                break;
                default:
                cout << endl << "Invalid entry, please enter another number."  << endl;
                setColor();
            }
        }

};

int main ()
{   int i;

    srand((unsigned)time(0));
    Dog Smokey("Smokey", 'y');
    Smokey.bark(i = rand() % 11);
    Smokey.eat();
    Smokey.sleep();
    getchar();
    return 0; 
}

Just a question about the operator overload...I have no idea why that part won't compile. I'm trying to overload the '<' operator for use in comparing the dogs 'names', but I keep getting a

C2664 error saying that I can't convert std::string to a const char *.  

Any help is greatly appreciated!

Edited by mike_2000_17: Fixed formatting

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by JohnKSmith
0

Specifically, the problem is "Write a less than operator ('<') for the Dog class that will test to see if the first dog's name is alphabetically less than the second dog's name. The operator will return a boolean value of true if so, false if not.

0

strcmp takes is defines as

strcmp(const char*, const char*)

and you are trying to do

strcmp(string, string)

so what you need to do is convert the string to a char* and then use that for the strcmp function. Fortunately help does exist. The string type has a member method called c_str() which will return a const char* of the string. the definition for this is

const char * c_str();

So you could use this to make strcmp work for you. If you are still having problems post what you have again and I'd be happy to help again.

Edited by NathanOliver: n/a

0
bool operator< (const Dog &other)
	{	
		if(strcmp(this->name, 
			(other.name).c_str()) < 0)
			return true;
		else 
			return false;
	}

Alright, I redid my code a bit, and tried implementing that, but it doesn't appear to have done anything...no new errors, but the other one still appears. Did I implement it correctly?

0

I know what this means (I think), but I have no idea how to fix it. Also, my other error arose due to this, that there is no match for operator<<() in std::cout << yours. Any ideas?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.