Question

I have a vector that takes in a class object but when I try to push back the object I have created into the vector I am getting these problems and don't know how to getnaround it. Can anyone help me please ?

    void populate( std::vector<NV*> vNav, NV *nVess); 

creating the object class

    NV vPB;

object class variables matching up

    vPB.name = namE;
    vPB.type = typE;
    vPB.length = lengtH;
    vPB.speed = speeD;
    vPB.range = rangE;
    vPB.serialNum = serialNuM;
    vPB.serialNum = beaM;
    vPB.displacement = displacemenT;
    vPB.draft = drafT;
    vPB.isActive = isActivE;

    namE = name;
    typE = type;
    lengtH = length;
    speeD = speed;
    rangE = range;
    serialNuM = serialNum;
    beaM = beam;
    displacemenT = displacement;
    drafT = draft;
    isActivE = isActive;

    vNav.push_back(vPB);

keep getting error error C2664: cannot convert parameter 1 from 'N_V::NV' to 'N_V::NV *&&'

and also keep getting error IntelliSence no instance of overload function

Was it helpful?

Solution

The vector is expecting a pointer to an object whereas the vPB you are pushing back is not a pointer to an object but rather the object itself.

The minimum you can do to fix this is:

 NV* vPB = new NV();
 vPB->name = namE;
 ... and similarly for all the members you need to initialize ...
 vNav.push_back(vPB);

Secondly the vNav vector is only going to exist within the context of that function because it is being passed by value. You probably want to take the argument by reference. This means changing your function's signature to:

void populate( std::vector<NV*>& vNav, NV *nVess); 

Lastly it's not advisable to use naked pointers if you can help it. Try looking into shared_ptr and unique_ptr as a more advanced step to ensure that the object's lifetime is correctly managed and you don't leak memory allocations.

OTHER TIPS

vNav is declared as std::vector<NV*>, but you try to push a NV type into there, an object not a pointer. So it's a type mismatch. You also pass the vector by value, which is probably not intended.

Using vector of pointers will be quite problematic. Because you need to manually allocate space for objects and then remember to allocate them. I would advise to revise the code to use std::vector<NV> or at least read about smart pointers.

As others pointed out, you are trying to insert an object where the type should be object pointer. Another error is you are passing the vector by value, it should be by reference, otherwise the caller will not see the effect of insertion.

          void populate( std::vector<NV*>& vNav, NV *nVess); 

I would write your populate function as

void populate( std::vector<NV>& vNav, NV& nVess); 

unless you want to add pointers to the objects inside the vector. And it is not clear where you get this error. Can you post the code where you get the error? Is it in push_back? If yes, then it's because you are trying to push back an object but your vector expects a pointer to object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top