注意:我不认为这与其他十亿个问题有关。我查看了有问题的代码,这不是语法错误(我认为)。
我有以下功能:
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';
}
在文件中先前声明的所有write_
...和read_
...函数。该文件是一个标题(因为我所做的只是包含它)。有一个原因,它总是会返回z
。这是一个黑客,我不确定的工作。编译器给出了以下错误:
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
此标头包含课程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 */
有人知道问题是什么吗?
答案 0 :(得分:4)
您必须使用::
来访问静态函数:
BinIO::AddBinType(typeid(uint16_t), read_uint16, write_uint16);
答案 1 :(得分:2)
C ++中的范围解析运算符是::
,这是您需要用来访问静态方法或成员的方法,与Java或C#不同:
BinIO::AddBinType
而不是
BinIO.AddBinType