-3

How to add object of class to vector in another class.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class info{
    private: 
        int id;
        string name;
    public:
        info(int extId, string extName) {
            this->id = extId;
            this->name = extName;
        }
};

class db {
    private:
        vector<info> infoVector;
    public:
        void pushData(info * data) {
            this->infoVector.push_back(&data);
        }
};

int main(){ 
    info * testData = new info(123, "nice"); 

    db database;
    database.pushData(testData);    

    return 0;
}

I am creating a object of info class. The object contains one int and one string variables. Then I am creating db object and I am passing there a testData object.

I got error message while building project.

main.cpp: In member function ‘void db::pushData(info*)’:
main.cpp:23:44: error: no matching function for call to ‘std::vector<info>::push_back(info*&)’
             this->infoVector.push_back(data);
                                            ^
In file included from /usr/include/c++/5/vector:64:0,
                 from main.cpp:2:
/usr/include/c++/5/bits/stl_vector.h:913:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = info; _Alloc = std::allocator<info>; std::vector<_Tp, _Alloc>::value_type = info]
       push_back(const value_type& __x)
       ^
/usr/include/c++/5/bits/stl_vector.h:913:7: note:   no known conversion for argument 1 from ‘info*’ to ‘const value_type& {aka const info&}’

What am I doing wrong?

Warszawski Koks
  • 69
  • 1
  • 1
  • 7
  • 2
    It's in the error message "no known conversion for argument 1 from ‘info*’ to ... const info&". You have `vector infoVector;` which accepts `info` types but you're trying to give it a `info*` type. – James Adkison Nov 06 '17 at 17:33
  • 2
    Why do you use new? –  Nov 06 '17 at 17:45
  • It's a bit worse than what James points out. `pushData` takes a pointer and the address of that pointer, a temporary local to the function, is passed to `push_back`. This does not line up with the given error messages, so I think this is the result of some misfires while debugging. Helpful reading: [When to use references vs. pointers](https://stackoverflow.com/questions/7058339/when-to-use-references-vs-pointers) – user4581301 Nov 06 '17 at 18:14

2 Answers2

1

It looks like you are trying to pass the address of an info * type to vector<info>::push_back, which only accepts types of const info & or info &&. Try using the dereference operator * instead of the address-of operator & when you call push_back:

this->infoVector.push_back(*data);

This isn't a great way to use pointers, however, and could lead to memory leakage or segfaults if data is removed from the vector or if it is deleted. It is better for the vector to own its members, so you might consider doing this instead:

class db {
    private:
    vector<info> infoVector;
    public:
    void pushData(info data) {            // note: not a pointer
        this->infoVector.push_back(data); // note: not address-of
    }
};

int main(){ 
    info testData(123, "nice"); // note: not a pointer
    db database;
    database.pushData(testData);
    return 0;
}

Otherwise, if you really want infoVector to contain pointers, declare it as:

std::vector<info*> infoVector;

Then remove the address-to operator.

P.S., avoid using namespace std whenever possible!

PaSTE
  • 4,050
  • 18
  • 26
  • `this->infoVector.push_back(*data);` would probably result in memory leakage. – user4581301 Nov 06 '17 at 18:21
  • Agreed--code and answer updated to make note of this. – PaSTE Nov 06 '17 at 18:49
  • both options works! I mean that I can add the new object to vector. Now I have the second problem. I don't know how to print the object from the vector. I have tried this: `void outPutData() { cout << this->infoVector.back(); }` Then I used this method like that: `database.outPutData()` I does not work. In next step I have created function `getId()` in `info class`. Then in modified `outPutData()` method like that: `cout << this -> infoVector[0].getId();` I got the error: `invalid use of non-static member function` Is there any way to print the values? – Warszawski Koks Nov 06 '17 at 19:39
  • It's difficult to know how to help without seeing the full class definitions you are trying. If an answer posted here is sufficient for your original question, I would suggest accepting one as an answer and asking a new question concerning this new problem. – PaSTE Nov 06 '17 at 21:08
-1

You have vector<info> and you want to put info *, try to do:

int main(){ 
    info testData(123, "nice"); 

    db database;
    database.pushData(testData);    

   return 0;
}
Ivan Sheihets
  • 802
  • 8
  • 17
  • 1
    This is the right direction, but incomplete. `pushData` also needs to be modified. You should show that as well. – user4581301 Nov 06 '17 at 18:07