gpt4 book ai didi

c++ - 对象 x 不是结构 y 的成员

转载 作者:行者123 更新时间:2023-11-28 06:37:23 26 4
gpt4 key购买 nike

#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 -> 应定义 Vector2Vector3,并且包含您的应用程序结构
  • PacketDefines.h -> 应该 #include "Vectors.h" 而不是别的(不需要包含 Predefined.h ).
  • Connection.h -> 应该#include "PacketDefines.h"

超出这个问题的范围,我还建议将您的 header 名称更改为更有意义的名称(是的, header 通常声明符号和定义结构;那又怎样?)。

关于c++ - 对象 x 不是结构 y 的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26573829/

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