Referencing and inserting into a vector of list












-3















I am implementing a hashtable and am having trouble with the implementation. After literal hours of googling on this one thing, i've given up and was hoping to see of i could get any help here. The biggest issue is to do with the use of vectors in the HashTable(doesnt make sense to me, rather just use list<> but using it is required)



My main issue is to do with how to implement the insert function to add to the HashTable.



void HashTable::insert(ulint key,ulint value){  //insert data associated with key

HashNode nodeToAdd;
nodeToAdd.assign(key, value);

int index = hash_function(key);

this->table[index].push_back(nodeToAdd);

}


Now the issue im having is adding the HashNode to my HashTable.



for reference in HashTable, the field for the table is



typedef vector <list<HashNode> > Table;
Table *table;


So by my understanding



   this->table[index].push_back(nodeToAdd);


is going to the vector HashTable[index], which at the index should be a list. and when it gets to that list, it should push_back the new node into the list.



However when compiled, i'm hit by an error(no matching function to call) and i don't understand why.










share|improve this question

























  • Any C++ code that uses malloc() is broken. malloc is for C code. Even if HashNode is a POD, new should be used. Otherwise, some innocent non-POD member gets added to HashNode, later, and you'll start wondering why all these mysterious crashes started to happen, all over the place. Finally, the correct way to handle compilation errors is to try to understand what they mean, instead of running Google searches for how do I do X.

    – Sam Varshavchik
    Nov 16 '18 at 2:29


















-3















I am implementing a hashtable and am having trouble with the implementation. After literal hours of googling on this one thing, i've given up and was hoping to see of i could get any help here. The biggest issue is to do with the use of vectors in the HashTable(doesnt make sense to me, rather just use list<> but using it is required)



My main issue is to do with how to implement the insert function to add to the HashTable.



void HashTable::insert(ulint key,ulint value){  //insert data associated with key

HashNode nodeToAdd;
nodeToAdd.assign(key, value);

int index = hash_function(key);

this->table[index].push_back(nodeToAdd);

}


Now the issue im having is adding the HashNode to my HashTable.



for reference in HashTable, the field for the table is



typedef vector <list<HashNode> > Table;
Table *table;


So by my understanding



   this->table[index].push_back(nodeToAdd);


is going to the vector HashTable[index], which at the index should be a list. and when it gets to that list, it should push_back the new node into the list.



However when compiled, i'm hit by an error(no matching function to call) and i don't understand why.










share|improve this question

























  • Any C++ code that uses malloc() is broken. malloc is for C code. Even if HashNode is a POD, new should be used. Otherwise, some innocent non-POD member gets added to HashNode, later, and you'll start wondering why all these mysterious crashes started to happen, all over the place. Finally, the correct way to handle compilation errors is to try to understand what they mean, instead of running Google searches for how do I do X.

    – Sam Varshavchik
    Nov 16 '18 at 2:29
















-3












-3








-3








I am implementing a hashtable and am having trouble with the implementation. After literal hours of googling on this one thing, i've given up and was hoping to see of i could get any help here. The biggest issue is to do with the use of vectors in the HashTable(doesnt make sense to me, rather just use list<> but using it is required)



My main issue is to do with how to implement the insert function to add to the HashTable.



void HashTable::insert(ulint key,ulint value){  //insert data associated with key

HashNode nodeToAdd;
nodeToAdd.assign(key, value);

int index = hash_function(key);

this->table[index].push_back(nodeToAdd);

}


Now the issue im having is adding the HashNode to my HashTable.



for reference in HashTable, the field for the table is



typedef vector <list<HashNode> > Table;
Table *table;


So by my understanding



   this->table[index].push_back(nodeToAdd);


is going to the vector HashTable[index], which at the index should be a list. and when it gets to that list, it should push_back the new node into the list.



However when compiled, i'm hit by an error(no matching function to call) and i don't understand why.










share|improve this question
















