简体   繁体   中英

What is the proper syntax for initializing an array of hashtables in C++?

How would I implement an Array of Hashtables in c++?

I have a hashtable class with a constructor that looks like this:

explicit ChainingHashTable( const HashedObj & notFound, int size = 101 );

So I attempted making an array of these hashtables by doing:

static ChainingHashTable<int> answers[5] = { {0, 500}, {0, 500}, {0, 500}, {0, 500}, {0, 500} };

however, I get the following errors & warnings:

browser.cpp:106:71: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
browser.cpp:106:71: error: converting to ‘ChainingHashTable<int>’ from initializer list would use explicit constructor ‘ChainingHashTable<HashedObj>::ChainingHashTable(const HashedObj&, int) [with HashedObj = int]’

I can't use a different compiler since this is for a class assignment, but what am I doing incorrectly? What's the proper syntax?

The compiler is telling you that the code you are trying to write is C++11.

You could use Boost.Assignment

Have you tried

static ChainingHashTable<int> answers[5] = { ChainingHashTable<int>(0, 500), ChainingHashTable<int>(0, 500), ChainingHashTable<int>(0, 500), ChainingHashTable<int>(0, 500), ChainingHashTable<int>(0, 500) };

?


Above is the method for a global variable. For a static class member, the declaration and definition must be separate:

class SomeClass
{
    static ChainingHashTable<int> answers[5];
};

and in one compilation unit (so not inside a header)

ChainingHashTable<int> SomeClass::answers[5] = { ChainingHashTable<int>(0, 500), ChainingHashTable<int>(0, 500), ChainingHashTable<int>(0, 500), ChainingHashTable<int>(0, 500), ChainingHashTable<int>(0, 500) };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM