0

It keeps on saying "function contains is never used". I've never posted before, so if you have advice on how I can get my point across better please let me know.

[Edit] I'm trying to post only code that I wrote myself. I wrote these 3 functions, and only the first one works:

This is what's in the CPP file:

template<class ItemType>
ItemType LinkedList<ItemType>::replace(int position, const ItemType& newEntry)
{
    std::cout << "replacing at position " << position << " with " << newEntry << std::endl;
    Node<ItemType>* curPtr =this->getNodeAt(position);
    curPtr->setItem(newEntry);
    return newEntry;
}  // end replace


// TODO: LinkedList<ItemType>>::contains
template<class ItemType>
bool LinkedList<ItemType>::contains(const ItemType& entry)
{
    for(int i = 0; i<this->getLength(); i++)
    {
        Node<ItemType>* curPtr = this->getNodeAt(i+1);
        if(curPtr->getItem()==entry) {
            return true;
        }
    }
    return false;
}

// TODO: LinkedList<ItemType>>::containsRecursive
template<class ItemType>
bool LinkedList<ItemType>::containsRecursive(Node<ItemType>* node,const ItemType& entry){
    if(node == nullptr){
        return false;
    }
    if(node->getItem()==entry){
        return true;
    }
    return containsRecusive(node->getNext(),entry);
}

and this is what's in the header

ItemType replace(int position, const ItemType& newEntry);
bool contains(const ItemType& entry);
bool containsRecursive(Node<ItemType>* node,const ItemType& entry);

and when I try to call it in the driver, it doesn't appear on the dropdown. Just the other functions in the program such as:

replace, remove, insert, getNodeAt, getEntry, getLength, clear, and isEmpty

and it basically says the function doesn't exist, when I call it in the driver with

listPtr->contains("hello"); //doesn't work, turns red
listPtr->replace(1,"hello"); //does work
listPtr->containsRecurssive(headPtr, "hello")//doesn't work, turns red

The contains is red, and it says, "No member named 'contains' in 'ListInterfacestd::basic_string<char>'"

And it still says in the .h and the .cpp file that "Function 'contains' is never used"

But all the other functions work fine.

jrobi17
  • 1
  • 1
  • 2
    https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file is worth a read. You haven't really included enough code to diagnose your current problem. Consider a [mcve]. – Retired Ninja Apr 30 '21 at 03:10
  • Oops, the second image is wrong. If I add an actual string it does the same thing – jrobi17 Apr 30 '21 at 03:13
  • 1
    Images of code and errors are not helpful. I didn't even look at them. Copy/paste the text into your question. – Retired Ninja Apr 30 '21 at 03:15
  • There is not really enough code there. I am not entirely sure why you would have a templated member function in the `.cpp` file. Usually the templates go in the headers. – Galik Apr 30 '21 at 03:25
  • I guess I would like to know how to show what I mean without posting the whole code. I'm fine with doing it but half of the code is starter code, and I don't think it is looked down upon to post other peoples code. – jrobi17 Apr 30 '21 at 03:38

1 Answers1

0

I don't know which type you passed to LinkedList to construct listPtr, but most likely that type and the type passed to contains are not the same (although they might look like the same).

Passing "hello" might be interpreted by the compiler in different, as std::string but also as const char *.

Victor
  • 460
  • 3
  • 11