函数的原型与类C++中的任何原型都不匹配
prototype for function does not match any in class C++
我不断收到一个错误,说"'void Engine::start(Tank&)'的原型与类'引擎'中的任何原型都不匹配" 此外,它说"'Tank'尚未声明",所有这些都在引擎类中处于同一函数'start'。
//Engine.h
#ifndef ENGINE_H
#define ENGINE_H
using namespace std;
class Engine {
public:
Engine(int);
~Engine() {};
void setTrip(int tr);
int getTrip();
bool isStarted();
void start(Tank &inTank);
void stop();
protected:
bool started;
int trip, numCyl;
};
#endif /* ENGINE_H */
//Engine.cpp
using namespace std;
#include "Engine.h"
#include "Tank.h"
这个.cpp还有更多,但这是错误的函数发生。
void Engine::start(Tank &inTank) {
if(inTank.isEmpty()) {
cout << "Engine cannot startn";
started = false;
}
else {
cout << "Engine startedn";
started = true;
}
}
我在这里的主要内容是用于测试这两个类。
#include "Tank.h"
#include "Engine.h"
#include <cstdlib>
using namespace std;
int main()
{
Tank t(12);
Engine e(4);
e.start(t);
e.stop();
t.refuel();
e.start(t);
e.stop();
return 0;
}
我将添加我的坦克类。
#ifndef TANK_H
#define TANK_H
using namespace std;
class Tank {
public:
Tank(float);
virtual ~Tank();
void consumeFuel(float);
float getFuel();
virtual void refuel();
bool isEmpty();
int getNumTanks();
float getTankCapacity();
void setFuel(float fl);
protected:
static int numTanks;
const float tankCapacity;
float fuel;
bool empty;
};
#endif /* TANK_H */
坦克.cpp
using namespace std;
#include "Tank.h"
#include "Engine.h"
//creates tank with certain capacity taken from parameter.
Tank::Tank(float inCap) {
Tank::tankCapacity(inCap);
}
//I completed the rest of the functions with no errors... so far.lul
每当引用或指针在标头中使用类时,您需要转发声明该类,而不是包含整个.h文件。
因此,在您的情况下,您只需要在 Engine.h 中转发声明类 Tank,并在 Engine.cpp 中包含 Tank.h。
//Engine.h
#ifndef ENGINE_H
#define ENGINE_H
//Forward Declaration
class Tank;
using namespace std;
class Engine {
public:
Engine(int);
~Engine() {};
void setTrip(int tr);
int getTrip();
bool isStarted();
void start(Tank &inTank);
void stop();
protected:
bool started;
int trip, numCyl;
};
#endif /* ENGINE_H */
将 Tank 类声明移到引擎类上方
相关文章:
- 函数向量_指针有不同的原型,我可以构建一个吗
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何在不产生任何垃圾的情况下获得C中的像素
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- 错误:'EM::EM(...)' 的原型与类 'EM' 中的任何原型都不匹配
- 错误:'void TNode::inititTree(TNode*)' 的原型与类 'TNode' 中的任何原型都不匹配
- 错误:原型与类中的任何原型都不匹配
- "...."的原型与类中的任何"..."都不匹配
- C++ 2D 阵列...迷失在原型和电话上,在任何地方都看不到这个
- 你能帮助解决这个错误吗?(g++原型与类中的任何原型都不匹配)
- 函数的原型与类C++中的任何原型都不匹配
- 错误:类的原型与任何类 - 继承都不匹配
- 错误:“XXX”的原型与类“YYY”中的任何原型都不匹配
- 错误:的原型与类中的任何原型都不匹配
- XXX的原型::XXX与类XXX中的任何一个都不匹配
- 为什么即使没有任何类声明也需要原型
- 错误原型与类中的任何错误都不匹配
- 'Figure::Figure()'的原型与类中的任何'Figure'都不匹配
- 如何声明带有void *指针的c++原型,以便它可以接受任何指针类型