创建对链表向量时 Gnu 编译器的错误:无效使用"::"
Error from Gnu compiler when creating vector of linked lists of pairs: invalid use of '::'
我正在创建一个哈希表,设置一个forward lists
pairs
的array
来保存数据。
在C++条款中宣布std::vector
std::forward_list
std::pair
,如下:
std::vector<std::forward_list<std::pair<int, std::string>>> table_ = std::vector<std::forward_list<std::pair<int, std::string>>>(HASHSIZE);
(编辑:下面发布的完整示例(
上面的行在Visual Studio和Xcode中都可以编译,但是Gnu编译器抛出以下错误。有什么想法吗?谢谢基思:^(
[~/pickledEgg] $ g++ -std=c++11 main.cpp -o hash
main.cpp:39:122: error: invalid use of ‘::’
std::vector<std::forward_list<std::pair<int, std::string>>> table_ = std::vector<std::forward_list<std::pair<int, std::string>>>(HASHSIZE);
^
main.cpp:39:122: error: expected ‘;’ at end of member declaration
main.cpp:39:128: error: expected unqualified-id before ‘>>’ token
std::vector<std::forward_list<std::pair<int, std::string>>> table_ = std::vector<std::forward_list<std::pair<int, std::string>>>(HASHSIZE);
^
main.cpp:39:112: error: wrong number of template arguments (1, should be 2)
std::vector<std::forward_list<std::pair<int, std::string>>> table_ = std::vector<std::forward_list<std::pair<int, std::string>>>(HASHSIZE);
^
In file included from /usr/include/c++/4.8.2/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8.2/bits/char_traits.h:39,
from /usr/include/c++/4.8.2/ios:40,
from /usr/include/c++/4.8.2/ostream:38,
from /usr/include/c++/4.8.2/iostream:39,
from main.cpp:1:
/usr/include/c++/4.8.2/bits/stl_pair.h:96:12: error: provided for ‘template<class _T1, class _T2> struct std::pair’
struct pair
^
main.cpp:39:107: error: template argument 1 is invalid
std::vector<std::forward_list<std::pair<int, std::string>>> table_ = std::vector<std::forward_list<std::pair<int, std::string>>>(HASHSIZE);
^
main.cpp:39:107: error: template argument 2 is invalid
main.cpp:39:89: error: template argument 1 is invalid
std::vector<std::forward_list<std::pair<int, std::string>>> table_ = std::vector<std::forward_list<std::pair<int, std::string>>>(HASHSIZE);
^
main.cpp:39:89: error: template argument 2 is invalid
[~/QuickBase] $
完整示例
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <forward_list>
#include <utility>
#include <chrono>
namespace qb {
const int HASHSIZE{ 7 };
class HashTable {
public:
bool insert(int key, std::string value) {
if (get(key) == "") { // Duplicate handling: ignore if exists
std::pair<int, std::string> myPair(key, value);
int hashedKey = hash_(key);
it_ = table_[hashedKey].before_begin();
table_[hashedKey].emplace_after(it_, myPair);
}
return true;
}
std::string get(int key) {
for (it_ = table_[hash_(key)].begin(); it_ != table_[hash_(key)].end(); ++it_)
if (it_->first == key)
return it_->second;
return "";
};
void print() {
for (int i = 0; i < HASHSIZE; ++i) {
std::cout << "BUCKET " << i << ": ";
for (it_ = table_[i].begin(); it_ != table_[i].end(); ++it_)
std::cout << "[" << it_->first << ", " << it_->second << "]" << " --> ";
std::cout << "NULL" << std::endl;
}
}
private:
std::vector<std::forward_list<std::pair<int, std::string>>> table_ = std::vector<std::forward_list<std::pair<int, std::string>>>(HASHSIZE);
std::forward_list<std::pair<int, std::string>>::iterator it_;
// Hashing function
int hash_(int key) {
return (key % HASHSIZE);
}
};
}
int main() {
qb::HashTable h;
std::cout << "Buckets: " << qb::HASHSIZE << std::endl << std::endl;
// Insertion
std::wcout << "INSERTION" << std::endl << std::endl;
h.insert(42, "beta");
h.insert(0, "gamma");
h.insert(32767, "delta");
h.insert(29, "epsilon");
h.insert(123, "zeta");
h.insert(74, "heta");
h.insert(1, "theta");
h.insert(42, "iota");
h.insert(33, "kappa");
h.insert(947, "lambda");
h.insert(10225, "mu");
h.print();
std::cout << std::endl;
// Retrieval
std::wcout << "RETRIEVAL" << std::endl << std::endl;
std::cout << "Get 29: " << h.get(29) << std::endl;
std::cout << "Get 34: " << h.get(34) << std::endl;
std::cout << "Get 10225: " << h.get(10225) << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << "Enter 'e' and press Enter to end: ";
std::cin.get();
std::cout << "nn";
return 0;
}
啊,我明白了。
在发布的行中,我无意中在类定义中分配了一个值。创建对象时,需要在构造函数中完成,如下所示:
HashTable() {
table_ = std::vector<std::forward_list<std::pair<int, std::string>>>(HASHSIZE);
}
相关文章:
- 如何解决错误:SCIP C++中的 SCIP 阶段无效 <10>
- what(): basic_string::_M_construct 空无效错误
- C++ Linux 运行时basic_string::_M_construct null 无效错误
- 调用boost v1.58中的函数时出现模板名称使用无效错误
- 对象分配-成员函数的使用无效错误
- 从 WindowsAPI 调用 NetLocalGroupAddMembers 时,安全 ID 结构无效错误
- 为什么映射迭代器显示基本操作数无效错误
- 获取数组类型的无效错误
- 调用 vector.erase() 函数时指针操作无效错误
- C++:basic_string::_M_construct 空,对于图形 BFS 算法无效错误
- C++运行程序后给我 basic_string::_S_construct null 无效错误
- 使用 std::vector 时抽象类类型"Shape"的新表达式无效错误
- mmap 错误:分段错误/指针无效错误
- 从字符到字符编译器的转换无效错误
- 恒常性添加无效?错误:无法使用字符**初始化常量字符**
- 二进制表达式的操作数无效错误消息
- "从'文件* {aka _iobuf}'到'int'的转换无效"错误
- 在 boost 中序列化二进制数据失败,并出现"签名无效"错误
- 基类 T 无效错误
- 从“常量字符*”到“字符*”的转换无效错误