对象 X 不是结构 Y 的成员
object x is not a member of struct y
#pragma once
#include "Predefined.h"
#include <string>
#include <vector>
using namespace std;
namespace Packets
{
enum { EnumLoginData, EnumPlayerData };
struct LoginData
{
std::string username;
std::string password;
};
struct PlayerData
{
Predefined::Vector2 position;
};
struct MainPacket
{
char type;
int id;
LoginData loginData;
vector<PlayerData> playerData;
};
}
上面的代码是一个名为 PacketDefines.h 的单个头文件。我准备了几个结构,如您所见,我将在程序的另一部分使用它们。现在,结构 PlayerData 使用一个 Predefined::Vector2 对象,这是我在 Predefined.h 中创建的自定义结构,包含在当前头文件中。
问题是我收到此错误:
error C2146: syntax error : missing ';' before identifier 'position'
此外,这使得代码中的其他内容(依赖于此结构(会导致引发错误:
error C2039: 'position' : is not a member of 'Packets::PlayerData'
这是预定义的头文件:
#pragma once
#include <iostream>
#include <memory>
#include "PacketDefines.h"
// some other includes
using namespace std;
#define LOBBY_MAX_CONNECTIONS 5
#define MAX_DATA_SIZE 512
namespace Predefined
{
struct Vector2
{
Vector2(float valueX = 0.0f, float valueY = 0.0f) : x(valueX), y(valueY) {}
float x;
float y;
};
struct Vector3
{
Vector3(float valueX = 0.0f, float valueY = 0.0f, float valueZ = 0.0f) : x(valueX), y(valueY), z(valueZ) {}
float x;
float y;
float z;
};
struct Connection
{
int ID;
SOCKET socket;
Packets::PlayerData playerData;
};
struct Lobby
{
string lobbyName;
vector<Connection> connectionList;
};
}
我不知道发生了什么,因为据我所知,一切都是相互关联的。我希望我的问题很清楚,有人可以帮助我解决这些错误。
这是一个肤浅的概述,但你的"Predefined.h"包含两个不同的部分;第一个是你的Vector
类,第二个是包含应用程序逻辑的更复杂的类。为了克服这个问题,你实际上需要三个头文件:
-
Vectors.h
-> 应定义Vector2
和Vector3
,并且不包含任何应用程序结构 -
PacketDefines.h
-> 应该#include "Vectors.h"
,没有别的(不需要包括Predefined.h
(。 -
Connection.h
->应该#include "PacketDefines.h"
.
超出这个问题的范围,我还建议将标题名称更改为更有意义的名称(是的,标头通常声明符号并定义结构;那又怎样?(。
相关文章:
- 如何在C++中定义静态成员结构
- 使用模板化类的成员结构
- 初始化固定的 C 数组成员结构
- 指向成员结构的指针如何工作
- 如何将此“指针从外部类传递到成员结构
- 导致IAR ARM中出现错误的成员结构位字段元素的Initializer列表初始化
- 映射文件中成员结构的地址
- Visual C++ 模板类成员结构初始化语法糖
- C :初始化成员结构的静态字段的正确方法
- 如何在C 类的初始化器列表中使用未命名结构初始化成员结构
- 从成员结构的成员函数中访问类的成员?
- 模板化类的成员函数无法返回指向成员结构的指针?
- 重载非类型模板结构的成员结构的复制赋值运算符
- 灵气属性传播问题与单成员结构.
- 将具有 int* 成员C++结构编接到 C#
- 复杂的班级成员结构
- 将向量成员(结构)传递给函数
- 成员结构的前向声明
- 重载运算符<<用于模板结构中定义的成员结构
- 返回带有模板化类的类成员结构