So there's a handsheet that my professor gave me as a guideline to follow. It gives us the prototype of the functions and that's IT. However, when I put in the prototype and fill in the body, an error comes up in regards to a conversion error. Any help?

Here's my code:

#include <iostream>
#include <string>
#include "Cust.h"	

using namespace std;

typedef Customer ItemType;

class LinkedList
{
	private:
		class Node 
		{
			public:
				ItemType info;
			    Node * next;
		};
		typedef Node * NodePointer;
	public:
		LinkedList();
		void insertItem(const ItemType &element);
		bool deleteItem(const ItemType &element);

	private:
		NodePointer first;
		int mySize;	
};

And my implementation file:

#include <iostream>
#include <fstream>
#include <string>
#include "LinkedList.h"

using namespace std;

LinkedList::LinkedList()
{
	first = 0;
	mySize = 0;
}

void LinkedList::insertItem(const ItemType &element)
{
	NodePointer ptr, prevPtr, newPtr;
	bool found;

	NodePointer newPtr = new Node(element);

	newPtr->next = 0;
	if(first == 0)
		first->next = newPtr;
		mySize++;
	if(first != 0)
	{
		first = ptr;
		found = false;
		while(ptr != 0 && !found)
		{
			if(ptr->info >= newPtr->info)
				found = true;
			else 
			{
				ptr = prevPtr;
				ptr->next = ptr;
			}
		}
	}
	if(ptr == first)
	{
		newPtr->next = first;
		first->next = newPtr;
		mySize++;
	}
	if(ptr != first)
	{
		prevPtr->next = newPtr;
		newPtr->next = ptr;
		mySize++;
	}
}

I know I probably won't finish this program as it is due in about 4 hours lol. But I would GREATLY appreciate it if someone helped me out as soon as they were able to. Thank you forum! :D

Dani AI

Generated

Two quick diagnoses. As noted, newPtr was declared twice. The more likely cause of the "conversion" error, though, is that the inner Node type has no constructor that takes an ItemType, so new Node(element) fails to match any constructor. There are also several logic bugs that will cause crashes (dereferencing first when it is null, using uninitialized ptr/prevPtr, and incrementing mySize in multiple branches).

A simple, safe approach:

  • give Node a constructor that accepts an ItemType (or else allocate and assign info after new),
  • initialize traversal pointers (cur = first, prev = nullptr),
  • walk until the insertion point, then link newNode in place,
  • increment mySize exactly once.

Example implementation sketch (replace ItemType/Customer as needed):

class Node {
public:
    ItemType info;
    Node* next;
    Node(const ItemType& v, Node* n = nullptr) : info(v), next(n) {}
};

void LinkedList::insertItem(const ItemType& element) {
    Node* newNode = new Node(element);
    if (!first) { first = newNode; mySize++; return; }

    Node* prev = nullptr;
    Node* cur = first;
    while (cur && cur->info < element) { prev = cur; cur = cur->next; }

    if (!prev) { newNode->next = first; first = newNode; }
    else { prev->next = newNode; newNode->next = cur; }
    mySize++;
}

Notes and troubleshooting:

  • Ensure Customer defines the comparison used (e.g., operator<) or compare a key field instead.
  • Compile with -Wall -Wextra to see useful diagnostics and fix null-dereferences.
  • Add a proper destructor that deletes nodes to avoid leaks.
  • For reference on constructors and new, see the cppreference pages for and dynamic allocation.

Applying these fixes should remove the conversion error and prevent the runtime crashes.

To make it easier for someone to help, please post the compiler error along with the code. Just saying that the compiler says that there is a conversion error is too vague. The only problem I can see be whatever code you have provided is that you have declared 'newPtr' twice in 'insertItem' function.

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.