开发者

Vector error message and how to insert an object into a vector in C++?

I have a Room class and it has this constructor:

Room::Room(string a, int b, int c, string d)

And in my main function I do:

vector<Room> room;
sale.push_back("aaa", 1, 2, "ccc");

It gives me this error:

error: no matching function for call to ‘std::vector<Room, std::allocator<Room> >::push_back(const char [4])’

note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Room, _Al开发者_运维问答loc = std::allocator<Room>]

I don't understand this error. How can I add a new room object into the vector?


Probably something like:

std::vector<Room> rooms;
room.push_back(Room("aaa", 1, 2, "ccc"));


You cannot use the push_back function as you do, just because the push_back function does not replaces the constructor. Here is the solution:

vector<Room> rooms;
Room ins("aaa",1,2,"ccc");
rooms.push_back(ins);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