0

I have the following class:

class Database
{
private:
    vector<myObject*> m_vectorObj;

public: 
    void addObject(myObject &passObj)
}

I'm trying to do this:

void Database::addObject (myObject &passObj)
{
    m_vectorObj.push_back(passObj);
}

But is giving me the error "No matching function to call", how can I make this work and pass the object and store the pointer ?

jww
  • 97,681
  • 90
  • 411
  • 885
IzonFreak
  • 409
  • 1
  • 5
  • 15
  • `m_vectorObj.push_back(& passObj)`, but yeah it's better to change reference to pointer as Vlad suggests. – mip Dec 03 '14 at 21:35

3 Answers3

2

you are passing a reference to a pointer type, which does not equal any prototype call you have.

If you want to add pointers change your code to accept pointer objects:

void Database::addObject (myObject * passObj)
{
   m_vectorObj.push_back(passObj);
}

//somewhere:
Database db;
db.addObject(new myObject);

note that references are usually ment for modyfiing the passed arguments

Gizmo
  • 1,990
  • 1
  • 24
  • 50
0

Scope resolution operator is '::' not ':'. Change void DataBase:addObject to DataBase::addObject.

user2970916
  • 1,146
  • 3
  • 15
  • 37
0

First of all, ask yourself if you really need to use pointers

If you do, then you have 2 options.

1: You are just missing & before passObj

m_vectorObj.push_back(&passObj);
                      ^

2: Change your function (from reference to pointer)

void Database:addObject (myObject *passObj)//pointer to myObject instead of reference
{
    m_vectorObj.push_back(passObj);
}

I wouldn't use pointers at all, but i suggest you to encapsulate'em into std::unique_ptr

Quest
  • 2,764
  • 1
  • 22
  • 44
  • Option 2 is better, +1 for option 2 – mip Dec 03 '14 at 21:40
  • 2
    you have to be careful with `std::unique_ptr`, as you cannot `push_back` it into the vector (it is not copy-constructible), but only `std::move`-it. – vsoftco Dec 03 '14 at 22:02
  • Thank you will apply option 2 :) – IzonFreak Dec 03 '14 at 22:38
  • @IzonFreak Glad I helped you. BTW You should accept an answer – Quest Dec 04 '14 at 16:51
  • I gave you +1 now -1 for `unique_ptr`. Passing unique_ptr would mean that `passObj` changes owner, which may be not the case. When `m_vectorObj` will be destroyed wrapped `passObj` will be destroyed along with it. – mip Dec 04 '14 at 16:53
  • @doc not really. When you use pointers, they will not be deleted when vector goes out of scope! – Quest Dec 04 '14 at 17:02
  • What you mean? If you pass object from one `unique_ptr` to another `unique_ptr` it changes owner. – mip Dec 04 '14 at 17:04
  • @doc but not when you make unique when adding it into vector. Unfortunatelly it was only a suggestion, something that he can use to avoid memory leaks. As i said i wouldn't use pointers at all in this situations. In most cases, it is a sign of a bad programmer. – Quest Dec 04 '14 at 17:10
  • I think the opposite is true. Decent programmer can use pointers without memory leaks. `unique_ptr` is not a replacement of pointers. It's for different things. It's for sole ownership. For shared ownership you can use shared pointers or raw pointers. If method expects raw pointer then caller must guarantee that passed object will be alive for the time it is used within a callee. If this can't be guaranteed then you can use shared pointer, but it's last resort. And you can have subtle and hard to detect memory leaks with `shared_ptr` - worse than with raw pointers. – mip Dec 04 '14 at 17:20
  • @I didn't say that smart pointers are a replacement of pointers. I said that most programmers are using pointers everywhere, even if there is no need for pointer – Quest Dec 04 '14 at 17:23
  • @vsoftco are they even usable within vector, which copies its elements during resize for example? – mip Dec 04 '14 at 18:36
  • @doc, good point, forgot about the resize. If your move ctor and destructor are declared as `noexcept`, then the move ctor will be used during resizing. See http://stackoverflow.com/q/8001823/3093378 – vsoftco Dec 04 '14 at 21:10
  • @doc it s usable. When you declared your move constructor as noexcept, std::vector will use move instead. And for your comments above, when you std::move unique_ptr into a vector, it will not.change ownership – Quest Dec 05 '14 at 06:19
  • @Quest As I read from another question, some functions for `std::vector` are still unusable, so IMO it's not the best idea to use `std::unique_ptr` with `std::vector`. If you move `unique_ptr` to `vector` it changes ownership, why shouldn't it? http://coliru.stacked-crooked.com/a/d7bfd379825f715b – mip Dec 05 '14 at 09:38
  • @doc I was thinking about passing a pointer into a function and directly pass newly created unique_ptr into the vector – Quest Dec 06 '14 at 10:01
  • @Quest this does not make any difference, because newly created `unique_ptr` will take ownership of pointer -> http://coliru.stacked-crooked.com/a/869fd0c892adca93 Using `unique_ptr` in such way does not make any sense. – mip Dec 06 '14 at 12:34