Reputation: 311
I'm making a .lib file for a mobile robot. Currently I'm writing a function for scanning Bluetooth devices.
The function is:
struct Device
{
string DeviceName;
BTH_ADDR DeviceAddress;
};
void ScanForDevices(vector<Device> *Robot)
{
m_bt = BluetoothFindFirstRadio(&m_bt_find_radio, &m_radio);
BluetoothGetRadioInfo(m_radio, &m_bt_info);
m_search_params.hRadio = m_radio;
ZeroMemory(&m_device_info, sizeof(BLUETOOTH_DEVICE_INFO));
m_device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
m_bt_dev = BluetoothFindFirstDevice(&m_search_params, &m_device_info);
int m_device_id = 0;
char charDeviceName[250];
do {
charDeviceName[250]=NULL;
Robot->push_back(Device());
//WideCharToMultiByte(CP_UTF8,0,m_device_info.szName,-1, charDeviceName,0,NULL, NULL);
WideCharToMultiByte(CP_UTF8,0,m_device_info.szName,-1, charDeviceName,250,NULL, NULL);
Robot[m_device_id]->DeviceName=(string)charDeviceName;
Robot[m_device_id]->DeviceAddress=m_device_info.Address.ullLong;
m_device_id++;
} while(BluetoothFindNextDevice(m_bt_dev,&m_device_info));
BluetoothFindDeviceClose(m_bt_dev);
BluetoothFindRadioClose(m_bt);
}
I keep getting:
Error 6 error C2819: type 'std::vector<_Ty>' does not have an overloaded member 'operator ->'
Error 7 error C2039: 'DeviceName' : is not a member of 'std::vector<_Ty>'
Error 8 error C2819: type 'std::vector<_Ty>' does not have an overloaded member 'operator ->'
Error 9 error C2039: 'DeviceAddress' : is not a member of 'std::vector<_Ty>'
Error 10 IntelliSense: expression must have pointer type
Error 11 IntelliSense: expression must have pointer type
I'm starting C++ and am not really skilled in the use of pointers.
Any help is welcomed.
Upvotes: 0
Views: 1315
Reputation: 45410
Robot is a pointer points to vector<Device>
. You are confusing yourself with point to vector and vector element accessing. To fix the issue, you could change:
Robot[m_device_id]->DeviceName=(string)charDeviceName;
to
Robot->at(m_device_id).DeviceName = std::string(charDeviceName);
Or
(*Robot)[m_device_id].DeviceName = std::string(charDeviceName);
Suggestion:
Better solution is to pass vector by reference instead of pointer.
void ScanForDevices(vector<Device> &Robot)
{
std::string charDeviceName; //<<-- use std::string instead of char array
Robot[m_device_id].DeviceName = charDeviceName; //<<-- this is better coding style
}
Upvotes: 1
Reputation: 43311
To access vector<Device> *Robot
element by index, use the following syntax:
(*Robot)[m_device_id]
Your code should be:
(*Robot)[m_device_id].DeviceName=(string)charDeviceName;
Note that ->
is replaced by .
because the vector element has Device
type, this is not pointer.
Your existing code matches the following vector declaration:
vector<Device*> Robot
See the difference.
Upvotes: 1