Reputation: 35783
NOTE: I don't think this is a duplicate of the billion other questions about this. I have looked over the code in question and it is not a syntax error (I think).
I have the following function:
char init_fundementals()
{
BinIO.AddBinType(typeid(uint16_t), read_uint16, write_uint16);
BinIO.AddBinType(typeid(char*), read_cstr, write_cstr);
BinIO.AddBinType(typeid(uint32_t), read_uint32, write_uint32);
return 'z';
}
Where all of the write_
... and read_
... function declared earlier in the file. The file is a header (because all I do is include it). There is a reason why this returns z
always. It is a hack, and one I am not sure works. The compiler gives me the following error:
fundemental_bin_types.h:70:10: error: expected unqualified-id before ‘.’ token
fundemental_bin_types.h:71:10: error: expected unqualified-id before ‘.’ token
fundemental_bin_types.h:72:10: error: expected unqualified-id before ‘.’ token
This header includes the header for the class BinIO
.
BinIO.h:
#ifndef BINIO_H
#define BINIO_H
#include <fstream>
#include <boost/shared_ptr.hpp>
#include <map>
#include <typeinfo>
using namespace std;
typedef boost::shared_ptr<fstream> fstream_ptr;
class BinIO {
public:
BinIO();
static void AddBinType(type_info t_name, void(*reader)(fstream_ptr, void*), void(*writer)(fstream_ptr, void*));
protected:
static map<char*, void(*)(fstream_ptr, void*)> typemap_r;
static map<char*, void(*)(fstream_ptr, void*)> typemap_w;
};
#endif /* BINIO_H */
BinIO.cpp:
#include "BinIO.h"
BinIO::typemap_r=map<char*, void(*)(fstream_ptr, void*)>;
BinIO::typemap_w=map<char*, void(*)(fstream_ptr, void*)>;
BinIO::BinIO() {
}
BinIO::AddBinType(type_info t_name, void(*)(fstream_ptr,void*) reader, void(*)(fstream_ptr,void*) writer)
{
typemap_r.insert(pair<char*, void(*)(fstream_ptr, void*)>(t_name.name(), reader));
typemap_w.insert(pair<char*, void(*)(fstream_ptr, void*)>(t_name.name(), writer));
}
fundamental_bin_types.h:
#ifndef FUNDEMENTAL_BIN_TYPES_H
#define FUNDEMENTAL_BIN_TYPES_H
#include <cstring>
#include <arpa/inet.h>
#include "BinIO.h"
using namespace std;
void read_uint32(fstream_ptr in, void* out)
{
char data[sizeof(uint32_t)];
(*in).read(data, 4);
*((uint32_t*)out)=ntohl(reinterpret_cast<uint32_t>(data));
}
void write_uint32(fstream_ptr out, void* in)
{
(*out).write(reinterpret_cast<char*>(htonl(*(uint32_t*)in)), sizeof(uint32_t));
}
//
void read_cstr(fstream_ptr in, void* out)
{
char* buff;
char* size;
(*in).read(size, sizeof(size_t));
(*in).read(buff, reinterpret_cast<size_t>(size));
*((char*)out)=*buff;
}
void write_cstr(fstream_ptr out, void* in)
{
(*out).write(reinterpret_cast<char*>(in), sizeof(size_t));
(*out).write((char*)in, strlen((char*)in));
}
//
void read_uint16(fstream_ptr in, void* out)
{
char data[sizeof(uint16_t)];
(*in).read(data, sizeof(uint16_t));
*((uint16_t*)out)=ntohs(*reinterpret_cast<uint16_t*>(data));
}
void write_uint16(fstream_ptr out, void* in)
{
(*out).write(reinterpret_cast<char*>(htons(*((uint16_t*)in))), sizeof(uint16_t));
}
//
void read_bindata(fstream_ptr in, void* out)
{
uint32_t* size;
read_uint32(in, size);
out=new char[*size];
(*in).read((char*)out, *size);
}
void write_bindata(fstream_ptr out, void* in)
{
char buff[4];
for(int i=0; i<4; i++)
{
buff[i]=*(((char*)in)+i);
}
(*out).write(buff, 4);
(*out).write(((char*)in)+4, reinterpret_cast<uint32_t>(buff));
}
void init_fundementals()
{
BinIO.AddBinType(typeid(uint16_t), read_uint16, write_uint16);
BinIO.AddBinType(typeid(char*), read_cstr, write_cstr);
BinIO.AddBinType(typeid(uint32_t), read_uint32, write_uint32);
}
#endif /* FUNDEMENTAL_BIN_TYPES_H */
Does someone know what the problem is?
Upvotes: 1
Views: 8102
Reputation: 258558
The scope resolution operator in C++ is ::
, and that's what you need to use to access static methods or members, unlike Java or C#:
BinIO::AddBinType
instead of
BinIO.AddBinType
Upvotes: 2
Reputation: 229563
You have to use ::
to access the static function:
BinIO::AddBinType(typeid(uint16_t), read_uint16, write_uint16);
Upvotes: 4