creating a database class using the multimap class in c++
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
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.
精彩评论