casting or function prototype problem

Hi all,

I have defined a class which constructor is :

TableInFile(const std::string &aFileName, std::vector<std::string> &aColumnNames, std::vector<std::string> &aColumnTypes, std::vector<void *> &aVectors, std::vector<enum OpeningMode> &aModes);

Please, consider the 4th argument : std::vector<void *> &aVectors

I use it like this :

1
2
3
4
5
6
7
8
vector<string> col = {"string1", "uint2", "float3"};
vector<string> typ = {"string", "u_int32_t", "float"};
vector<string> string1 = {"raw1", "raw2", "raw3", "raw4"};
vector<u_int32_t> uint2 = {111, 222, 333, 444};
vector<float> float3 = {1.1, 2.2, 3.3, 4.4};
vector<void *> vec = {static_cast<void *>(&string1), static_cast<void *>(&uint2), static_cast<void *>(&float3)};
vector<enum OpeningMode> mod = {READ, READ, RW};
TableInFile t("table.txt", col, typ, vec, mod);


and it works.

Then, I have defined the following method :

int addColumn(const std::string &aColumnName, const std::string &aColumnType, void * &aVector, const std::string &aColumnNameToInsertBefore);

The class compiles well, but when I link this library with my test application where I use it like this :

1
2
3
TableInFile t("table.txt", col, typ, vec, mod);
vector<int> int23 = {123, 223, 323, 423};
t.addColumn(string("int23"), string("int"), static_cast<void *>(&int23), string("float3"));


I have the following compilation error :

../src/test.cpp:1044:93: error: no matching function for call to ‘TableInFile::addColumn(std::string, std::string, void*, std::string)’
/home/alain/Documents/Poker/WorkSpace/MyUtils/src/MyFiles.h:40:6: note: candidate is: int TableInFile::addColumn(const std::string&, const std::string&, void*&, const std::string&)
make: *** [src/test.o] Error 1


The error message is quite clear. But after many trials, I cannot manage to make it work. Probably it is obvious, but I cannot catch it.

Help please. What do I do wrong ?
I think the problem is that the function is defined as taking a reference to a pointer, but you pass in a variable that is cast to a pointer using static_cast. I suspect it is impossible to make a reference to the variable after it is cast, so the compiler can only interpret it as a void*, not as a reference to a void*. (Also using a reference to a pointer in this case is probably unnecessary). Change the definition of addColumn to
 
int addColumn(const std::string &aColumnName, const std::string &aColumnType, void * aVector, const std::string &aColumnNameToInsertBefore);
Last edited on
Thanks for your help slicedpan,

For information, the rest of the code is here : http://www.cplusplus.com/forum/beginner/70282/
You can see there the use I make of void*. I recast it to the type of a vector, and it works. The point is that I don't know at compile time the type T of vector<T>.

With int addColumn(const std::string &aColumnName, const std::string &aColumnType, void * aVector, const std::string &aColumnNameToInsertBefore);, it compiles, but I obtain a wrong result :

$ cat .table.txt
string1 uint2 int23 float3 
string u_int32_t int float 
raw1 111 -1338695944 1.1 
raw2 222 959854643 2.2 
raw3 333 875837749 3.3 
raw4 444 2110254 4.4 


I checked with some cout that the argument aVector as the same value than &int23 : 0x7fffffffbff0
Last edited on
&string1 = 0x7fff5a050d30, &uint2 = 0x7fff5a050d10, &float3 = 0x7fff5a050cf0
&int23 = 0x7fff5a050c70
mVectors = 0x7fff5a050d30, 0x7fff5a050d10, 0x7fff5a050c70, 0x7fff5a050cf0, 


The trace of the vectors are just after their creation in the test application program. The trace of mVectors is just at the beginning of the write function. What we can see here is that your proposition works : mVectors contains the right information after the call of addColumn.

It seems that somewhere the content of int23 is corrupted.
After putting traces everywhere, I could localise the corruption at the call of the TableInFile class destructor.

If in my test application, I call at the end explicitely write t.write(); instead of having this call in the TableInFile destructor, then it works.

I thought the class attributes were destroyed only after the code of the customised destructor has finished its execution !

Can someone provide me some insight here please ?
Last edited on
Topic archived. No new replies allowed.