Header file is inaccessible - c++

I'm using cocos2d-x to create my project but I'm getting this error:
Error (active) "CocosDenshion::SimpleAudioEngine::SimpleAudioEngine()"
(declared at line 256 of "c:\MyGame
\cocos2d\cocos\audio\include\SimpleAudioEngine.h") is
inaccessible MyGame c:\MyGame \Classes\MyGame .cpp
So I am including the SimpleAudioEngine.h file into my CPP file, to work with it. As you can see from the error, to use the SimpleAudioEngine, I need to use the CocosDenshion namespace first, but as soon as I am done typing:
CocosDenshion::SimpleAudioEngine()
Visual Studio shows this error to me, VS can show me the declaration, so that tells me that it knows where the header is and it can be read. So I don't know what is the issue for being inaccessible. What are some reason for header files to be inaccessible?
MyGame.cpp
#include "MyGame.h"
#include "SimpleAudioEngine.h"
#include "GlobalVariables.h"
USING_NS_CC;
Scene* MyGame::createScene()
{
auto scene = Scene::create();
auto layer = MyGame::create();
scene->addChild(layer);
return scene;
}
bool MyGame::init()
{
if (!Layer::init())
{
return false;
}
is_dragged = false;
const char* MUSIC_PATH = "Music/Main_Theme_loop.ogg";
initTouch();
initTiled();
tempSetupSprite();
debugDrawLine();
this->scheduleUpdate();
return true;
}
MyGame.h
#include "GameSprite.h"
#include "GameMap.h"
class MyGame : public cocos2d::Layer
{
private:
void update(float dt);
void initTouch();
void initTiled();
void tempSetupSprite();
void debugDrawLine();
public:
static cocos2d::Scene* createScene();
virtual bool init();
virtual bool onTouchBegan(cocos2d::Touch* _touch, cocos2d::Event* _event);
virtual void onTouchEnded(cocos2d::Touch* _touch, cocos2d::Event* _event);
virtual void onTouchMoved(cocos2d::Touch* _touch, cocos2d::Event* _event);
virtual void onTouchCancelled(cocos2d::Touch* _touch, cocos2d::Event* _event);
CREATE_FUNC(MyGame);
private:
bool is_dragged;
Vec2 first_touch;
Vec2 last_drag_touch;
GameSprite* sprite;
GameMap* map;
};

Looking at the documentation of cocos2d-x (http://www.cocos2d-x.org/reference/native-cpp/V3.5/de/d8f/class_cocos_denshion_1_1_simple_audio_engine.html). The constructor is protected. You have to use the following method to get the shared static simple audio engine instance:
getInstance()

Check "cocos2d.h" and you will find that "SimpleAudioEngine.h" is not included here. So you have to include it first when you try to use it.
Locating the header file by VS doesn't mean that the file is accessible in your cpp file. It is just a convenient function provided by VS to let the user easy to check the file.

Related

Undefined reference to vtable in qt despite defing all my functions except the signal?

I am aware that this error usually occurs due to the parent class having a virtual function and that virtual function not being defined in the subclass.
I have , to my knowledge at least defined all the functions bar the function that is supposed to be the signal.
This the .h file for the class
#ifndef HREAD_H
#define HREAD_H
#include "package.h"
#include <QThread>
class hread:public QThread
{
Q_OBJECT
signals:
void transfer(structureData* pkg);
public:
hread();
virtual ~hread();
private:
structureData Send;
bool abort,restart;
protected:
void run() override;
};
#endif // INPUTTHREAD_H
This is my main .cpp file
hread::hread()
{
MutexSend.lock();
restart=false;
abort=false;
MutexSend.unlock();
}
hread::~hread()
{
}
void hread::run()
{
emit transfer(&pkg);
}
I can't quite pinpoint why I got this error.

How to call a pure virtual base class method implemented by subclass function?

I have two classes: AbstractClass and SubClass.
This is basically my code (well, just some example code):
abstractclass.h
class AbstractClass
{
public:
AbstractClass();
void doSomething();
protected:
virtual void implementMe() = 0;
int a;
};
abstractclass.cpp
#include "abstractclass.h"
AbstractClass::AbstractClass(){}
void AbstractClass::doSomething()
{
implementMe(); // compiler error: "implementMe() was not declared in this scope"
a = 0; // same compiler error here...
}
subclass.h
#include "abstractclass.h"
class SubClass : public AbstractClass
{
public:
SubClass();
protected:
void implementMe();
};
subclass.cpp
#include "subclass.h"
SubClass::SubClass() {}
void SubClass::implementMe()
{
// do some stuff
}
In the AbstractClass, however, I keep getting a compiler error (for the virtual function as well as for the class variable):
implementMe() was not declared in this scope
The only way I found to get rid of this was to use forward-declaration:
void implementMe();
AbstractClass::doSomething()
{
implementMe();
}
I cannot believe that this is the correct way, though?
Thanks!
EDIT:
Ok, as my conceptual understanding of subclassing in C++ doesn't seem to be totally wrong (see the comments), I'm gonna share some of my original source code. Hopefully this will help to indentify the error.
This is my abstract / base class:
abstractenvironment.h
#ifndef ABSTRACTENVIRONMENT_H
#define ABSTRACTENVIRONMENT_H
#include <QObject>
class AbstractEnvironment : public QObject
{
Q_OBJECT
public:
AbstractEnvironment(QObject *parent = 0);
protected:
virtual void process() = 0;
quint32 counter;
private slots:
void handleTimeout();
};
#endif // ABSTRACTENVIRONMENT_H
abstractenvironment.cpp
#include "abstractenvironment.h"
#include <QTimer>
QTimer *myTimer;
AbstractEnvironment::AbstractEnvironment(QObject *parent) :
QObject(parent)
{
myTimer = new QTimer(this);
connect(myTimer, &QTimer::timeout, this, &AbstractEnvironment::handleTimeout);
myTimer->start(1);
counter = 0;
}
void handleTimeout()
{
process();
counter++;
}
And this is my subclass:
environment.h
#ifndef ENVIRONMENT_H
#define ENVIRONMENT_H
#include "abstractenvironment.h"
class Environment : public AbstractEnvironment
{
Q_OBJECT
public:
Environment(Controller *controller, QObject *parent = 0);
protected:
void process();
};
#endif // ENVIRONMENT_H
environment.cpp
#include "environment.h"
Environment::Environment(Controller *controller, QObject *parent) :
AbstractEnvironment(controller, parent) {}
void Environment::process()
{
// do something
}
PS: I've learned from the first part of this question and tried to compile the source code above inside Qt with MinGW. I get exactly two error messages (as expected):
..\untitled\abstractenvironment.cpp: In function 'void handleTimeout()':
..\untitled\abstractenvironment.cpp:17:13: error: 'process' was not declared in this scope
..\untitled\abstractenvironment.cpp:18:5: error: 'counter' was not declared in this scope
In case you want to try it yourself, I've zipped the Qt project and uploaded it to my Dropbox (of course I will remove this file at some point but the code is exactly the same as in the post above --> it's just for the sake of convenience, so you don't have to copy-paste it yourself)
EDIT: You just changed your question. So I can't tell if your original text was your actual source code or not. Good rule of thumb, paste your actual code rather than paraphrase it (then de-identify or reduce it if needed).
ORIGINAL ANSWER:
implementMe(); // compiler error: "implementMe() was not declared in this scope"
That is because doSomething() isn't declared properly in AbstractClass. You "declared" it in the base class with:
doSomething();
The compiler doesn't recognize AbstractClass::doSomething() out of line definition so nothing inside the implementation is resolved to the class scope.
Change that to:
void doSomething();
just like in your derived class.
and
AbstractClass::doSomething()
{
implementMe();
}
to
void AbstractClass::doSomething()
{
implementMe();
}
UPDATE:
void handleTimeout()
{
process();
counter++;
}
is a global function. That isn't the class implementation. It should be:
void AbstractClass::handleTimeout()
{
process();
counter++;
}
In abstractenvironment.cpp you define void handleTimeout(), which is non-member function and does not relate to AbstractEnvironment class. Thus, it doesn't look for AbstractEnvironment::process() and AbstractEnvironment::counter, but for ::process() and ::counter instead (which are not declared, hence the error).
Change it to void AbstractEnvironment::handleTimeout() and it should compile.

C++ error No matching member function for call to addtoucheventlistener

I'm new with cocos2d-x. I'm getting error in this line :
Button* btnRegister=static_cast<Button*>(Helper::seekWidgetByName(m_pLayout, "btnRegister"));
btnRegister->addTouchEventListener(CC_CALLBACK_0(LoginScene::GameLoginTest, this));//get error no matching member function for call to addtoucheventlistener
I don't know why cause i have already create one constructor below
Please help, what should i do to fix that
File LoginScene.h
#include "cocos2d.h"
#include "cocos-ext.h"
#include "CocosGUI.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace ui;
class LoginScene : public Scene
{
public:
LoginScene(bool pPortrait=false);
~LoginScene();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
//static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
//virtual bool init();
virtual void onEnter();
virtual void onExit();
// a selector callback
virtual void GameLogin();
void GameLoginTest(Ref* pSender,TouchEventType type);
//virtual void runThisTest()=0;
protected:
Layer* m_pUILayer;
Layout* m_pLayout;
int authenticate();
//int authenticate(const char* username, const char* password);
// implement the "static create()" method manually
//CREATE_FUNC(LoginScene);
};
File LoginScene.cpp
#include "LoginScene.h"
#include "cocostudio/CCSSceneReader.h"
#include "cocostudio/CCSGUIReader.h"
#include "cocostudio/CCActionManagerEx.h"
#include "LoadingScene.h"
#include "MainScene.h"
#include "curl/curl.h"
LoginScene::LoginScene(bool pPortrait):m_pUILayer(NULL),m_pLayout(NULL)
{
Scene::init();
}
LoginScene::~LoginScene()
{
}
void LoginScene::onEnter()
{
Scene::onEnter();
m_pUILayer=Layer::create();
m_pUILayer->scheduleUpdate();
this->addChild(m_pUILayer);
m_pLayout=dynamic_cast<Layout*>(cocostudio::GUIReader::getInstance()->widgetFromJsonFile("LoginScene.json"));
m_pUILayer->addChild(m_pLayout);
Button* btnLogin = static_cast<Button*>(Helper::seekWidgetByName(m_pLayout, "btnLogin"));
btnLogin->addTouchEventListener(CC_CALLBACK_0(LoginScene::GameLogin, this));//it's okay
Button* btnRegister=static_cast<Button*>(Helper::seekWidgetByName(m_pLayout, "btnRegister"));
btnRegister->addTouchEventListener(CC_CALLBACK_0(LoginScene::GameLoginTest, this));
//get error no matching member function for call to addtoucheventlistener
}
void LoginScene::GameLogin()
{
auto scene=LoadingScene::createScene();
Director::getInstance()->pushScene(scene);
}
void LoginScene::GameLoginTest(Ref* pSender,TouchEventType type)
{
if (type==TOUCH_EVENT_ENDED)
{
if (authenticate()==1)
{
auto scene=MainScene::createScene();
Director::getInstance()->pushScene(TransitionFade::create(2.3f, scene));
}
}
}
for LoginScene::GameLoginTest, maybe you can use CC_CALLBACK_2,like this:
btnRegister->addTouchEventListener(CC_CALLBACK_2(LoginScene::GameLoginTest, this));
The prototype of the functor you are sending is
(LoginScene* , Ref* , TouchEventType)
Since LoginTest is not a static method, it has an implicit parameter called "this".
You have two options :
make the function static
change the prototype to just TouchEventType (I believe that Ref* is meant to be "this").

i need help implementing my event manager and engine system (SDL && C++)

i need help with my attempt at writing a reusable game engine. it's not the best engine, but i definitely think it will be reusable once i am done. i am not asking for code or to be spoonfed, but i am asking for some advice :-).
my current layout:
i have an engine class, a game class, and an event manager class. the engine class extends the event manager class, and the game class extends the engine class. here is my current code for these 3 classes (ignore the Graphics class--it is just a reusable class i use to avoid rewriting fullscreen, initialize, and resize screen functions).
ENGINE.HPP
#ifndef _ENGINE_HPP
#define _ENGINE_HPP
#pragma once
#include <SDL/SDL.h>
#include "event_manager.hpp"
enum
{
ENGINE_SUCCESS = 0,
ENGINE_INITIALIZATION_ERROR
};
class Engine : public EventManager
{
public:
Engine();
virtual ~Engine();
int exec();
void handle_event(SDL_Event *);
void update_engine();
virtual bool init();
virtual void render();
virtual void update();
virtual void clean();
bool running_;
};
ENGINE.CPP
#include "./engine.hpp"
Engine::Engine()
{
running_ = false;
}
Engine::~Engine()
{
}
int Engine::exec()
{
if (!init())
{
clean();
return ENGINE_INITIALIZATION_ERROR;
}
SDL_Event event;
while (running_)
{
while (SDL_PollEvent(&event))
handle_event(&event);
update();
render();
}
clean();
return ENGINE_SUCCESS;
}
void update_engine()
{
}
void handle_event(SDL_Event *event)
{
EventManager::handle_event(event);
}
bool init() {return true;}
void render() {}
void update() {}
void clean() {}
EVENT_MANAGER.HPP
#ifndef _EVENT_MANAGER_HPP
#define _EVENT_MANAGER_HPP
#pragma once
#include <SDL/SDL.h>
class EventManager
{
public:
EventManager();
virtual ~EventManager();
virtual void handle_event(SDL_Event *);
// events here
virtual void event_exit();
};
#endif
EVENT_MANAGER.CPP
#include "./event_manager.hpp"
EventManager::EventManager()
{
}
EventManager::~EventManager()
{
}
void EventManager::handle_event(SDL_Event *event)
{
switch (event->type)
{
case SDL_QUIT:
event_exit();
break;
}
}
void on_exit() {}
GAME.HPP
#ifndef _GAME_HPP
#define _GAME_HPP
#include "./engine.hpp"
#include "./entity.hpp"
#include "./graphics.hpp"
class Game : public Engine
{
public:
Game();
bool init();
void render();
void update();
void clean();
private:
Graphics g;
};
#endif
GAME.CPP
#include "./game.hpp"
int main(int argc, char **argv)
{
Engine engine;
return engine.exec();
}
Game::Game() {}
bool Game::init()
{
if (!g.init(800, 600, 32, g.screen_flags()))
{
return false;
}
SDL_WM_SetCaption("Title", NULL);
return true;
}
void Game::update()
{
Engine::update_engine();
}
void Game::clean()
{
SDL_FreeSurface(g.screen_);
SDL_Quit();
}
void Game::render()
{
SDL_Flip(g.screen_);
}
i am getting this error:
engine.cpp: In function ‘void handle_event(SDL_Event*)’:
engine.cpp:40: error: cannot call member function ‘virtual void
EventManager::handle_event(SDL_Event*)’ without object
why is this happening? shouldn't i be able to do EventManager:: if the I did
class Engine : public EventManager
???
that is the only error i get, i am sure it is something simple. now i need a little bit of advice.
instead of handling events like
void Engine::event_exit()
in the engine, i'd rather do it in the game class.
class Game : public Engine
void Game::event_exit()
if that doesn't make sense, notice how i made Engine extend EventManager, and my Game class extends Engine
class Engine : public EventManager
class Game : public Engine
would it work if i called the snippet above these ^ two snippets? i can't test it because i get that error.
Happens to the best of us, but I think it's just a matter of forgetting to specify namespaces. When you implement the functions in engine.cpp, you forgot to prepend Engine::, so the code should be:
void Engine::update_engine()
{
}
void Engine::handle_event(SDL_Event *event)
{
EventManager::handle_event(event);
}
It's a classic case of C++ error messages not really telling you the root of the error.
A short explanation, just in case:
The compiler tried to compile the function void handle_event(SDL_Event *event), and saw a call to a method EventManager::handle_event(event);. Since the compiler thought the function was not part of the Engine class, it would expect you to call the method of a particular instance of the EventManager class, i.e.
someEventManager->handle_event(event);
As soon as you specify that the implementation you wrote is that of a method, belonging to the class Engine, the compiler essentially deduces:
void Engine::handle_event(SDL_Event *event)
{
this->EventManager::handle_event(event);
}
And therefore is happy.

Static Linking Problem in Qt with SQLite

I'm having a static linking problem in my C++ app. I'm hoping you can help. Code for header and source below.
#ifndef PRACTICARDSDB_H
#define PRACTICARDSDB_H
#include "cardset.h"
#include "card.h"
#include "filter.h"
class PractiCardsDB
{
public:
PractiCardsDB();
static void resetAll();
static void resetDates();
static CardSet getCardSet();
static CardSet getCardSet(Filter filter);
static void addCard(Card card);
static void editCard(Card card);
static void deleteCard(Card card);
static bool createConnection();
};
#endif // PRACTICARDSDB_H
Above is the header file and below is the source file.
#include "practicardsdb.h"
#include <QtSql/QSqlDatabase>
#include <QMessageBox>
PractiCardsDB::PractiCardsDB() {}
static bool PractiCardsDB::createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("EnglishSpanish");
if (!db.open())
{
return false;
}
return true;
}
The error I receive is: cannot declare member function 'static bool PractiCardsDB::createConnection()' to have static linkage. Any help?
I'm using Qt 4.7 with C++ inside Qt Creator if it helps.
When you define a static member function separately from the declaration, you do not have to use the static modifier.
bool PractiCardsDB::createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("EnglishSpanish");
if (!db.open())
{
return false;
}
return true;
}
Also do you really mean to make every single function of your class static? Your class represents a database of sorts for Card objects, so I think you'd want to actually store member data with the class itself?
Even in that snippet above, you create a QSqlDatabase object, but db's existence is only the extent of the createConnection() function.
Remove static decleration from your cpp file, it should only be in the header file. Like:
bool PractiCardsDB::createConnection()
{
....
}