C++ 未声明的标识符,对象向量
我遇到了一个错误,我不知道如何修复它。我检查了所有的东西,也许我忽略了什么,但我没能找到问题 错误: Fish.hC++ 未声明的标识符,对象向量,c++,vector,undeclared-identifier,C++,Vector,Undeclared Identifier,我遇到了一个错误,我不知道如何修复它。我检查了所有的东西,也许我忽略了什么,但我没能找到问题 错误: Fish.h #pragma once #include <iostream> #include <string> #include "EntityControl.h" class Fish { private: //location of the fish unsigned int _xLocation = 0, _yLocation = 0, _zL
#pragma once
#include <iostream>
#include <string>
#include "EntityControl.h"
class Fish
{
private:
//location of the fish
unsigned int _xLocation = 0, _yLocation = 0, _zLocation = 0;
//name of fish
std::string nameOfFish;
unsigned int speed;
public:
//constructor
Fish(std::string name, unsigned int fishSpeed)
{
nameOfFish = name;
speed = fishSpeed;
}
//getters
int getX() const;
int getY() const;
int getZ() const;
std::string getName() const;
void changeX(unsigned int x);
void changeY(unsigned int y);
void changeZ(unsigned int z);
void move();
~Fish();
};
#pragma once
#include <iostream>
#include <vector>
#include "Fish.h"
#include <random>
#include <array>
#include <Windows.h>
class EntityControl
{
private:
/*Didn't make a separate object because I only
needed the x,y,z of the fish container although it is against the rules of object oriented programming*/
const unsigned int _xContainer = 20;
const unsigned int _yContainer = 60;
const unsigned int _zContainer = 40;
public:
/*grabs the location of a vector of fish pointers, then takes the
vector and accesses every object threw it's data location with dereferencing.
(Controls the movement of the fish and prevents collision)*/
void movementController(std::vector<Fish*> *fishInputVector);//thread!!
//ACHTUNG! fishInput is the fish to check the surrounding of, changing this might bring up unexpected errors
bool CheckCollision(Fish* fishInput , Fish* fishInput2);
int getXContainer() const;
int getYContainer() const;
int getZContainer() const;
~EntityControl();
};
水族馆.cpp(主)
使用名称空间std;
#包括“EntityControl.h”
#包括
#包括
#包括“LooseClulationClass.h”
#包括
#包括“Fish.h”
int main(){
/*不使用新的对象定义,所以我不必在以后删除它们,因为指针不会留在内存中*/
bool running=true;
//为函数定义我的对象
实体控制实体;
松散计算类计算;
矢量鱼矢量;
鱼a(“劳埃德”,200);
fishVector.push_back(&a);
线程t1(&EntityControl::movementController、实体和fishVector);
//std::thread t2(&LooseCalculationClass::userInput,calc);
//t2.分离();
t1.分离();
//主gameloop,打印每条鱼的结果,稍等片刻,然后刷新屏幕
(跑步时){
用于(自动鱼_ptr:fishVector){
计算打印输出xyz(*Fish_ptr);
睡眠(50万);
系统(“暂停”);
//系统(“cls”);
}
}
返回0;
}
EntityControl.h
#pragma once
#include <iostream>
#include <string>
#include "EntityControl.h"
class Fish
{
private:
//location of the fish
unsigned int _xLocation = 0, _yLocation = 0, _zLocation = 0;
//name of fish
std::string nameOfFish;
unsigned int speed;
public:
//constructor
Fish(std::string name, unsigned int fishSpeed)
{
nameOfFish = name;
speed = fishSpeed;
}
//getters
int getX() const;
int getY() const;
int getZ() const;
std::string getName() const;
void changeX(unsigned int x);
void changeY(unsigned int y);
void changeZ(unsigned int z);
void move();
~Fish();
};
#pragma once
#include <iostream>
#include <vector>
#include "Fish.h"
#include <random>
#include <array>
#include <Windows.h>
class EntityControl
{
private:
/*Didn't make a separate object because I only
needed the x,y,z of the fish container although it is against the rules of object oriented programming*/
const unsigned int _xContainer = 20;
const unsigned int _yContainer = 60;
const unsigned int _zContainer = 40;
public:
/*grabs the location of a vector of fish pointers, then takes the
vector and accesses every object threw it's data location with dereferencing.
(Controls the movement of the fish and prevents collision)*/
void movementController(std::vector<Fish*> *fishInputVector);//thread!!
//ACHTUNG! fishInput is the fish to check the surrounding of, changing this might bring up unexpected errors
bool CheckCollision(Fish* fishInput , Fish* fishInput2);
int getXContainer() const;
int getYContainer() const;
int getZContainer() const;
~EntityControl();
};
#pragma一次
#包括
#包括
#包括“Fish.h”
#包括
#包括
#包括
类实体控件
{
私人:
/*没有单独制作一个对象,因为我
需要鱼容器的x,y,z,尽管这违反了面向对象编程的规则*/
常量无符号整数_xContainer=20;
常量无符号整数_yContainer=60;
常量无符号整型容器=40;
公众:
/*获取fish指针向量的位置,然后获取
向量并通过解引用访问每个对象的数据位置。
(控制鱼的移动并防止碰撞)*/
void movementController(std::vector*fishInputVector);//线程!!
//ACHTUNG!fishInput是要检查周围环境的鱼,更改此选项可能会导致意外错误
bool CheckCollision(Fish*fishInput,Fish*fishInput2);
int getXContainer()常量;
int getYContainer()常量;
int getZContainer()常量;
~EntityControl();
};
EntityControl.cpp
#include "Fish.h"
int Fish::getX() const
{
return _xLocation;
}
int Fish::getY() const
{
return _yLocation;
}
int Fish::getZ() const
{
return _zLocation;
}
std::string Fish::getName() const
{
return nameOfFish;
}
void Fish::changeX(unsigned int x)
{
_xLocation = x;
}
void Fish::changeY(unsigned int y)
{
_yLocation = y;
}
void Fish::changeZ(unsigned int z)
{
_zLocation = z;
}
void Fish::move()
{
EntityControl entity;
unsigned int x = rand() % entity.getXContainer();
unsigned int y = rand() % entity.getYContainer();
unsigned int z = rand() % entity.getZContainer();
}
Fish::~Fish()
{
}
#include "EntityControl.h"
/*if the container was much larger,
then multiple threads would be a better solution*/
void EntityControl::movementController(std::vector<Fish*> * fishInputVector)
{
while (true) {
for (auto fish_ptr : *fishInputVector) {
}
}
}
bool EntityControl::CheckCollision(Fish * fishInput, Fish * fishInput2)
{
//collision true/false
bool collision = false;
//collision detectors
bool xCollision = false;
bool yCollision = false;
bool zCollision = false;
//fishInput
unsigned int xOriginal = fishInput->getX();
unsigned int yOriginal = fishInput->getY();
unsigned int zOriginal = fishInput->getZ();
//fishInput2
unsigned int xEntity = fishInput2->getX();
unsigned int yEntity = fishInput2->getY();
unsigned int zEntity = fishInput2->getZ();
//directions, (triggerBox)
if (xOriginal - 1 == xEntity || xOriginal + 1 == xEntity || xOriginal == xEntity) { xCollision = true; }
if (yOriginal - 1 == yEntity || yOriginal + 1 == yEntity || yOriginal == yEntity) { yCollision = true; }
if (zOriginal - 1 == zEntity || zOriginal + 1 == zEntity || zOriginal == zEntity) { zCollision = true; }
//returns true if all 3 directions are true
if (xCollision && yCollision && zCollision) { collision = true; }
return collision;
}
int EntityControl::getYContainer() const
{
return _yContainer;
}
int EntityControl::getXContainer() const
{
return _xContainer;
}
int EntityControl::getZContainer() const
{
return _xContainer;
}
EntityControl::~EntityControl()
{
}
#包括“EntityControl.h”
/*如果容器大得多,
那么多线程将是更好的解决方案*/
void EntityControl::movementController(std::vector*fishInputVector)
{
while(true){
用于(自动fish_ptr:*fishInputVector){
}
}
}
bool EntityControl::CheckCollision(Fish*fishInput,Fish*fishInput2)
{
//碰撞正确/错误
布尔碰撞=假;
//碰撞检测器
bool xCollision=false;
bool yCollision=false;
bool zCollision=false;
//鱼输入
unsigned int xOriginal=fishInput->getX();
unsigned int yOriginal=fishInput->getY();
unsigned int zOriginal=fishInput->getZ();
//鱼输入2
无符号int-xEntity=fishInput2->getX();
unsigned int yEntity=fishInput2->getY();
unsigned int zEntity=fishInput2->getZ();
//方向(触发盒)
如果(xOriginal-1==xEntity | | xOriginal+1==xEntity | | xOriginal==xEntity){xCollision=true;}
如果(yOriginal-1==yEntity | | yOriginal+1==yEntity | | yOriginal==yEntity){yCollision=true;}
如果(zOriginal-1==zEntity | | zOriginal+1==zEntity | | zOriginal==zEntity){zCollision=true;}
//如果所有3个方向均为真,则返回真
如果(xCollision&&yCollision&&zCollision){collision=true;}
返回碰撞;
}
int EntityControl::getYContainer()常量
{
返回容器;
}
int EntityControl::getXContainer()常量
{
返回xContainer;
}
int EntityControl::getZContainer()常量
{
返回xContainer;
}
EntityControl::~EntityControl()
{
}
它以前工作过,不知道到底发生了什么变化。我认为所有的事情都被宣布为应该的,但这已经困扰了我一段时间了。将这些类放在另一个项目中并从那里运行也不起作用。改变一些变量/删除方法也没有改变,但我可能又错过了一些东西。问题是,当您包括EntityControl.h时,您还包括了Fish.h,它指的是Fish.h中定义的
Fish
。然而,EntityControl.h引用Fish.h,由于pragma once
指令,它不会被包括在内。如果删除一次pragma,那么由于循环依赖关系,将得到一个无限循环
要解决循环依赖的问题,请使用前向声明。
从Fish.h中删除#包括“EntityControl.h”
,并写入类EntityControl代码>取而代之
您还可以删除#包括“Fish.h”从EntityControl.h中选择code>并编写类Fish代码>代替它。您的可能副本应生成一个。我很确定你的代码几乎都是无关的。当你甚至不知道错误出现的原因时,你很难知道该发布什么?似乎不起作用,EntityControl上未声明的标识符