gpt4 book ai didi

c++ - "expected unqualified-id before ‘.’ token”错误--C++

转载 作者:行者123 更新时间:2023-11-30 00:57:10 25 4
gpt4 key购买 nike

注意:我不认为这是与此有关的十亿个其他问题的重复。我查看了有问题的代码,这不是语法错误(我认为)。

我有以下功能:

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

此 header 包括类 BinIO 的 header 。

二进制IO.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 */

有人知道问题出在哪里吗?

最佳答案

您必须使用 :: 来访问静态函数:

BinIO::AddBinType(typeid(uint16_t), read_uint16, write_uint16);

关于c++ - "expected unqualified-id before ‘.’ token”错误--C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8771037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com