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

时间:2017-04-19 12:20:13

标签: c++

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

我的文件是

Connection.hpp

(\w+)

不介意#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; }; ,他们不会发出任何错误,错误是:未声明的'LogicSimulator'

Connection.cpp

CircuitObjects

错误:未声明的'LogicSimulator'

LogicSimulator.hpp

#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.cpp,它不会发出任何错误。

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

1 个答案:

答案 0 :(得分:2)

正如我在评论中写的那样,这很可能是由你的通告所引起的。 Connection.hpp包含LogicSimulator.hpp,其中包含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:

#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();
};