我收到错误,但我不知道如何修复它。我检查了一切,也许我忽略了一些事情,但我无法找到问题。
错误:
1>------ Build started: Project: Aquarium, Configuration: Release x64 ------ 1> Fish.cpp 1>c:\users\lloyd17\dropbox\aquarium project\aquarium\aquarium\EntityControl.h(21): error C2065: 'Fish': undeclared identifier 1>c:\users\lloyd17\dropbox\aquarium project\aquarium\aquarium\EntityControl.h(21): error C2059: syntax error: '>' 1>c:\users\lloyd17\dropbox\aquarium project\aquarium\aquarium\EntityControl.h(21): error C2976: 'std::vector': too few template arguments 1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vector(682): note: see declaration of 'std::vector' 1>c:\users\lloyd17\dropbox\aquarium project\aquarium\aquarium\EntityControl.h(24): error C2061: syntax error: identifier 'Fish' 1> LooseCalculationClass.cpp 1>c:\users\lloyd17\dropbox\aquarium project\aquarium\aquarium\EntityControl.h(21): error C2065: 'Fish': undeclared identifier 1>c:\users\lloyd17\dropbox\aquarium project\aquarium\aquarium\EntityControl.h(21): error C2059: syntax error: '>' 1>c:\users\lloyd17\dropbox\aquarium project\aquarium\aquarium\EntityControl.h(21): error C2976: 'std::vector': too few template arguments 1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vector(682): note: see declaration of 'std::vector' 1>c:\users\lloyd17\dropbox\aquarium project\aquarium\aquarium\EntityControl.h(24): error C2061: syntax error: identifier 'Fish' 1>LooseCalculationClass.cpp(7): warning C4551: function call missing argument list ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Fish.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();
};
Fish.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()
{
}
Aquarium.cpp(主)
using namespace std;
#include "EntityControl.h"
#include <Windows.h>
#include <time.h>
#include "LooseCalculationClass.h"
#include <thread>
#include "Fish.h"
int main() {
/*Not using new in object definitions so I don't have to delete them afterwards since pointers don't stay in memory*/
bool running = true;
//defining my objects for functions
EntityControl entity;
LooseCalculationClass calc;
vector<Fish*> fishVector;
Fish a("Lloyd", 200);
fishVector.push_back(&a);
std::thread t1(&EntityControl::movementController, entity, &fishVector);
//std::thread t2(&LooseCalculationClass::userInput, calc);
//t2.detach();
t1.detach();
//main gameloop, prints out the results for every fish, waits a bit and then refreshes the screen
while(running){
for (auto Fish_ptr : fishVector) {
calc.printOutXYZ(*Fish_ptr);
Sleep(500000);
system("pause");
//system("cls");
}
}
return 0;
}
EntityControl.h
#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();
};
EntityControl.cpp
#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()
{
}
以前工作过,不知道究竟发生了什么变化。我认为所有事情都应该被宣布,但这已经困扰了我一段时间。将类放在另一个项目中并从那里运行它也没有工作。没有改变某些变量/删除方法,但同样,我可能错过了一些东西。
答案 0 :(得分:1)
问题在于,当你包含 EntityControl.h 时,你还包含了 Fish.h ,它引用了 Fish.h中定义的Fish
。但是 EntityControl.h 指的是 Fish.h ,由于pragma once
指令而不会包含它。如果你删除pragma once
,那么由于循环依赖,你会得到一个无限循环。
要解决循环依赖的问题,请使用前向声明。
从 Fish.h 中删除#include "EntityControl.h"
并改为编写class EntityControl;
。
您也可以从 EntityControl.h 中删除#include "Fish.h";
并代替class Fish;
。