Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4083224
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:17:09+00:00 2026-05-20T18:17:09+00:00

I currently have a vector using my template as its type: vector<hashData> myTable; hashData

  • 0

I currently have a vector using my template as its type:

vector<hashData> myTable;

hashData is a class:

class hashData{
    public:
        // constructor for hashData
        hashData(hashType data){
            this->data = data;
            this->isActive = true;
            this->deleted = false;
        }

        hashData(){
            this->isActive = false;
            this->deleted = false;
        }

        // internal data for hashTable
        hashType data;
        bool deleted;
        bool isActive;
    };

When I attempt to perform an operation such as the following:

vector<hashData> oldTable = myTable;

I receive this error message:

error C2440: ‘initializing’ : cannot
convert from ‘std::vector<_Ty>’ to
‘std::vector<_Ty>’

hashtable.h(211): error C2440:
‘initializing’ : cannot convert from
‘std::vector<_Ty>’ to> ‘std::vector<_Ty>’ with
[_Ty=hashTable::hashData] and [_Ty=unsigned long]

No constructor could take the source type, or constructor overload resolution was ambiguous

Any ideas as to why this is occurring? My reference materials seem to think this is possible, so I’m not sure where my mistake is.

EDIT: Here is the complete header file of the hashTable implementation. I apologize for the length of the code, but I wanted to include everything since my initial “fragment” appears to be insufficient.

// Driver file
#include “hashTable.h”

int main(void){
    // hash table creation
    hashTable<unsigned long> newTable(3);

    // hash table insertion
    newTable.addRecord(5);
    newTable.addRecord(6);
    newTable.addRecord(7);
    newTable.addRecord(8);
}    

// hashTable header file

// BEGIN HEADER FILE
#ifndef HASHTABLE_H
#define HASHTABLE_H

// Includes (system libraries)
#include <iostream>
#include <vector>

// Includes (custom libraries)

// Namespace
using namespace std;

// hashTable class
template <typename hashType>
class hashTable{
public:
    // constructor
    hashTable(int tableSize, string collisionMode = "Linear"){
        this->myTable.resize(optimizeTableSize(tableSize));
        this->collisionMode = collisionMode;
        this->activeRecords = 0;
    }

    // hashTable operations
    void addRecord(hashType);
    void deleteRecord(hashType);
    pair<bool,int> locateRecordPosition(hashType);
    bool searchRecord(hashType);
    hashType returnRecord(hashType);

    // hashTable mainteance
    void considerRehash();
    void rehashTable();
    int optimizeTableSize(int);

    // hashTable math
    bool isPrime(int);
    int nextPrime(int);

    // collision monitoring
    void collisionLogUpdate(int, string);
    int collisionLogAverage();

    // hash table internal class
    class hashData{
    public:
        // constructor for hashData
        hashData(hashType data){
            this->data = data;
            this->isActive = true;
            this->deleted = false;
        }

        hashData(){
            this->isActive = false;
            this->deleted = false;
        }

        // internal data for hashTable
        hashType data;
        bool deleted;
        bool isActive;
    };

private:
    // hashing function
    int calculateHash(hashType, int);

    // hashTable data structure
    vector<hashData> myTable;
    int activeRecords;

    // collision information
    deque<pair<int, string> > collisionLog;
    string collisionMode;
};

// hashTable implementation
// insert a record into the hash table
template <typename hashType>
void hashTable<hashType>::addRecord(hashType toAdd){
    // search for the record
    pair <bool, int> recordPos = locateRecordPosition(toAdd);

    // analyze the results
    if (recordPos.first == true) // the record already exists and is active
        return;
    // otherwise, go ahead and insert the record at this location
    myTable[recordPos.second] = hashData(toAdd);

    // update our count of active records
    activeRecords++;

    // consider a rehash of the hashTable
    considerRehash();
}

// delete a record from the hash table
template <typename hashType>
void hashTable<hashType>::deleteRecord(hashType toDelete){
    // search for the record
    pair <bool, int> recordPos = locateRecordPosition(toDelete);

    // analyze the results
    if (recordPos.first == false) // the record does not exist -- there is nothing to delete here!
        return;

    // otherwise, go ahead and perform a shallow deletion at this area
    myTable[recordPos.second].deleted = true;

    // update our count of active records
    activeRecords--;

    // consider a rehash of the hashTable
    considerRehash();
}

// find position of record within hash table (if such position exists)
template <typename hashType>
pair<bool,int> hashTable<hashType>::locateRecordPosition(hashType toFind){
    // setup data structures
    int collisionNum = 0;
    unsigned int currentPos;

    // search for the entry within the table
    currentPos = calculateHash(toFind, myTable.size());

    // enter a while loop for checking if we've found the item
    while(myTable.at(currentPos).isActive && !myTable.at(currentPos).deleted){
        // check to see if the entry found at the expected position matches
        if(myTable.at(currentPos).data == toFind){
            // update the collisionLog
            collisionLogUpdate(collisionNum,"locateRecord");

            // return the position of the item
            return pair<bool, int>(true,currentPos); // we've successfully found the item
        }

        // otherwise, we need to look for the correct location
        if (collisionMode == "Quadratic"){
            currentPos += 2 * ++collisionNum - 1;
            if(currentPos >= myTable.size())
                currentPos -= myTable.size();
        }
        else if (collisionMode == "Linear"){
            currentPos += 2 * ++collisionNum - 1;
            if(currentPos >= myTable.size())
                currentPos -= myTable.size();
        }

        // reloop and search again
    }

    // update the collisionLog
    collisionLogUpdate(collisionNum,"locateRecord");

    // if we escaped the loop, we were unable to find the item in the table -- return the first open location
    return pair<bool, int>(false,currentPos); // we didn't find the item
}

