使用 c++ 中的 multimap 类创建数据库类
我正在使用 C++ 进行一个项目。我需要存储一对(字符串,整数)并根据字符串访问它们。 multimap 类似乎非常适合这一点。我正在尝试创建自己的类数据库,其中包括查找与特定字符串关联的所有整数的平均值和计数的方法。但是,我对初始构造函数有点困惑。我的数据库应该在调用 database data();
时创建,但出现了一个巨大的错误。
这是database.cpp中构造函数的实现
database::database()
{
data = new multimap<string,int>; // allocates new space for the database
*data["asdf"]= 0; // adds a point. not needed...?
}
此外,database.h文件看起来像这样。
private:
multimap<string,double> *data;
错误相当密集,我无法破译它,但是当我运行 g++ database.cpp test.cpp 时,就会出现这个错误。
$ g++ database.cpp test.cpp
database.cpp: In constructor ‘database::database()’:
database.cpp:11: error: cannot convert ‘std::multimap<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >*’ to ‘std::multimap<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > >*’ in assignment
database.cpp:12: error: invalid types ‘std::multimap<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > >*[const char [5]]’ for array subscript
编辑:
我忘了添加,谢谢您的帮助!
生锈的
I'm working with C++ for a project. I need to store a pair (string,integer) and access them based on the string. The multimap class seems perfect for that. I am trying to make my own class, database, which will include methods to find the average and count of all the integers associated with a particular string. However, I am a bit confused on the initial constructor. My database should be created when database data();
is called, but it comes up with a huge error.
Here is an implementation of the constructor in database.cpp
database::database()
{
data = new multimap<string,int>; // allocates new space for the database
*data["asdf"]= 0; // adds a point. not needed...?
}
Also, the database.h file looks like so.
private:
multimap<string,double> *data;
The error is rather dense, and I can't decipher it, but when I run g++ database.cpp test.cpp this comes out.
$ g++ database.cpp test.cpp
database.cpp: In constructor ‘database::database()’:
database.cpp:11: error: cannot convert ‘std::multimap<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >*’ to ‘std::multimap<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > >*’ in assignment
database.cpp:12: error: invalid types ‘std::multimap<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > >*[const char [5]]’ for array subscript
Edit:
I forgot to add, thanks for the help!
Rusty
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将
data
声明为multimap*
但随后尝试将data
分配给multimap;*
-- 协调这些。顺便说一句,
database data();
并不是您想要的——它实际上是一个名为data
的空函数的声明,其返回类型为数据库
;请改用数据库数据;
。You declare
data
as amultimap<string,double>*
but then you try to assign todata
amultimap<string,int>*
-- reconcile these.As an aside,
database data();
is not what you want -- it is in fact a declaration of a nullary function nameddata
with a return type ofdatabase
; usedatabase data;
instead.