我无法定义一个地图,该地图将我在类中描述的对象作为值类型。 我正在尝试创建一个书库,每本书都是地图形式,其中的键是一个字符串(书的名称),它的值是一个保存数据的类书。 这是我的代码:
class book{
// properties, fields
string title;
string author;
int isbn;
bool isCheckedOut;
int dayCheckedOut;
// constructor
book(string title, string author, int isbn){
title = title;
author = author;
isbn = isbn;
isCheckedOut = false;
dayCheckedOut = -1;
}
这就是我定义地图的地方:
class library{
map<string,book> bookCollection;
int currentDay;
int lengthOfCheckOutPeriod;
float initialLateFee;
float lateFeePerDay;}
public:
//constructor
library(map<string,book> bookCollection){
bookCollection = bookCollection;}
这是主要的:
int main(){
map<string,book> bookCollection;
book harry("Harry Potter","J.K.Rowling",123123);
book cal("Calculus","Stewart",456456);
bookCollection["Harry Potter"]=harry;
bookCollection["Calculus"]=cal;
library lib(bookCollection);
return 0;
}
当我编译它时引发错误:
c:\program files\microsoft visual studio 10.0\vc\include\map(215): error C2512: 'book::book' : no appropriate default constructor available
1> c:\program files\microsoft visual studio 10.0\vc\include\map(210) : while compiling class template member function 'book &std::map<_Kty,_Ty>::operator [](const std::basic_string<_Elem,_Traits,_Ax> &)'
1> with
1> [
1> _Kty=std::string,
1> _Ty=book,
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> d:\1551036\linh tinh\book library\book library\main.cpp(39) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=book
1> ]
它出了什么问题?有没有替代方法或结构呢?