This is how I fixed the same problem during my cross platform implementations. Above methods worked fine on Windows and Linux (Redhat) but not on Solaris 10 and IBM AIX 7.
Solaris 10 gives following error with above suggested methods.
'nofieldident: data is not a member of const std::vector<unsigned char>'
Here is a way to work with the 'const'
ness of vector data, which will work across Windows, Linux, Solaris
#include <iostream>
#include <vector>
using namespace std;
/// Data Interface, which is visible to the outside
class IDataInterface {
public:
// Get the underlying data as a const data pointer
virtual const unsigned char* Data(size_t& nSize) = 0;
};
/// Implemetation of the IDataInterface
class CData : public IDataInterface {
public:
// Constructor
CData(vector<unsigned char> data);
// Overriden function of the interface
const unsigned char* Data(size_t& nSize);
private:
/// Actual data member
vector<unsigned char> m_vData;
};
/// Constructor implementation
CData::CData(vector<unsigned char> vData) {
// resize to the input data size
m_vData.resize(vData.size());
// Copy the data
memcpy(&m_vData[0], &vData[0], vData.size());
}
/// Implementation of the data function
const unsigned char* CData::Data(size_t& nSize /**< Size of data returned */) {
/*
* Following four methods worked fine on Windows, RedHat, but Failed on Solaris 10 and IBM AIX
*/
// return &m_vData[0];
// return m_vData.data();
// return const_cast<unsigned char*>(reinterpret_cast<const unsigned char *>(m_vData.data()));
// return reinterpret_cast<unsigned char*>(const_cast<unsigned char *>(m_vData.data()));
/* This was tested on following and works fine.
* Windows (Tested on 2008 to 2016, 32/64 bit, R2 versions)
* RedHat 5&7 (32/64 bit)
* Solaris 10 (32/64 bit)
* IBM AIX 7.10
*
*/
return &m_vData.front(); --> This works on windows, redhat, solaris 10, aix
}
/// Main class
int main() {
// Vector of data
vector<unsigned char> vData;
// Add data onto the vector
vData.push_back('a');
vData.push_back('b');
vData.push_back('c');
// Create an instance from the vector of data given
CData oData(vData);
// Get the data from the instance
size_t nSize(0);
cout << "Data:" << oData.Data(nSize) << endl;
return 0;
}
Output:
Data:abc