trouble with std::vector

I have a variable declared as such:

 
  std::vector<DynamicSdmProtocol::DynamicSdm*>  m_devices;


When I try to return the variable:

1
2
3
4
5
6
7
  DynamicSdmProtocol::DynamicSdm* DynamicSdmSlaveProvider::master()
  {
    // Should always have a master
    assert(m_devices.size() > 0);

    return m_devices;
  }


I am getting an error:

1
2
../../../src1/Hardware/DynamicSdmSlaveProvider.cpp:143: 
error: cannot convert 'std::vector<DynamicSdmProtocol::DynamicSdm*, std::allocator<DynamicSdmProtocol::DynamicSdm*> >' to 'DynamicSdmProtocol::DynamicSdm*' in return


I am able to return individual elements of the vector for instance:

1
2
3
4
5
6
7
  DynamicSdmProtocol::DynamicSdm* DynamicSdmSlaveProvider::master()
  {
    // Should always have a master
    assert(m_devices.size() > 0);

    return m_devices[0];
  }


However, I need to pass the whole vector. How might I go about doing that?
Last edited on
Look at the function declaration:
DynamicSdmProtocol::DynamicSdm* DynamicSdmSlaveProvider::master()

What does it say it returns?
DynamicSdmProtocol::DynamicSdm*

What do you want it to return?
std::vector<DynamicSdmProtocol::DynamicSdm*>

So what should it say it returns?
std::vector<DynamicSdmProtocol::DynamicSdm*>

So what should the function declaration be?
std::vector<DynamicSdmProtocol::DynamicSdm*> DynamicSdmSlaveProvider::master()
Last edited on
If you want a reference to the vector,

1
2
3
4
5
6
7
  std::vector<DynamicSdmProtocol::DynamicSdm*>& DynamicSdmSlaveProvider::master()
  {
    // Should always have a master
    assert(m_devices.size() > 0);

    return m_devices;
  }


If you want to make a copy of the vector,
1
2
3
4
5
6
7
  std::vector<DynamicSdmProtocol::DynamicSdm*> DynamicSdmSlaveProvider::master()
  {
    // Should always have a master
    assert(m_devices.size() > 0);

    return m_devices;
  }

Normally, copies of vectors are normally avoided so I would go with the reference. Depends on the design of your program.
Topic archived. No new replies allowed.