I am implementing a hashtable and am having trouble with the implementation. After literal hours of googling on this one thing, i've given up and was hoping to see of i could get any help here. The biggest issue is to do with the use of vectors in the HashTable(doesnt make sense to me, rather just use list<> but using it is required)



My main issue is to do with how to implement the insert function to add to the HashTable.



void HashTable::insert(ulint key,ulint value){  //insert data associated with key

HashNode nodeToAdd;
nodeToAdd.assign(key, value);

int index = hash_function(key);

this->table[index].push_back(nodeToAdd);

}


Now the issue im having is adding the HashNode to my HashTable.



for reference in HashTable, the field for the table is



typedef vector <list<HashNode> > Table;
Table *table;


So by my understanding



   this->table[index].push_back(nodeToAdd);


is going to the vector HashTable[index], which at the index should be a list. and when it gets to that list, it should push_back the new node into the list.



However when compiled, i'm hit by an error(no matching function to call) and i don't understand why.







c++ list vector hash hashtable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 3:03







John

















asked Nov 16 '18 at 2:06









JohnJohn

54




54













  • Any C++ code that uses malloc() is broken. malloc is for C code. Even if HashNode is a POD, new should be used. Otherwise, some innocent non-POD member gets added to HashNode, later, and you'll start wondering why all these mysterious crashes started to happen, all over the place. Finally, the correct way to handle compilation errors is to try to understand what they mean, instead of running Google searches for how do I do X.

    – Sam Varshavchik
    Nov 16 '18 at 2:29





















  • Any C++ code that uses malloc() is broken. malloc is for C code. Even if HashNode is a POD, new should be used. Otherwise, some innocent non-POD member gets added to HashNode, later, and you'll start wondering why all these mysterious crashes started to happen, all over the place. Finally, the correct way to handle compilation errors is to try to understand what they mean, instead of running Google searches for how do I do X.

    – Sam Varshavchik
    Nov 16 '18 at 2:29



















Any C++ code that uses malloc() is broken. malloc is for C code. Even if HashNode is a POD, new should be used. Otherwise, some innocent non-POD member gets added to HashNode, later, and you'll start wondering why all these mysterious crashes started to happen, all over the place. Finally, the correct way to handle compilation errors is to try to understand what they mean, instead of running Google searches for how do I do X.

– Sam Varshavchik
Nov 16 '18 at 2:29







Any C++ code that uses malloc() is broken. malloc is for C code. Even if HashNode is a POD, new should be used. Otherwise, some innocent non-POD member gets added to HashNode, later, and you'll start wondering why all these mysterious crashes started to happen, all over the place. Finally, the correct way to handle compilation errors is to try to understand what they mean, instead of running Google searches for how do I do X.

– Sam Varshavchik
Nov 16 '18 at 2:29














1 Answer
1






active

oldest

votes


















0














Your list stores objects of type HashNode, not type HashNode*.



So you need to decide which of those you want to use, and change the code accordingly.




  1. If you want to keep storing HashNode, then your insert is wrong -- it should instead create the node on the stack and store it by value in the list.


  2. If you want to store a pointer, then your Table type is wrong, and should instead be vector<list<HashNode*>> -- note it should be managed carefully since the pointers will not be automatically deleted.



Personally, I'd suggest you go with #1 and save yourself a whole lot of headaches. But if you insist on #2, then I suggest you stop using malloc and use new -- or better yet use std::unique_ptr or std::shared_ptr for automatic lifetime management.



Also noteworthy is your definition Table *table. This is baffling, since Table is a vector. Your insert function is dereferencing this pointer, expecting it to perhaps point to an array of Table values, when it's quite clear you actually think it's a vector. I'm pretty sure you don't want that to be a pointer.



Since I only just noticed that detail, I imagine that's the first source of your error, since table[index] is actually type Table, not type list<HashNode> and you were trying to call the non-existent function vector<list<HashNode>>::push_back(HashNode*).






