1

I have a homework assignment in which I have to code some methods for a linked list and test with a driver from the professor. I keep running into this error: no matching conversion for functional-style cast from 'int' to 'ItemType'

Here are my files for my "Node" class ItemType:

// ItemType.h. 

#include <fstream>
const int MAX_ITEMS = 5;
enum RelationType  {LESS, GREATER, EQUAL};

class ItemType{
public:
ItemType();
RelationType ComparedTo(ItemType) const;
void Print(std::ostream&) const;
void Initialize(int number);
private: int value;
};

And ItemType.cpp

#include <fstream>
#include <iostream>
#include "ItemType.h"

ItemType::ItemType()
{
  value = 0;
}

RelationType ItemType::ComparedTo(ItemType otherItem) const 
{
  if (value < otherItem.value)
    return LESS;
  else if (value > otherItem.value)
    return GREATER;
  else return EQUAL;
}

void ItemType::Initialize(int number) 
{
  value = number;
}

void ItemType::Print(std::ostream& out) const 
// pre:  out has been opened.
// post: value has been sent to the stream out.
{
  out << value;
}

When I try to use the professors driver, I get an error with initializing the ItemType class with the constructor. I initialize them like so: classList.putItem(ItemType(4)) But I end up with the error stated above, I'm not sure where Im wrong, here is my driver:

#include "unsorted.h"
using namespace std;

int main() {
    UnsortedType classList;


    classList.PutItem(ItemType(4));
    classList.PutItem(ItemType(5));
    classList.PutItem(ItemType(4));
    classList.PutItem(ItemType(4));
    classList.PutItem(ItemType(8));
    cout << "(original) length: " << classList.GetLength() << endl; classList.ResetList();
    classList.Print();
    classList.ShiftRight();
    cout << "(shifted right) length: " << classList.GetLength() << endl; classList.ResetList();
    classList.Print();
    classList.DeleteItem(ItemType(4));
    cout << "(delete all 4s) length: " << classList.GetLength() << endl; classList.ResetList();
    classList.Print();
    classList.ShiftRight();
    cout << "(shift right) length: " << classList.GetLength() << endl; classList.ResetList();
    classList.Print();
    return 0;
}
waleed khalid
  • 67
  • 1
  • 1
  • 6

1 Answers1

3

You don't have a constructor for ItemType that takes an int. A simple fix would be to define that constructor:

ItemType(int v) : value{v} { }
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76