C++ No members available in a template class - c++

I'm trying to implement the hash table class, but when I try to access the STL list member functions and iterator in the Hash_Table class, visual studio says no members available. Here is my code:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#define INIT_SIZE 11
using namespace std;
template<typename KEY_TYPE, typename VALUE_TYPE>
class Hash_Table
{
public:
typedef pair<const KEY_TYPE, VALUE_TYPE> Entry_Type;
// Constructor
Hash_Table(const int size = INIT_SIZE)
{
myTable.resize(size);
}
// HashCode() function for calculating hash code of a specified key;
int HashCode(const KEY_TYPE& key)
{
int index = hash<KEY_TYPE>(key) % myTable.size();
return index;
}
void Insert (const Entry_Type& value)
{
}
// Index operator
Entry_Type& operator [] (const KEY_TYPE& key)
{
typename list<Entry_Type>:: ;//no members available
myTable[0]. ; //no members available
}
void Rehash ();
private:
// the table
vector<list<Entry_Type>> myTable;
};
Can anyone tell me why it happens? Thank you so much!

Related

How to Use std::any as mapped_type

I am trying to solve the problem asked yesterday on SO based on this answer.
I have modified the code given here to use std::any instead of void*. The code that i currently have is as follows:
#include <iostream>
#include <map>
#include <vector>
#include <any>
#include <typeindex>
struct cStreet{};
struct cHouse{};
struct cComputer{};
struct cBook{};
class cPlayer
{
public:
struct Properties
{
std::vector<cStreet*> Streets;
std::vector<cHouse*> Houses;
std::vector<cComputer*> Computers;
std::vector<cBook*> Book;
};
cPlayer(std::string name) : m_name{name}{};
~cPlayer(){};
std::string m_name{};
Properties m_Properties;
std::map<std::type_index, std::any> myMap{{typeid(cStreet*), m_Properties.Streets}, {typeid(cHouse*), m_Properties.Houses}, {typeid(cComputer*), m_Properties.Computers}, {typeid(cBook*), m_Properties.Book}};
template<typename T> void buy(T& Arg);
};
template<typename T> void cPlayer::buy(T& Arg)
{
std::cout << m_name.c_str() << " : Do you want buy this ?" <<typeid(Arg).name() << std::endl;
//Todo: Decision (here yes)
std::any_cast<std::vector<decltype(&Arg)>>(myMap.at(typeid(&Arg))).push_back(&Arg); //THIS DOESN'T ADD ELEMENTS INTO THE VECTORS BECAUSE STD::ANY HAS A COPY OF THE ORIGINAL VECTORS
}
int main()
{
//create objects
cStreet S;
cHouse H;
cComputer C;
cBook B;
cPlayer c("anoop");
//lets test our code
c.buy(S);
c.buy(H);
c.buy(C);
c.buy(B);
}
The problem is that when i wrote
std::any_cast<std::vector<decltype(&Arg)>>(myMap.at(typeid(&Arg))).push_back(&Arg);
this does not adds(push_back) element into the original vectors but a copy of it.
How can i add element into the original vectors m_Properties.Streets, m_Properties.Houses etc? I tried using std::ref but i was not able to successfully do it using std::ref.
Based on this answer, redefine your myMap as:
std::map<std::type_index, std::any> myMap{
{typeid(cStreet*), std::ref(m_Properties.Streets)},
{typeid(cHouse*), std::ref(m_Properties.Houses)},
{typeid(cComputer*), std::ref(m_Properties.Computers)},
{typeid(cBook*), std::ref(m_Properties.Book)}
};
Then cast any to the corresponding reference_wrapper type according to T to access the original vector:
template<typename T>
void cPlayer::buy(T& Arg) {
// ...
using mapped_type = std::reference_wrapper<std::vector<T*>>;
std::any_cast<mapped_type>(myMap.at(typeid(T*))).get().push_back(&Arg);
// ...
}

template "error: expected expression" in object declaration

