问题描述
我收到一个未声明的标识符错误,我不知道为什么.我什至重写了完整的内容,以确保我没有打错字.有人可以告诉我为什么会出现此错误吗?
I'm getting an undeclared identifier error and I don't know why. I even rewrote the complete stuff to make sure I didn't do a typo. Can someone tell me why I got this error?
我的文件是
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"
Don't mind the CircuitObjects
, they don't give out any errors, errors are: undeclared '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'
Errors: undeclared '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,它不会发出任何错误.
Don't mind LogicSimulator.cpp, it doesn't give out any errors.
错误:未声明连接"(是的,并且在连接LogicSimulator中未声明)
Errors: undeclared 'Connection' (Yeah, and in Connection LogicSimulator is undeclared)
推荐答案
正如我在评论中所写,这很可能是由您的通函包含引起的. Connection.hpp
包括 LogicSimulator.hpp
,它本身也包含 Connection.hpp
.
This is, as I wrote in the comments, most likely caused by your circular includes. Connection.hpp
includes LogicSimulator.hpp
which itself includes Connection.hpp
.
就您而言,您甚至不需要包含.对于指针和引用,可以使用向前声明:
In your case, you don't even need the includes. For Pointers and References, a forward declaration is fine:
Connection.hpp:
Connection.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;
};
LogicSimulator.hpp:
LogicSimulator.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 ++未声明的标识符错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!