0

I wrote a hash table class and header but I cannot construct it on main. It gives "no appropriate default constructor available". What is the cause of that?

Constructor in my header: HashTable.h

explicit HashTable( const HashedObj & notFound, int size = 101 );
        HashTable( const HashTable & rhs )
        : ITEM_NOT_FOUND( rhs.ITEM_NOT_FOUND ),
        array( rhs.array ), currentSize( rhs.currentSize ) { }

Constructor in my cpp: HashTable.cpp

    HashTable<HashedObj>::HashTable( const HashedObj & notFound, int size )
    : ITEM_NOT_FOUND( notFound ), array( nextPrime( size ) )
{
    makeEmpty( );
}

And I'm trying to do following code in my main:

HashTable <int> * hash = new HashTable<int>();
1
  • Please think of readers: don't use shouting ALL UPPERCASE IDENTIFIERS. Well, except for macros, but don't use macros either. Commented Dec 1, 2012 at 13:52

1 Answer 1

1
HashTable <int> * hash = new HashTable<int>();

You've defined a constructor which takes argument, but here you're not passing arguments to the constructor. Instead you're using a parameterless (default) constructor which doesn't exist in your class.

Note that if you've defined a constructor (which takes argument(s)) in your class, then the compiler will not generate a default constructor for you. You've to define it yourself.

Sign up to request clarification or add additional context in comments.

2 Comments

however when I try to pass any parameters, like: "= new HashTable <int>(1,3)" it gives "LNK2019: unresolved external symbol "public: __thiscall HashTable<int>::HashTable<int>(int const &,int)" (??0?$HashTable@H@@QAE@ABHH@Z) referenced in function _main" error.
@Tugkan: Does any constructor two int as arguments? Why don't you see the constructor's parameter types and find out what the constructor can take as argument (s)?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.