I'm receiving an error code when trying to declare a new stack object that uses a template class. I can't seem to find anything regarding this and most of the questions asked that I've found seem to refer to be for creating functions. Any help on how to resolve this would be greatly appreciated.
#include <stdio.h>
#include <iostream>
#include "stack4.h"
using namespace main_savitch_6B;
using namespace std;
int main(int argc, char **argv)
{
template <class Item> //error: expected expression
stack a;
a.push(4);
return 0;
}
my header file:
#ifndef MAIN_SAVITCH_STACK4_H
#define MAIN_SAVITCH_STACK4_H
#include <cstdlib> // Provides NULL and size_t
#include "node2.h" // Node template class from Figure 6.5 on page 308
namespace main_savitch_6B //7B
{
template <class Item>
class stack
{
public:
// TYPEDEFS
typedef std::size_t size_type;
typedef Item value_type;
// CONSTRUCTORS and DESTRUCTOR
stack( ) { top_ptr = NULL; }
stack(const stack& source);
~stack( ) { list_clear(top_ptr); }
// MODIFICATION MEMBER FUNCTIONS
void push(const Item& entry);
void pop( );
void operator =(const stack& source);
Item& top( );
void swap(stack& y);
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const
{ return main_savitch_6B::list_length(top_ptr); }
bool empty( ) const { return (top_ptr == NULL); }
const Item& top( ) const;
private:
main_savitch_6B::node<Item> *top_ptr; // Points to top of stack
};
}
#include "stack4.template" // Include the implementation
#endif
stack<int> a;
if you want to define an instance of a template class

std::pair of strings as custom key of unordered_map defined in std fails with template errors

