gpt4 book ai didi

C++ 未声明的标识符错误

转载 作者:行者123 更新时间:2023-11-30 03:32:22 34 4
gpt4 key购买 nike

我收到一个未声明的标识符错误,我不知道为什么。我什至重写了完整的东西以确保我没有打错字。谁能告诉我为什么会出现此错误?

我的文件是

Connection.hpp

#pragma once

#include "../LogicSimulator.hpp"
#include "CircuitObject.hpp"

class Connection
{
public:
Connection(CircuitObject& c1, CircuitObject& c2, LogicSimulator&
simulator);
private:
int state;
CircuitObject& c1;
CircuitObject& c2;
LogicSimulator& simulator;
};

不要介意 CircuitObjects,它们不会给出任何错误,错误是:未声明的“LogicSimulator”

Connection.cpp

#pragma once

#include "Connection.hpp"
#include "../LogicSimulator.hpp"
#include "CircuitObject.hpp"

Connection::Connection(CircuitObject& c1, CircuitObject& c2, LogicSimulator&
simulator) : c1(c1), c2(c2), simulator(simulator)
{
state = c1.State;
}

错误:未声明的“LogicSimulator”

LogicSimulator.hpp

#pragma once

#include "Simulator\CircuitObject.hpp"
#include "Simulator\Connection.hpp"
#include <SFML\Graphics.hpp>
#include <vector>

class LogicSimulator
{
public:
std::vector<CircuitObject*> circuitObjects;
std::vector<CircuitObject*> selectedCircuitObjects;
std::vector<Connection*> connections;
sf::RenderWindow Window;

void Init();
private:
void start();
void draw();
};

不要介意 LogicSimulator.cpp,它不会给出任何错误。


错误:未声明的“连接”(是的,并且在连接中 LogicSimulator 未声明)

最佳答案

正如我在评论中所写,这很可能是由您的通函包含引起的。 Connection.hpp 包含 LogicSimulator.hpp,后者本身包含 Connection.hpp

在您的情况下,您甚至不需要包含。对于指针和引用,前向声明是可以的:

连接.hpp:

#pragma once

class CircuitObject;
class LogicSimulator;

class Connection
{
public:
Connection(CircuitObject& c1, CircuitObject& c2, LogicSimulator&
simulator);
private:
int state;
CircuitObject& c1;
CircuitObject& c2;
LogicSimulator& simulator;
};

逻辑模拟器.hpp:

#pragma once

#include <SFML\Graphics.hpp>
#include <vector>

class CircuitObject;
class Connection;

class LogicSimulator
{
public:
std::vector<CircuitObject*> circuitObjects;
std::vector<CircuitObject*> selectedCircuitObjects;
std::vector<Connection*> connections;
sf::RenderWindow Window;

void Init();
private:
void start();
void draw();
};

关于C++ 未声明的标识符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43495589/

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