#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!

Recommended Answers

All 5 Replies

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.

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.

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?

Unfortunalry no. the syntax should be

strcmp(name.c_str(), other.name.c_str())

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.