share|improve this answer


























  • Unfortunately im forced to use the class defs given, so have to use Table *table. Maybe its me misunderstanding typedef but doesnt the typedef line make "Table" equivalent to vector<list<HashNode>> ? So i would think Table *table is of type vector<list<HashNode>> ?

    – John
    Nov 16 '18 at 3:19











  • 1) Yes; 2) No! Table* is a pointer, so it's equivalent to vector<list<HashNode>>*

    – paddy
    Nov 16 '18 at 5:39











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330454%2freferencing-and-inserting-into-a-vector-of-listnodes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Your list stores objects of type HashNode, not type HashNode*.



So you need to decide which of those you want to use, and change the code accordingly.




  1. If you want to keep storing HashNode, then your insert is wrong -- it should instead create the node on the stack and store it by value in the list.


  2. If you want to store a pointer, then your Table type is wrong, and should instead be vector<list<HashNode*>> -- note it should be managed carefully since the pointers will not be automatically deleted.



Personally, I'd suggest you go with #1 and save yourself a whole lot of headaches. But if you insist on #2, then I suggest you stop using malloc and use new -- or better yet use std::unique_ptr or std::shared_ptr for automatic lifetime management.



Also noteworthy is your definition Table *table. This is baffling, since Table is a vector. Your insert function is dereferencing this pointer, expecting it to perhaps point to an array of Table values, when it's quite clear you actually think it's a vector. I'm pretty sure you don't want that to be a pointer.



Since I only just noticed that detail, I imagine that's the first source of your error, since table[index] is actually type Table, not type list<HashNode> and you were trying to call the non-existent function vector<list<HashNode>>::push_back(HashNode*).






share|improve this answer


























  • Unfortunately im forced to use the class defs given, so have to use Table *table. Maybe its me misunderstanding typedef but doesnt the typedef line make "Table" equivalent to vector<list<HashNode>> ? So i would think Table *table is of type vector<list<HashNode>> ?

    – John
    Nov 16 '18 at 3:19











  • 1) Yes; 2) No! Table* is a pointer, so it's equivalent to vector<list<HashNode>>*

    – paddy
    Nov 16 '18 at 5:39
















0














Your list stores objects of type HashNode, not type HashNode*.



So you need to decide which of those you want to use, and change the code accordingly.




  1. If you want to keep storing HashNode, then your insert is wrong -- it should instead create the node on the stack and store it by value in the list.


  2. If you want to store a pointer, then your Table type is wrong, and should instead be vector<list<HashNode*>> -- note it should be managed carefully since the pointers will not be automatically deleted.



Personally, I'd suggest you go with #1 and save yourself a whole lot of headaches. But if you insist on #2, then I suggest you stop using malloc and use new -- or better yet use std::unique_ptr or std::shared_ptr for automatic lifetime management.



Also noteworthy is your definition Table *table. This is baffling, since Table is a vector. Your insert function is dereferencing this pointer, expecting it to perhaps point to an array of Table values, when it's quite clear you actually think it's a vector. I'm pretty sure you don't want that to be a pointer.



Since I only just noticed that detail, I imagine that's the first source of your error, since table[index] is actually type Table, not type list<HashNode> and you were trying to call the non-existent function vector<list<HashNode>>::push_back(HashNode*).






share|improve this answer


























  • Unfortunately im forced to use the class defs given, so have to use Table *table. Maybe its me misunderstanding typedef but doesnt the typedef line make "Table" equivalent to vector<list<HashNode>> ? So i would think Table *table is of type vector<list<HashNode>> ?

    – John
    Nov 16 '18 at 3:19











  • 1) Yes; 2) No! Table* is a pointer, so it's equivalent to vector<list<HashNode>>*

    – paddy
    Nov 16 '18 at 5:39














0












0








0







Your list stores objects of type HashNode, not type HashNode*.



So you need to decide which of those you want to use, and change the code accordingly.




  1. If you want to keep storing HashNode, then your insert is wrong -- it should instead create the node on the stack and store it by value in the list.


  2. If you want to store a pointer, then your Table type is wrong, and should instead be vector<list<HashNode*>> -- note it should be managed carefully since the pointers will not be automatically deleted.