// return whether a record exists within hash table
template <typename hashType>
bool hashTable<hashType>::searchRecord(hashType toFind){
    return locateRecordPosition(toFind).first; // we didn't find the item
}

// return the contents of a record from the hash table
template <typename hashType>
hashType hashTable<hashType>::returnRecord(hashType toReturn){
    if (locateRecordPosition(toReturn).first) // if the record actually exists
        return myTable[locateRecordPosition(toReturn).second].data;
    else
        return hashType();
}

// calculate hash value
template <typename hashType>
int hashTable<hashType>::calculateHash(hashType toHash, int tableSize){
    if (toHash < 0) // if we have a negative number, change it prior to hashing
        toHash = (toHash*-1);
    return ((toHash*37) % tableSize);
}

// review the collision log and consider rehashing
template <typename hashType>
void hashTable<hashType>::considerRehash(){
    // check if we have used up more then half of the table, if we have, rehash
    if((activeRecords + 1) > ((signed) myTable.size() / 2))
        rehashTable();

    // check the current average of collisions
    // if the average number of collisions is greater then 20% of the table size (meaning it had to search through 20% of table), rehash
    else if((collisionLogAverage() > (myTable.size() * .20)) && (myTable.size() >= 100))
        rehashTable();

    // check the last operations number of collisions
    // if the number of collisions encounter is greater then 30% of the table size (meaning it had to search through 30% of table), rehash
    else if((collisionLog.back().first > (myTable.size() * .30)) && (myTable.size() >= 100))
        rehashTable();
}

// rehash the table
template <typename hashType>
void hashTable<hashType>::rehashTable(){
    // make a copy of the existing vector
    vector<hashType> oldTable = myTable;

    // reallocate myTable
    myTable.resize(optimizeTableSize(myTable.size() * 2)); // double the size of the current table

    // clear myTable
    myTable.clear();

    // copy the existing table over
    for (unsigned int i = 0; i < oldTable.size(); i++){
        if(oldTable[i].isActive && !oldTable[i].deleted){
            addRecord(oldTable[i].data);
        }
    }
}

// optimze table size
template <typename hashType>
int hashTable<hashType>::optimizeTableSize(int tableSize){
    // if we are performing quadratic probing, we need to optimize the table size to be a prime number, to prevent loops
    if (!isPrime(tableSize)){
        return nextPrime(tableSize);
    }

    // we only need to bother with optimizing the table size IF we are performing quadratic probing
    else
        return tableSize;
}

// determine if prime number
template <typename hashType>
bool hashTable<hashType>::isPrime(int numberToEvaluate){
    if(numberToEvaluate == 0)
        return true;

    numberToEvaluate = abs(numberToEvaluate);

    if(numberToEvaluate % 2 == 0) return true;

    for(int i = 3; i <= sqrt((float)numberToEvaluate); i+=2)
        if(numberToEvaluate % i == 0)
            return false;

    return true;
}

// find the next prime number
template <typename hashType>
int hashTable<hashType>::nextPrime(int numberToEvaluate){
    if (numberToEvaluate % 2 == 0)
        numberToEvaluate++;

    for (; !isPrime(numberToEvaluate); numberToEvaluate+=2)
        ;

    return numberToEvaluate;
}

// update collision log with a new entry
template <typename hashType>
void hashTable<hashType>::collisionLogUpdate(int numberOfCollisions, string operationPerformed){
    // add an entry to the log
    collisionLog.push_back(pair<int,string>(numberOfCollisions, operationPerformed));

    // verify we don't have more then 5 entires, if so, remove them
    while(collisionLog.size() > 5)
        collisionLog.pop_front();
}   

template <typename hashType>
int hashTable<hashType>::collisionLogAverage(){
    // add the last five entries, then take their average
    // the log should be maxed at five entries.. so just add them all

    // average holder
    int average;

    // loop through log
    for (unsigned int i = 0; i < collisionLog.size(); i++){
        average = collisionLog.at(i).first;
    }

    // average the sum
    average = average/5;

    // return the calculated average
    return average;
}   

// END HEADER FILE
#endif
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:17:10+00:00Added an answer on May 20, 2026 at 6:17 pm

    myTable is not a std::vector<hashData> – Doublecheck it.

    EDIT: After source posting.

    vector<hashData> myTable;
    vector<hashType> oldTable = myTable;
    

    hashData != hashType

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an std::vector of std::strings containing data similar to this: [0] = ""
I have an x, y, and z vector that I am currently using to
I currently have an existing database and I am using the LINQtoSQL generator tool
We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
I currently have a class and I'm trying to create an easy GUI to
I have a std::vector< tr1::shared_ptr<mapObject> > that I'm trying build from data contained in
I'm currently learning about concurrency in C++ and came across using a vector of
I have a class that accepts a list of policy classes using boost::mpl. Each
I have a class (Uniform) that has a constructor with 2 parameters, and a
I have a n-dimensional vector (1xn dataset, and it is not image data), and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.