Dear Friends:

I need some help to identify the error.
When I compile my application I get the following error message:

Error   1   error C2678: binary '==' : no operator found which takes a left-hand operand of type 'extPersonType' (or there is no acceptable conversion) f:\college material\cs270\sourcecode\addressbook\addressbook\arrayListType.h    251 1   AddressBook

Any help will be greatly appreciated,

Dani

Recommended Answers

All 8 Replies

What's the code at that line?

You will need to write an override for == for your extPersonType, or revise your code to compare members of that type which are basic data types.

Both of the above replies are basically correct.

  1. Provide your code.
  2. If you are trying to use the comparison operator (==) on a non-trivial class, you need to write an '==' operator to deal with it.

Thanks , here is the code:

My addressBookType Class:
#ifndef H_addressBookType
#define H_addressBookType

#include "arrayListType.h"

using namespace std;

template <class extPersonType>
class addressBookType : public arrayListType<elemType>
{
public:
addressBookType(int size = 500);    //Increase the default size from 100 to 500
const arrayListType<extPersonType>& operator= (const arrayListType<extPersonType>&);

/*int searchByLastName(string lname) const;
void print() const;
void printNameBetweenDate(int startMonth, int startDay, int startYear, int endMonth, int endDay, int endYear) const;
void printNameInAGroup(string grp) const;
void printNameBetweenNames(string startName, string endName) const;*/

private:

};


template <class extPersonType>
addressBookType<extPersonType>::addressBookType(int size):arrayListType(size)
{
    if (size < 0)
    {
        cerr << "The array size must be positive. Creating "
             << "an array of size 500. " << endl;

        maxSize = 500;
    }
    else
        maxSize = size;

    length = 0;

    list = new extPersonType[maxSize];
    assert(list != NULL);
}

template <class extPersonType>
const addressBookType<extPersonType>& addressBookType<extPersonType>::operator= (const addressBookType<extPersonType>& otherList)
{
    if (this != &otherList)   //avoid self-assignment
    {
        delete [] list; 
        maxSize = otherList.maxSize; 
        length = otherList.length; 

        list = new extPersonType[maxSize];  //create the array
        assert(list != NULL);   //if unable to allocate memory 
                                //space, terminate the program 
        for (int i = 0; i < length; i++)
            list[i] = otherList.list[i]; 
    }

    return *this; 
}

#endif

I guess you overwrite the '=', but you don't overwrite the '=='.
As I can see in your code, you're comparing them classes.
You can't compare if the tomato is a melon.
But you can compare if the tomato has as many seeds inside of it as the melon.
You're using '!=', so you need to overwrite the '==' comparison operator.
!= is just the NOT operator of equality.
Then you can just do:

if( !(tomato == melon) ) { 
    // stuff happens here..
}

Just to be sure..
But I guess you already know that, especially after rubberman's answer. :)
Good luck!

Thank you all for your assisntance.

I understand that I need to overwrite the == function. I understand that I can't compare the class as a whole to another class because they aren't the same type, so I have to go deeper and compare elements within the class. The problem comes from understanding the syntax for identifying the two object types that I want to compare. If I first define the function as such:

const arrayListType<extPersonType>& operator==(const arrayListType<extPersonType>&) const;

The class that I am placing the overwrite function is in the addressBookType, which is overwriting the arrayListType. The book doesn't provide clarification on exactly what class I am adding. I am assuming that it should be the class name where the overwrite is being performed, but since I want to use many of the functions that are available within the arrayListType function then I figured that I must somehow switch the object types so that is understands what it is reading.

This is where I am getting lost. In my addressBookType, what should be within the override operator== function, and how does this allow the arrayListType class to understand the object and perform the various tasks that will be asked.

I'm hoping I am explaining this correctly. I would still like to successfully complete the task as well as the following task that goes with it. I just need some sample code to clear up what I am missing.

Again, thank you so much for all your help.

Dani

Comparison of non-identical types is not simple. You need to know which elements of each should be compared. In truth, this is an equivalence function. Create a class::compare(const otherclass& other) function and then iterate through the instances (this and other) until they don't compare equal, and return 1 or -1 as appropriate, otherwise at the end return 0. Use this compare function in the operator== and operator!= functions, and return true/false as appropriate.

Thank ypu so much.

I don’t know which way is correct with class templates:

#include <iostream>
#include <cassert>

using namespace std;

//Definition of a node
template <class Type>
struct nodeType
{
    Type info;
    nodeType<Type> *link;
};

template <class Type>
class linkedListHeadTrail
{
public:
    linkedListHeadTrail();          //Constructor - link = NULL
    void clearAll();                //Clear all items in the list
    void print();               //Print the content of the linked list
    void listLength();          //Print the number of items stored in the list 
    void insertAt(int position, const Type& value); //Insert an item at the designated position
    void removeAt(int position);            //Remove an item at the designated position

protected:
    nodeType<Type> *head;                //Points to the first item in the list
    nodeType<Type> *trail;               //Points to the last item in the list
    //nodeType* current;                //Contains the current item within the list
    int listLen;                    //Number of items within the list
};

template <class nodeType>
linkedListHeadTrail<nodeType>::linkedListHeadTrail() //Default Constructor
{
    head = NULL;
    trail = NULL;
    listLen = 0;
}

template <class nodeType>
linkedListHeadTrail<nodeType>::clearAll()                    
{
    //Move all elements from the list
    nodeType<Type>* prog;            ------- Error with TYPE

}

OR:

#include <iostream>
#include <cassert>

using namespace std;

//Definition of a node
template <class Type>
struct nodeType
{
    Type info;
    nodeType<Type> *link;
};

template <class Type>
class linkedListHeadTrail
{
public:
    linkedListHeadTrail();              //Constructor - link = NULL
    void clearAll();                    //Clear all items in the list
    void print();                   //Print the content of the linked list
    void listLength();              //Print the number of items stored in the list 
    void insertAt(int position, const Type& value); //Insert an item at the designated position
    void removeAt(int position);            //Remove an item at the designated position

protected:
    nodeType<Type> *head;                //Points to the first item in the list
    nodeType<Type> *trail;               //Points to the last item in the list
    //nodeType* current;                //Contains the current item within the list
    int listLen;                    //Number of items within the list
};

template <class Type>
linkedListHeadTrail<Type>::linkedListHeadTrail()     //Default Constructor
{
    head = NULL;
    trail = NULL;
    listLen = 0;
}

template <class Type>
linkedListHeadTrail<Type>::clearAll()                    
{
    //Move all elements from the list
    nodeType<Type>* prog;            ------------ Error with TYPE
}

Doesn’t seem to matter which way I use, neither are working. I initially wrote it using the second method, but have an error which is stopping me from moving on. I get the same error at the same line. Can you please enlighten me?

Best Wishes,

Dani

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.