Personally, I'd suggest you go with #1 and save yourself a whole lot of headaches. But if you insist on #2, then I suggest you stop using malloc and use new -- or better yet use std::unique_ptr or std::shared_ptr for automatic lifetime management.



Also noteworthy is your definition Table *table. This is baffling, since Table is a vector. Your insert function is dereferencing this pointer, expecting it to perhaps point to an array of Table values, when it's quite clear you actually think it's a vector. I'm pretty sure you don't want that to be a pointer.



Since I only just noticed that detail, I imagine that's the first source of your error, since table[index] is actually type Table, not type list<HashNode> and you were trying to call the non-existent function vector<list<HashNode>>::push_back(HashNode*).






share|improve this answer















Your list stores objects of type HashNode, not type HashNode*.



So you need to decide which of those you want to use, and change the code accordingly.




  1. If you want to keep storing HashNode, then your insert is wrong -- it should instead create the node on the stack and store it by value in the list.


  2. If you want to store a pointer, then your Table type is wrong, and should instead be vector<list<HashNode*>> -- note it should be managed carefully since the pointers will not be automatically deleted.



Personally, I'd suggest you go with #1 and save yourself a whole lot of headaches. But if you insist on #2, then I suggest you stop using malloc and use new -- or better yet use std::unique_ptr or std::shared_ptr for automatic lifetime management.



Also noteworthy is your definition Table *table. This is baffling, since Table is a vector. Your insert function is dereferencing this pointer, expecting it to perhaps point to an array of Table values, when it's quite clear you actually think it's a vector. I'm pretty sure you don't want that to be a pointer.



Since I only just noticed that detail, I imagine that's the first source of your error, since table[index] is actually type Table, not type list<HashNode> and you were trying to call the non-existent function vector<list<HashNode>>::push_back(HashNode*).







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 2:23

























answered Nov 16 '18 at 2:17









paddypaddy

42.6k53076




42.6k53076













  • Unfortunately im forced to use the class defs given, so have to use Table *table. Maybe its me misunderstanding typedef but doesnt the typedef line make "Table" equivalent to vector<list<HashNode>> ? So i would think Table *table is of type vector<list<HashNode>> ?

    – John
    Nov 16 '18 at 3:19











  • 1) Yes; 2) No! Table* is a pointer, so it's equivalent to vector<list<HashNode>>*

    – paddy
    Nov 16 '18 at 5:39



















  • Unfortunately im forced to use the class defs given, so have to use Table *table. Maybe its me misunderstanding typedef but doesnt the typedef line make "Table" equivalent to vector<list<HashNode>> ? So i would think Table *table is of type vector<list<HashNode>> ?

    – John
    Nov 16 '18 at 3:19











  • 1) Yes; 2) No! Table* is a pointer, so it's equivalent to vector<list<HashNode>>*

    – paddy
    Nov 16 '18 at 5:39

















Unfortunately im forced to use the class defs given, so have to use Table *table. Maybe its me misunderstanding typedef but doesnt the typedef line make "Table" equivalent to vector<list<HashNode>> ? So i would think Table *table is of type vector<list<HashNode>> ?

– John
Nov 16 '18 at 3:19





Unfortunately im forced to use the class defs given, so have to use Table *table. Maybe its me misunderstanding typedef but doesnt the typedef line make "Table" equivalent to vector<list<HashNode>> ? So i would think Table *table is of type vector<list<HashNode>> ?

– John
Nov 16 '18 at 3:19













1) Yes; 2) No! Table* is a pointer, so it's equivalent to vector<list<HashNode>>*

– paddy
Nov 16 '18 at 5:39





1) Yes; 2) No! Table* is a pointer, so it's equivalent to vector<list<HashNode>>*

– paddy
Nov 16 '18 at 5:39


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330454%2freferencing-and-inserting-into-a-vector-of-listnodes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word