首先,我想为我的英语道歉。我担心这不是我天生的语言。我对这段代码感到很疯狂。
我制作一个小引擎来制作游戏。我实现了一个名为" process"的类。这是将继承构成游戏的所有元素的类。所发生的是通过实现其中两个元素,以便从" process"继承。编译器抛出以下错误:
var Person = (function() {
//private state
var inst = 1;
function Person(name, age) {
this.name = name;
this.age = age;
Object.defineProperty(this, "age", {
get: function() {
return age;
},
set: function(num) {
age = num;
}
});
Object.defineProperty(this, "_id", {
value: inst++
});
}
//Attempt to return number of instances divided by all current Person weights
Person.prototype.aveAge = function() {
return inst;
};
return Person;
}());
var jim = new Person("jim", 32);
var richard = new Person("richard", 27);
richard.age = 28;
var alfie = new Person("alfie", 42);
Person.aveAge() //Returns TypeError: Person.aveAge is not a function
我已经读过很多这方面的问题已经在这个论坛上发表过了,我明白错误的是我没有正确使用一个类(类"进程")因为没有实例化正确或因为它缺少类的东西(未实现所需的方法)。
但是当我阅读并检查我的代码时,我无法找到我犯错误的地方。有人能够看到我错了吗?
这是源代码: https://github.com/yawin123/SDLEngine
"的main.cpp"
||=== Build: Debug in MotorSDL (compiler: GNU GCC Compiler) ===|
include/mainProcess.h |6 | error: invalid use of incomplete type ‘class process’
include/declarativas.h |65| error: forward declaration of ‘class process’
include/exeControl.h |6 | error: invalid use of incomplete type ‘class process’
include/declarativas.h |65| error: forward declaration of ‘class process’
/home/yawin/Dokumentuak/c/MotorSDL/src/engine.cpp | | In destructor ‘virtual Engine::~Engine()’:
/home/yawin/Dokumentuak/c/MotorSDL/src/engine.cpp |11| warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
/home/yawin/Dokumentuak/c/MotorSDL/src/engine.cpp | | In member function ‘void Engine::update()’:
/home/yawin/Dokumentuak/c/MotorSDL/src/engine.cpp |76| warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
||=== Build failed: 4 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
"包括/ declarativas.h"
#include <SDL2/SDL.h>
#include "declarativas.h"
int main()
{
Engine *e = new Engine(true);
e->newWindow("Hola mundo", SDL_WINDOW_OPENGL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_RENDERER_ACCELERATED);
e->newTask(new exeControl());
do
{
e->run();
SDL_Delay(1);
}while(e->isRunning());
e->destroyWindow();
return 0;
}
&#34;包括/ process.h&#34;
#ifndef DECLARATIVAS_H
#define DECLARATIVAS_H
enum keyCode
{
_esc,
_1, _2, _3, _4, _5, _6, _7, _8, _9, _0,
_minus, _plus,
_backspace,
_tab,
_q, _w, _e, _r, _t, _y, _u, _i, _o, _p,
_a, _s, _d, _f, _g, _h, _j, _k, _l,
_z, _x, _c, _v, _b, _n, _m,
_f1, _f2, _f3, _f4, _f5, _f6, _f7, _f8, _f9, _f10, _f11, _f12,
_enter,
_c_enter,
_control,
_l_shift,
_comma,
_point,
_r_shift,
_c_asterisk,
_prn_scr,
_alt,
_caps_lock,
_num_lock,
_scroll_lock,
_home,
_c_home,
_up,
_c_up,
_pgup,
_c_minus,
_left,
_c_left,
_c_center,
_right,
_c_right,
_c_plus,
_end,
_c_end,
_down,
_c_down,
_pgdn,
_c_pgdn,
_ins,
_c_ins,
_del,
_c_del,
_less,
_equals,
_greater,
_asterisk,
_r_alt,
_r_control,
_l_alt,
_menu,
_l_windows,
_r_windows,
_close_window,
_FOO_KEY
};
class Engine;
class process;
class Ventana;
//Procesos
class mainProcess;
class exeControl;
#include "process.h"
#include "engine.h"
#include "Ventana.h"
#include "mainProcess.h"
#include "exeControl.h"
#endif // DECLARATIVAS_H
&#34; SRC / process.cpp&#34;
#ifndef PROCESS_H
#define PROCESS_H
#include "engine.h"
#include "declarativas.h"
class process
{
public:
process();
process(Engine *e);
virtual ~process();
virtual void Update();
int id;
int father;
void setEngine(Engine *e);
protected:
Engine *engine;
};
#endif // PROCESS_H
&#34;包括/ engine.h&#34;
#include "process.h"
process::process(){return;}
process::process(Engine *e)
{
setEngine(e);
}
process::~process()
{
return;
}
void process::setEngine(Engine *e)
{
engine=e;
}
void engine::Update()
{
return;
}
&#34; SRC / engine.cpp&#34;
#ifndef ENGINE_H
#define ENGINE_H
#include "Ventana.h"
#include "process.h"
#include "declarativas.h"
#include <SDL2/SDL.h>
#include <vector>
#include <iostream>
using namespace std;
class Engine
{
public:
Engine(bool debug=false);
virtual ~Engine();
void run();
void newWindow(string i_title, int i_windowMode, int i_posX, int i_posY, int i_width, int i_height, int i_rendererMode);
void destroyWindow();
Ventana* ventana;
/**Ciclo de ejecución**/
void input();
void update();
void render();
/*********************/
/**Control de ejecución*******/
bool isRunning();
void isRunning(bool m_r);
bool m_run=true;
/*****************************/
vector<process*> taskManager; //Gestor de procesos
int newTask(process *task); //Dar de alta procesos
SDL_Event event; //Evento para controlar teclado
bool key[_FOO_KEY]; //Array de teclas pulsadas
/**Control de debug*******/
bool isDebug;
void Debug(string t);
void Debug(int t);
void Debug(float t);
/************************/
};
#endif // ENGINE_H
&#34;包括/ mainProcess.h&#34;
#include "engine.h"
Engine::Engine(bool debug)
{
isDebug=debug;
SDL_Init(SDL_INIT_VIDEO);
}
Engine::~Engine()
{
for(int i=0;i<taskManager.size();i++)
{
delete(taskManager[i]);
}
taskManager.clear();
delete(ventana);
SDL_Quit();
}
void Engine::newWindow(string title, int windowMode, int posX, int posY, int width, int height, int rendererMode)
{
if(ventana)
{
destroyWindow();
}
ventana = new Ventana(title,windowMode,posX,posY,width,height,rendererMode);
}
void Engine::destroyWindow()
{
delete(ventana);
}
void Engine::run()
{
input();
update();
render();
}
void Engine::input()
{
for(int i=0;i<_FOO_KEY;i++)
{
key[i]=false;
}
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT: //Si pulsamos el botón de cerrar ventana
key[_close_window]=true;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
key[_esc]=true;
break;
default:
break;
}
break;
default:
break;
}
}
}
void Engine::update()
{
for(int i=0;i<taskManager.size();i++)
{
taskManager[i]->Update();
}
}
void Engine::render()
{
ventana->render();
}
bool Engine::isRunning()
{
return m_run;
}
void Engine::isRunning(bool m_r)
{
m_run=m_r;
}
int Engine::newTask(process *task)
{
taskManager.push_back(task);
taskManager[taskManager.size()-1]->setEngine(this);
return taskManager.size()-1;
}
void Engine::Debug(string t)
{
if(isDebug)
cout<<t<<endl;
}
void Engine::Debug(int t)
{
if(isDebug)
cout<<t<<endl;
}
void Engine::Debug(float t)
{
if(isDebug)
cout<<t<<endl;
}
&#34; SRC / mainProcess.cpp&#34;
#ifndef MAINPROCESS_H
#define MAINPROCESS_H
#include "process.h"
class mainProcess : public process
{
public:
mainProcess();
virtual ~mainProcess();
virtual void Update();
};
#endif // MAINPROCESS_H
&#34;包括/ exeControl.h&#34;
#include "mainProcess.h"
mainProcess():process(){}
mainProcess::~mainProcess(){}
void mainProcess::Update()
{
return;
}
&#34; SRC / exeControl.cpp&#34;
#ifndef EXECONTROL_H
#define EXECONTROL_H
#include "process.h"
class exeControl : public process
{
public:
exeControl();
virtual ~exeControl();
virtual void Update();
};
#endif // EXECONTROL_H
我认为你不需要其他课程。但如果您需要,可以在上面的github链接中找到。
答案 0 :(得分:1)
您有循环包含问题。
process.h
包括engine.h
,engine.h
包括process.h
。你需要打破这个循环。这通常使用前向声明并在源文件中推送包含来完成。
快速浏览一下,您可以在engine.h
中执行流程的前瞻性声明并删除包含。
注意:通常在类的头文件和同一类的源文件中包含一个标题,但标题中没有必要包含它。
Foo.h
#ifndef FOO_H
#define FOO_H
#include "Bar.h"
class Foo
{
private:
Bar* mBar; // Raw pointer should actually be an appropriate smart pointer type
};
#endif
Foo.cpp
// Nothing required here for this example
Bar.h
#ifndef BAR_H
#define BAR_H
#include "Foo.h"
class Bar
{
private:
Foo mFoo;
};
#endif
Bar.cpp
// Nothing required here for this example
在上面有一个循环包含问题;包含警卫与修复此问题无关。此外,由于Foo
仅使用指向Bar
实例的指针,因此可以使用前向声明来打破循环。
Foo.h
#ifndef FOO_H
#define FOO_H
// Note: #include "Bar.h" is removed
class Bar; // This forward declares the `Bar` class
class Foo
{
private:
Bar* mBar; // Raw pointer should actually be an appropriate smart pointer type
};
#endif
Foo.cpp
#include "Bar.h"
Bar.h
#ifndef BAR_H
#define BAR_H
#include "Foo.h"
class Bar
{
private:
Foo mFoo;
};
#endif
Bar.cpp
// Nothing required here for this example
答案 1 :(得分:0)
这里有一个循环依赖的案例:Engine
类使用process
而process
使用Engine
。这通常是有缺陷的架构的一个指标。
如果您所关心的只是解决编译问题,那么前向声明应该可以解决问题:删除&#34; process.h&#34;来自&#34; engine.h&#34; (反之亦然)并声明
class process;
在&#34; engine.h&#34;报头中。