使用 c++ 中的 multimap 类创建数据库类

发布于 2024-11-01 04:07:37 字数 2008 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱情眠于流年 2024-11-08 04:07:37

您将 data 声明为 multimap* 但随后尝试将 data 分配给 multimap;* -- 协调这些。

顺便说一句,database data(); 并不是您想要的——它实际上是一个名为 data 的空函数的声明,其返回类型为 数据库;请改用数据库数据;

You declare data as a multimap<string,double>* but then you try to assign to data a multimap<string,int>* -- reconcile these.

As an aside, database data(); is not what you want -- it is in fact a declaration of a nullary function named data with a return type of database; use database data; instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文