1

I am writing a server in C++ and created a class called client to store information about connected clients. I wanted to store the clients in a vector. I have a call

clients.push_back(new client(addr,fd));

to add a client object to the vector clients. I get the following error on compile

server.cpp:67: error: no matching function for call to ‘std::vector<client, std::allocator<client> >::push_back(client*)

I think it has something to do with my misunderstanding of the new keyword and how data is stored/moved in C++. I come from a Java background, so I am not use to pointers and memmory stuff of C++.

Roddy
  • 66,617
  • 42
  • 165
  • 277
dpeterson3
  • 13
  • 1
  • 3
  • There's nothing wrong with that, what does the line where you declare your `clients` vector look like? – Dean Harding Sep 22 '10 at 23:35
  • 7
    You do have [a good introductory C++ book,](http://stackoverflow.com/questions/388242/the-definitive-c++-book-guide-and-list) right? If not, you _really_ need to get one and read it. I'd guess from your post that you probably don't understand the basics of memory management in C++, and it's an area that is fraught with common pitfalls. – James McNellis Sep 22 '10 at 23:36
  • 1
    Likely your vector is not declared as `vector` but as `vector` – John Dibling Sep 23 '10 at 00:15

3 Answers3

4

How did you create your vector?

You need to pass a template parameter of Client* so you'd have std::vector<Client*> clients; if you want to store pointers inside. If you use this method and use raw pointers which point to memory on the heap (such as when created with new), remember that you will need to eventually iterate through each element of your vector and call delete on each element.

Or if you don't mind your Client objects being copied you can use std::vector<Client> clients; and then call clients.push_back(myClient);

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
4

You almost certainly just want to get rid of the new so it's:

clients.push_back(client(addr, fd));

In Java you have to explicitly new all your objects, but in C++ you not only don't need to, but generally want to avoid it when/if at all reasonable.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • I think you misstate. `new` is not "optional", but is an entirely different thing from declaring an automatic instance. They aren't, generally, interchangeable. (In this case, though, unless the apparently defective declaration of of the vector is changed, `new` should not be used (with an added `*` to pass the syntax check) because it will produce a leak, and because it results in unnecessary extra work.) – Hot Licks Oct 01 '11 at 13:14
  • @Daniel: Read the question again. For what the OP asked, Jerry certainly gave the right answer. That certainly doesn't mean that `new` is bad in general, but then he didn't say that. What the OP needs is [a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). And rather urgently. – sbi Oct 01 '11 at 13:21
0

Assuming you can use the boost library, you might also want to consider something like (untested):

typedef ClientSharedPtr boost::shared_ptr<Client>;
std::vector<ClientSharedPtr > clients;
ClientSharedPtr client(new Client());
clients.push_back(client);

This way, you'll get pointers to client automatically managed.

Alternatively, consider providing a copy constructor on Client and then:

std::vector<Client> clients;
Client client;
clients.push_back(client);

A copy of client will then occur when it is pushed on to the vector.

Christopher Hunt
  • 2,071
  • 1
  • 16
  • 20