I have a map defined and used like this
// def.h
struct X{};
struct Y{};
struct myStruct
{
X x;
Y y;
};
typedef std::unordered_map<std::pair<std::string, std::string>, myStruct> myMap;
namespace std
{
template<> struct pair<std::string, std::string>
{
std::string s1,s2;
pair(const std::string& a, const std::string& b):s1(a),s2(b){}
bool operator < (const pair<std::string,std::string>& r)
{
return (0 < r.s1.compare(s1) && (0 < r.s2.compare(s2)));
}
};
}
//use.cpp
class CUse
{
myMap m;
public:
CUse():m(0){}
};
Some errors emitted by the compiler are extracted as below
At the constructor CUse initialization,
note: see reference to function template instantiation
'std::unordered_map,myStruct,std::hash<_Kty>,std::equal_to<_Kty>,std::allocator>>::unordered_map(unsigned __int64)' being compiled
At the declaration of m in CUse
note: see reference to class template instantiation
'std::unordered_map,myStruct,std::hash<_Kty>,std::equal_to<_Kty>,std::allocator>>' being compiled
As #Bo Persson and #Sean Cline mentioned in the comments, you will need to use a custom hash function/functor to do that.
LIVE DEMO
#include <unordered_map>
#include <string>
#include <tuple>
#include <functional>
#include <cstddef>
#include <iostream>
struct myStruct { int x, y; };
using Key = std::pair<std::string, std::string>;
namespace something
{
struct Compare //custom hash function/functor
{
std::size_t operator()(const Key& string_pair) const
{
// just to demonstrate the comparison.
return std::hash<std::string>{}(string_pair.first) ^
std::hash<std::string>{}(string_pair.second);
}
};
}
using myMap = std::unordered_map<Key, myStruct, something::Compare>;
int main()
{
myMap mp =
{
{ { "name1", "name2" },{ 3,4 } },
{ { "aame1", "name2" },{ 8,4 } },
{ std::make_pair("fame1", "name2"),{ 2,4 } }, // or make pair
{ std::make_pair("fame1", "bame2"),{ 1,2 } }
};
for(const auto& it: mp)
{
std::cout << it.first.first << " " << it.first.second << " "
<< it.second.x << " " << it.second.y << std::endl;
}
return 0;
}
However, every symmetric pair will make almost same hashes and that can cause,
hash collisions and thereby less performance. Nevertheless, additional specializations for std::pair to compose hashes are available in boost.hash
An alternative solution, could be using std::map<>. There you can also specify the custom function/functor for the std::pair, in order to achieve the same map structure. Even though there you will not have to face hash-collisions, that would be well sorted which you might not want.
LIVE DEMO
#include <map>
#include <string>
#include <tuple>
#include <iostream>
struct myStruct { int x, y; };
using Key = std::pair<std::string, std::string>;
namespace something
{
struct Compare
{
bool operator()(const Key& lhs, const Key& rhs) const
{
// do the required comparison here
return std::tie(lhs.first, lhs.second) < std::tie(rhs.first, rhs.second);
}
};
}
using myMap = std::map<Key, myStruct, something::Compare>;
could you tell me why it is not good to use my data type in std ? My
type is defined in my own program anyway.
You shouldn't make it under the namespace of std, because it can cause a UB. A well defined situations/exceptions where you can extend std namespace are given here: https://en.cppreference.com/w/cpp/language/extending_std
Answer to the secondary question:
Thank you but could you tell me why it is not good to use my data type in std ? My type is defined in my own program anyway
You feel that you can define whatever you want in your program, right? (That is the impression you gave, at least.) Well, C++ implementations feel the same way about namespace std -- it is their namespace and they can define whatever they want in it (subject to the C++ standard, of course).
If an implementation needs to define a (possibly undocumented) helper function/class/whatever, the expectation is that it can be placed in namespace std without conflicting with your program. Case in point: what would happen to your program if your C++ library decided that it needed to define a specialization of the std::pair template for std::pair<std::string, std::string>? To my knowledge, the standard neither requires nor prohibits such a specialization, so the existence of it is left to the implementor's discretion.
Namespaces exist to prevent naming conflicts. In particular, namespace std exists to isolate C++ implementation details from user programs. Adding your code to namespace std destroys that isolation, hence the standard declares it undefined behavior. Don't do it.
(That being said, there is nothing stopping you from writing a wrapper class around std::pair<std::string, std::string> to get the functionality you need. Just do it in your own namespace.)
You need to define a specialization of std::hash for your key type, like so:
#include <unordered_map>
#include <string>
using KeyType = std::pair<std::string, std::string>;
namespace std
{
template<>
struct hash<KeyType>
{
size_t operator()(KeyType const& kt) const
{
size_t hash = 0;
hash_combine(hash, kt.first);
hash_combine(hash, kt.second);
return hash;
}
// taken from boost::hash_combine:
// https://www.boost.org/doc/libs/1_55_0/doc/html/hash/reference.html#boost.hash_combine
template <class T>
inline static void hash_combine(std::size_t& seed, const T& v)
{
std::hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
};
}
int main()
{
std::unordered_map<KeyType, int> us;
return 0;
}

C++ Why didn't the default copy work? [duplicate]

This question already has answers here:
Why does the C++ map type argument require an empty constructor when using []?
(6 answers)
Closed 5 years ago.
I've done a lot of Googling and can't seem to figure out what's going on. I'm teaching myself C++ (I'm more familiar with Java).
I have Item Class objects that are being stored in an Inventory Class map, not as pointers. I want to retrieve one of the items from the Inventory in a function, assign it to a temp variable while I delete it from the Inventory map, and then return the object itself so something else can use it. When I originally tried using the code within my function it was returning the error (followed by the stack trace of c++ library stuff):
no matching constructor for initialization of 'Item'
::new ((void*)__p) _Tp();
I tried creating a copy constructor, but to no avail. Eventually, it worked by including an empty constructor ( Item(); ) in my header file and defining it in my cpp file ( Item::Item() {} ).
I would just like to understand why this was necessary so I can recognize it in the future to know what I'm doing.
EDIT: Upon further inspection of the error stack trace, it turned out the actual problem with with the Inventory::addItem function. When assigning an object to a map using operator[], the map first instantiates the value type to the key using the default constructor before making the assignment. No default constructor was available, so the error was returned.
It was fixed by changing the line to map.insert({key, value})
Here are the important parts of the two class files:
//item.h
#include <string>
using namespace std;
class Item {
private:
string name;
int type;
int levelReq;
public:
Item(string name, int type, int levelReq);
Item();
string getName() {return name;}
int getType() {return type;}
friend ostream &operator<<(ostream &out, const Item &item);
};
---------------------------------------------------------------
//item.cpp
#include <string>
#include "item.h"
using namespace std;
Item::Item(string n, int t, int l) : name(n), type(t), levelReq(l) {}
Item::Item() {}
ostream &operator<<(ostream &out, const Item &item) {
return out << item.name;
}
---------------------------------------------------------------
//inventory.h
#include <map>
#include "item.h"
class Inventory {
private:
map <int, Item> inventory;
int size;
bool full;
int nextFree;
void findNextFree();
public:
Inventory();
bool isFull() {return full;}
void addItem(Item item);
Item getItem(int slot);
void showInv();
};
---------------------------------------------------------------
//inventory.cpp
#include <iostream>
#include <string>
#include "inventory.h"
#include "item.h"
using namespace std;
Inventory::Inventory() {
full = false;
nextFree = 1;
size = 28;
}
void Inventory::addItem(Item item) {
if (!full) {
inventory[nextFree] = item;
findNextFree();
}
else {
cout << "Your inventory is full (Inv::addItem)";
}
}
Item Inventory::getItem(int slot) {
Item item = inventory.at(slot);
inventory.erase(slot);
full = false;
if (nextFree > slot) {
nextFree = slot;
}
return item;
}
void Inventory::findNextFree() {
nextFree++;
if (nextFree == size + 1) {
full = true;
}
else if (inventory.count(nextFree)) {
findNextFree();
}
}
I think the issue rose because you declared a constructor for your item class.
C++ will automatically generate the necessary constructors if you don't provide any custom constructors.
The necessary constructors are the default, copy and move constructors.
The moment you provide one, the default constructors won't be generated and you have this issue. This principle will also apply to structs.
Check the reference to see for yourself:
http://en.cppreference.com/w/cpp/language/default_constructor
http://en.cppreference.com/w/cpp/language/copy_constructor
http://en.cppreference.com/w/cpp/language/move_constructor
Hope this answers your question.

C++ iterator on a list error

I have in a C++ class the following :
I want to use an iterator to have an element of the questions list at a time by calling the getNextQuestion() function after checking if the iterator is still valid by calling isValid(). It gives me the following terrible error :
passing ‘const iterator {aka const std::_List_iterator<domain::Question>}’ as ‘this’ argument of ‘std::_List_iterator<_Tp>::_Self&
std::_List_iterator<_Tp>::operator++() [with _Tp = domain::Question,std::_List_iterator<_Tp>::_Self =
std::_List_iterator<domain::Question>]’ discards qualifiers [-fpermissive]
#ifndef TESTREPOSITORY_H_
#define TESTREPOSITORY_H_
#include <iostream>
#include <iterator>
#include <list>
#include <algorithm>
#include <fstream>
#include "../domain/question.h"
using namespace domain;
namespace repository{
template<class T>
class TestRepository{
std::string file;
std::list<T> questions;
typename std::list<T>::iterator it;
public:
TestRepository(std::string& file=""):file(file){
this->questions = this->getQ();
this->it = this->questions.begin();
};
std::list<T> getQ() const{
std::list<T> listq;
using namespace std;
string line;
std::ifstream fin(file.c_str());
while(fin.good()){
Question q;
fin >> q;
listq.push_back(q);
}
fin.close();
return listq;
}
const bool isValid() const{
return this->it != this->questions.end();
}
const T getNextQuestion() const{
T q = (*this->it);
++this->it;
return q;
}
};
}
#endif /* TESTREPOSITORY_H_ */
Here is the code where I call these funcitons,maybe here is the problem coming from :
#include "TestController.h"
#include "../domain/test.h"
#include <iostream>
#include <list>
#include <iterator>
namespace controller{
TestController::TestController(repository::TestRepository<domain::Question>* repo,int testId){
this->repo = repo;
this->testId = 0;
}
const test TestController::getCurrentTest() const{
test test(this->testId,0,0);
return test;
}
const bool TestController::isValid() const{
return this->repo->isValid();
}
const Question TestController::getNextQuestion() const{
return this->repo->getNextQuestion();
}
}
Here:
const T getNextQuestion() const{
T q = (*this->it);
++this->it;
return q;
}
You are changing the field it and you're not supposed to, because the method is const. If you want to use const only in the meaning that your "repository" is not modified, but its internal iterator is irrelevant you can use the mutable keyword:
mutable typename std::list<T>::iterator it;
You are trying to modify a member in a const member function, which is forbidden (thats the point of const member functions) :
const T getNextQuestion() const{
T q = (*this->it);
++this->it; // << Here
return q;
}
This method should be non const, or consider having your member iterator be mutable :
mutable typename std::list<T>::iterator it;