[英]Why am I getting a “unknown type name Opponent” error? (Details under code)
player.h file播放器.h 文件
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include "cpputils/graphics/image.h"
class Player {
public:
// Constructor
Player() {}
Player(int x, int y) {
x_ = x;
y_ = y;
}
// Getters and Setters for x_ and y_
int GetX() const { return x_; }
int GetY() const { return y_; }
void SetX(int x) { x_ = x; }
void SetY(int y) { y_ = y; }
// Getters for kWidth and kHeight
int GetWidth() const { return kWidth_; }
int GetHeight() const { return kHeight_; }
// Draw
void Draw(graphics::Image& GameImage) {
GameImage.DrawRectangle(22 + x_, 27 + y_, 8, 9, 220, 0, 0);
GameImage.DrawRectangle(5 + x_, 27 + y_, 9, 9, 220, 0, 0);
GameImage.DrawRectangle(39 + x_, 27 + y_, 8, 9, 220, 0, 0);
GameImage.DrawRectangle(12 + x_, 27 + y_, 10, 9, 90, 90, 90);
GameImage.DrawRectangle(30 + x_, 27 + y_, 9, 9, 90, 90, 90);
GameImage.DrawRectangle(5 + x_, 23 + y_, 42, 4, 90, 90, 90);
GameImage.DrawRectangle(5 + x_, 36 + y_, 42, 4, 90, 90, 90);
GameImage.DrawRectangle(12 + x_, 19 + y_, 27, 4, 0, 150, 255);
GameImage.DrawRectangle(14 + x_, 15 + y_, 23, 4, 0, 150, 255);
GameImage.DrawRectangle(16 + x_, 11 + y_, 19, 4, 0, 150, 255);
}
bool IntersectsWith(const Opponent& opponent);
bool IntersectsWith(const OpponentProjectile& opponentprojectile);
private:
int x_;
int y_;
int kWidth_;
int kHeight_;
};
class PlayerProjectile {
public:
// Constructor
PlayerProjectile() {}
PlayerProjectile(int x, int y) {
x_ = x;
y_ = y;
}
// Getters and Setters for x_ and y_
int GetX() const { return x_; }
int GetY() const { return y_; }
void SetX(int x) { x_ = x; }
void SetY(int y) { y_ = y; }
// Getters for kWidth and kHeight
int GetWidth() const { return kWidth_; }
int GetHeight() const { return kHeight_; }
// Draw
void Draw(graphics::Image& GameImage) {
GameImage.DrawRectangle(21 + x_, 7 + y_, 12, 13, 0, 220, 0);
GameImage.DrawRectangle(24 + x_, 20 + y_, 6, 5, 0, 220, 0);
GameImage.DrawRectangle(25 + x_, 25 + y_, 4, 17, 0, 220, 0);
}
bool IntersectsWith(const Opponent& opponent);
private:
int x_;
int y_;
int kWidth_;
int kHeight_;
};
#endif
opponent.h file对手.h文件
#include "cpputils/graphics/image.h"
class Opponent {
public:
// Constructor
Opponent() {}
Opponent(int x, int y) {
x_ = x;
y_ = y;
}
// Getters and Setters for x_ and y_
int GetX() const { return x_; }
int GetY() const { return y_; }
void SetX(int x) { x_ = x; }
void SetY(int y) { y_ = y; }
// Getters for kWidth and kHeight
int GetWidth() const { return kWidth_; }
int GetHeight() const { return kHeight_; }
// Draw
void Draw(graphics::Image& GameScreen) {
GameScreen.DrawRectangle(17 + x_, 34 + y_, 20, 7, 155, 155, 155);
GameScreen.DrawRectangle(10 + x_, 14 + y_, 34, 20, 155, 155, 155);
GameScreen.DrawRectangle(17 + x_, 7 + y_, 20, 7, 155, 155, 155);
GameScreen.DrawRectangle(10 + x_, 23 + y_, 34, 3, 60, 60, 60);
GameScreen.DrawRectangle(28 + x_, 29 + y_, 16, 3, 60, 60, 60);
GameScreen.DrawRectangle(10 + x_, 16 + y_, 16, 3, 60, 60, 60);
GameScreen.DrawCircle(33 + x_, 18 + y_, 4, 60, 60, 60);
}
private:
int x_;
int y_;
int kWidth_ = 50;
int kHeight_ = 50;
};
class OpponentProjectile {
public:
// Constructor
OpponentProjectile() {}
OpponentProjectile(int x, int y) {
x_ = x;
y_ = y;
}
// Getters and Setters for x_ and y_
int GetX() const { return x_; }
int GetY() const { return y_; }
void SetX(int x) { x_ = x; }
void SetY(int y) { y_ = y; }
// Getters for kWidth and kHeight
int GetWidth() const { return kWidth_; }
int GetHeight() const { return kHeight_; }
// Draw
void Draw(graphics::Image& o_proj) {
o_proj.DrawRectangle(21 + x_, 7 + y_, 12, 13, 220, 0, 0);
o_proj.DrawRectangle(24 + x_, 20 + y_, 6, 5, 220, 0, 0);
o_proj.DrawRectangle(25 + x_, 25 + y_, 4, 17, 220, 0, 0);
}
private:
int x_;
int y_;
int kWidth_ = 50;
int kHeight_ = 50;
};
Basically, I have the declarations for the IntersectsWith functions in the player.h file, then the implementation for these functions in a different cc file.基本上,我在 player.h 文件中有 IntersectsWith 函数的声明,然后在不同的 cc 文件中实现这些函数。 Im getting an error in player.h thats calling "Opponent" and "OpponentProjectile" unidentified types even though I have those classes created in the opponent.h file.
我在 player.h 中遇到错误,即调用“Opponent”和“OpponentProjectile”未识别类型,即使我在对手.h 文件中创建了这些类。 I tried including opponent.h in player.h but that created so many redefinition problems that I assumed that was not the solution.
我尝试在 player.h 中包含对手.h,但这会产生很多重新定义问题,我认为这不是解决方案。 Any help?
有什么帮助吗?
opponent.h
should be made accessible by player.h
by including the former in the preamble of player.h
.通过将前者包含在
player.h
的序言中,可以让player.h
访问opponent.h
.h。
Also, there should be a player.cpp
file with methods definitions, and that file too shall have a #include "opponent.h"
line in the preamble.此外,应该有一个带有方法定义的
player.cpp
文件,并且该文件也应该在序言中包含#include "opponent.h"
行。
Moreover, you declare bool IntersectsWith(const Opponent& opponent);
此外,您声明
bool IntersectsWith(const Opponent& opponent);
two times inside player.h
which leads to redundancy.在
player.h
中两次,这会导致冗余。
Here below the updated player.h
:在更新的
player.h
:
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include "cpputils/graphics/image.h"
#include "opponent.h" // added #include
class Player {
public:
// Constructor
Player() {}
Player(int x, int y) {
x_ = x;
y_ = y;
}
// Getters and Setters for x_ and y_
int GetX() const { return x_; }
int GetY() const { return y_; }
void SetX(int x) { x_ = x; }
void SetY(int y) { y_ = y; }
// Getters for kWidth and kHeight
int GetWidth() const { return kWidth_; }
int GetHeight() const { return kHeight_; }
// Draw
void Draw(graphics::Image& GameImage) {
GameImage.DrawRectangle(22 + x_, 27 + y_, 8, 9, 220, 0, 0);
GameImage.DrawRectangle(5 + x_, 27 + y_, 9, 9, 220, 0, 0);
GameImage.DrawRectangle(39 + x_, 27 + y_, 8, 9, 220, 0, 0);
GameImage.DrawRectangle(12 + x_, 27 + y_, 10, 9, 90, 90, 90);
GameImage.DrawRectangle(30 + x_, 27 + y_, 9, 9, 90, 90, 90);
GameImage.DrawRectangle(5 + x_, 23 + y_, 42, 4, 90, 90, 90);
GameImage.DrawRectangle(5 + x_, 36 + y_, 42, 4, 90, 90, 90);
GameImage.DrawRectangle(12 + x_, 19 + y_, 27, 4, 0, 150, 255);
GameImage.DrawRectangle(14 + x_, 15 + y_, 23, 4, 0, 150, 255);
GameImage.DrawRectangle(16 + x_, 11 + y_, 19, 4, 0, 150, 255);
}
bool IntersectsWith(const Opponent& opponent);
bool IntersectsWith(const OpponentProjectile& opponentprojectile);
private:
int x_;
int y_;
int kWidth_;
int kHeight_;
};
class PlayerProjectile {
public:
// Constructor
PlayerProjectile() {}
PlayerProjectile(int x, int y) {
x_ = x;
y_ = y;
}
// Getters and Setters for x_ and y_
int GetX() const { return x_; }
int GetY() const { return y_; }
void SetX(int x) { x_ = x; }
void SetY(int y) { y_ = y; }
// Getters for kWidth and kHeight
int GetWidth() const { return kWidth_; }
int GetHeight() const { return kHeight_; }
// Draw
void Draw(graphics::Image& GameImage) {
GameImage.DrawRectangle(21 + x_, 7 + y_, 12, 13, 0, 220, 0);
GameImage.DrawRectangle(24 + x_, 20 + y_, 6, 5, 0, 220, 0);
GameImage.DrawRectangle(25 + x_, 25 + y_, 4, 17, 0, 220, 0);
}
// bool IntersectsWith(const Opponent& opponent); ELIMINATE THIS
private:
int x_;
int y_;
int kWidth_;
int kHeight_;
};
#endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.