Calling parent's method - c++

I'm kinda newbie to all this c++ stuff, so this probably is a beginner's problem:
ListScreen.h
#ifndef _LISTSCREEN_H_
#define _LISTSCREEN_H_
#include "MAUI/Screen.h"
namespace CoolPlaces {
namespace Views {
using namespace MAUI;
class ListScreen : public Screen {
public:
ListScreen();
~ListScreen();
void keyPressEvent(int keyCode, int nativeCode) {}
void keyReleaseEvent(int keyCode, int nativeCode) {}
void pointerPressEvent(MAPoint2d point) {}
void pointerReleaseEvent(MAPoint2d point) {}
void pointerMoveEvent(MAPoint2d point) {}
void show();
};
}
}
#endif //_LISTSCREEN_H_
ListScreen.cpp
#include "MAUI/Screen.h"
#include "ListScreen.h"
using namespace MAUI;
using namespace CoolPlaces::Views;
void ListScreen::show() {
Screen::show();
};
I'm getting this error: D:\MosyncProjects\Views\ListScreen.cpp:22: Error: Unresolved symbol '__ZN4MAUI6Screen4showEv' line 22 in this Screen::show(); call (for purpose of this topic I removed some code). So what exactly am I doing wrong here?

You're including the header file, which tells that the function Screen::show() exists, but probably not linking the library, which has the implementation.
See this page: http://www.mosync.com/docs/sdk/cpp/guides/libs/working-with-mosync-libraries/index.html
Specifically:
As well as referencing the header files in your application code, you also need to specify the actual libraries that you want to use in the project's Build Settings (Project > Properties > MoSync Project > Build Settings):
It looks like maui.lib should contain the screen code.

Related

VSCode C++ : triggering creation of class member function definition automatically

Consider Main.cpp:
//Main.cpp
#include "Main.h"
int main(){
class SIMPLE smpl;
smpl.setval(10);
smpl.setname();
smpl.printname();
}
and Main.h thus:
//Main.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
class SIMPLE {
public:
int retval() const { return val; }
void setval(int i) { val = i; }
void setname();
void printname();
private:
int val;
std::string name;
};
The member functions setname() and printname() are missing definitions. VSCode Intellisense does not seem to be able to warn the user about this. See figure below which does not have any squiggles:
Visual Studio IDE, on the other hand, is able to point out the missing definitions via squiggles and allows for creation of the definition in the implementation file directly from the header file. See gif image below:
Is there a way to have VSCode cpptools extension provide these features via some settings, perhaps?

Header file is inaccessible

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.

"plugin verification data mismatch" while loading plugin for qt5 project

I have raw (no QtDesigner) Qt5 project with two simple plugins, which one don't load with laconic error: "plugin verification data mismatch".
Header of first plugin (which loads and run well):
#ifndef __PIROGRONIAN__P2P2__GUI_PLUGIN__H__
#define __PIROGRONIAN__P2P2__GUI_PLUGIN__H__
#include "QtCore/QtCore"
#include "PluginInterface.h"
namespace P2P2 {
class GuiPlugin : public QObject, public PluginInterface {
Q_OBJECT
Q_PLUGIN_METADATA(IID "Pirogronian.P2P2.GuiPlugin")
Q_INTERFACES(P2P2::PluginInterface)
public:
bool init(CoreServer *);
bool receiveObject(Object*);
int channelType();
};
};
#endif
The second one, which don't load:
#ifndef __PIROGRONIAN__P2P2__CHAT_PLUGIN__H__
#define __PIROGRONIAN__P2P2__CHAT_PLUGIN__H__
#include <QtNetwork/QtNetwork>
#include "Chat.h"
#include "PluginInterface.h"
namespace P2P2 {
class ChatPlugin : public QObject, public PluginInterface {
Q_OBJECT
Q_PLUGIN_METADATA(IID "Pirogronian.P2P2.ChatPlugin")
Q_INTERFACES(P2P2::PluginInterface)
CoreServer *_server;
QHash<Channel *, Chat *> _chats;
public:
virtual bool init(CoreServer *);
virtual bool receiveObject(Object *);
virtual int channelType();
};
};
//Q_DECLARE_METATYPE(QPointer<P2P2::ChatPlugin>)
#endif
Here is PluginInterface header:
#ifndef __PIROGRONIAN__P2P2__PLUGIN_INTERFACE__H__
#define __PIROGRONIAN__P2P2__PLUGIN_INTERFACE__H__
#include "CoreServer.h"
namespace P2P2 {
class PluginInterface {
public:
virtual bool init(CoreServer *) = 0;
virtual bool receiveObject(Object *) = 0;
virtual int channelType() = 0;
};
};
Q_DECLARE_INTERFACE(P2P2::PluginInterface, "Pirogronian/P2P2/PluginInterface/1.0")
#endif
I'm not expert and writing plugins for qt5 is described very cursorily. But since I can't find any major difference between those plugins, problem becomes rather mysterious to me. Mayby a bug in Qt? I've rebuilt both several times to bye sure that both are up to date.
I'm trying put the whole code somewhere into net, but it'll take a while... Edit: done - packed as zip here: http://uploaduj.net/D74c2f/v0-1-pure-zip/
Not sure if it helps but it seems that your plugin has invalid metadata. Here's code that sets error message http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/plugin/qlibrary.cpp#n303
You could use debug version of Qt and set breakpoint in that function. This would give you exact line that fails while loading your plugin.
Maybe you have an error in your metadata?

Get list of methods in class using clang

In common IDEs (pick one) you often have an outline view showing you the list of methods for a specific class.
Suppose I have a C++ interface class in IFoo.h that looks like this:
#ifndef IFOO_H_
#define IFOO_H_
class IFoo {
public:
virtual ~IFoo() {}
virtual void bar() = 0;
};
#endif
How (programmatically) can I get such an IDE outliner list for my IFoo.h file above using maybe the clang libraries? For a first start, it would help if I can get a list of names of functions.
I specifically intend to use clang, so any help on how to analyze the my header file with clang would be really appreciated.
Meanwhile I will have a look at the clang tutorial here: https://github.com/loarabia/Clang-tutorial
Thanks in advance for your help.
I went through this tutorial http://clang.llvm.org/docs/LibASTMatchersTutorial.html and found some pretty helpful stuff there, this is what I came up with:
I had to rename my file from IFoo.h to IFoo.hpp to be detected as Cxx and not C code.
I had to call my program with -x c++ to have my IFoo.h file being recognized as C++ code rather than C code (clang interprets *.h files as C by default:
~/Development/llvm-build/bin/mytool ~/IFoo.h -- -x c++
This is my code to dump all virtual functions from the provided class:
// Declares clang::SyntaxOnlyAction.
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/ASTMatchers/ASTMatchers.h"
// Declares llvm::cl::extrahelp.
#include "llvm/Support/CommandLine.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include <cstdio>
using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::tooling;
using namespace llvm;
DeclarationMatcher methodMatcher = methodDecl(isVirtual()).bind("methods");
class MethodPrinter : public MatchFinder::MatchCallback {
public :
virtual void run(const MatchFinder::MatchResult &Result) {
if (const CXXMethodDecl *md = Result.Nodes.getNodeAs<clang::CXXMethodDecl>("methods")) {
md->dump();
}
}
};
// CommonOptionsParser declares HelpMessage with a description of the common
// command-line options related to the compilation database and input files.
// It's nice to have this help message in all tools.
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
// A help message for this specific tool can be added afterwards.
static cl::extrahelp MoreHelp("\nMore help text...");
int main(int argc, const char **argv) {
cl::OptionCategory cat("myname", "mydescription");
CommonOptionsParser optionsParser(argc, argv, cat, 0);
ClangTool tool(optionsParser.getCompilations(), optionsParser.getSourcePathList());
MethodPrinter printer;
MatchFinder finder;
finder.addMatcher(methodMatcher, &printer);
return tool.run(newFrontendActionFactory(&finder));
}
The output looks like this, when passed the IFoo.h file:
CXXDestructorDecl 0x1709c30 <~/IFoo.h:5:3, col:20> ~IFoo 'void (void)' virtual
`-CompoundStmt 0x1758128 <col:19, col:20>
CXXMethodDecl 0x1757e60 <~/IFoo.h:6:3, col:24> bar 'void (void)' virtual pure

Compiler error C2653: not a class or namespace name

So I have been having this extremely frustrating problem lately with Visual C++ 2012. Up until a few hours ago, I was writing code just fine and everything was working as intended, until I decided to optimize some things and deleted a few classes. I fixed all of the errors that were popping up because of that, e.g. false includes, etc. Unfortunately, after this the VS compiler went crazy. It started giving me errors such as:
Error 14 error C2653: 'Class' : is not a class or namespace name
or even
Error 5 error C2143: syntax error : missing ';' before '}'
Error 4 error C2059: syntax error : '>'
I've checked multiple times, and everything is in it's right place: all headers included, all symbols placed where they should be.
As far as I understand, the problem is not with my code but with the compiler itself... Visual Studio can be really annoying at times, I guess. Anyway, I would really be grateful if someone could help me out on this one.
(By the way, disabling precompiled headers did not work)
Relevant parts of code:
Error 14:
#include "PlayerEntity.h"
PlayerEntity::PlayerEntity(void) {} // This line causes the error
Error 5:
class GameScreen : public BaseScreen
{
public:
...
private:
...
}; // This line causes the error
Error 4:
private:
std::vector<BaseEntity*> _EntityList; // This line causes the error
Whole PlayerEntity.h file:
#ifndef PENTITY_H
#define PENTITY_H
#include "BaseEntity.h"
class PlayerEntity : public BaseEntity
{
public:
PlayerEntity(void);
PlayerEntity(float, float);
virtual ~PlayerEntity(void);
void render(sf::RenderWindow&);
void update();
private:
void init();
};
#endif
Whole GameScreen.h file:
#ifndef GSCREEN_H
#define GSCREEN_H
#include "BaseScreen.h"
#include "BaseEntity.h"
#include "PlayerEntity.h"
class GameScreen : public BaseScreen
{
public:
GameScreen(sf::Vector2u&);
virtual ~GameScreen(void);
void start();
void stop();
void render(sf::RenderWindow&);
void update(void);
void addEntity(BaseEntity*);
void destoryEntity(int id);
private:
std::vector<BaseEntity*> _EntityList;
sf::Vector2u _ScreenDimensions;
};
#endif
Whole BaseEntity.h file:
#ifndef BSENTITY_H
#define BSENTITY_H
#include "Input.h"
#include <SFML/Graphics.hpp>
class BaseEntity
{
public:
BaseEntity(void);
virtual ~BaseEntity(void);
sf::Vector2f position;
virtual void update(void);
virtual void render(sf::RenderWindow&);
void compare(BaseEntity*);
protected:
sf::Texture *_EntityTexture;
sf::Sprite _EntitySprite;
bool _isAlive;
int _id;
virtual void init();
};
#endif
Whole Input.h file:
#ifndef INPUT_H
#define INPUT_H
#include "ScreenSystem.h"
#include <SFML/Window.hpp>
class Input
{
public:
Input(void);
Input(sf::RenderWindow*);
virtual ~Input(void);
static bool keyPressed(int);
static bool keyReleased(int);
static bool mouseHeld(int);
static bool mouseReleased(int);
private:
static sf::RenderWindow *_Window;
};
#endif
Whole ScreenSystem.h file:
#ifndef GHANDLER_H
#define GHANDLER_H
#include "BaseScreen.h"
#include "MenuScreen.h"
#include "GameScreen.h"
#include <SFML/Window.hpp>
class ScreenSystem
{
public:
ScreenSystem(void);
ScreenSystem(sf::RenderWindow*);
virtual ~ScreenSystem(void);
BaseScreen *getCurrentScreen(void);
void setScreen(int);
private:
int _currentScreenID;
std::vector<BaseScreen*> _Screens;
sf::RenderWindow *_Window;
};
#endif
You have a circular dependency in your headers. BaseEntity.h includes Input.h, which includes ScreenSystem.h, which includes GameScreen.h, which in turn re-includes BaseEntity.h. This leads to class names appearing before they are declared, causing compilation failure.
To avoid this, do not include headers unnecessarily. For example, do not include Input.h from BaseEntity.h, since it's not needed at all; and do not include BaseScreen.h from ScreenSystem.h since only a declaration class BaseScreen; is needed, not the complete class definition.
Also, check that you do not have duplicate header guards. Some of them do not match the header name (e.g. GHANDLER_H for ScreenSystem.h), which makes me think that they may have been accidentally copied from other headers. Finally, don't use reserved names like _EntitySprite for your own symbols; for simplicity, avoid leading or double underscores.
Did you copy the error messages into your question or did you retype them? Because error 14 has 'Class' with a capital C which is almost certainly not right.
Also, you should use as few include directives in your header files as possible. For example, GameScreen doesn't use PlayerEntity, so you can remove that include and BaseEntity is only used via pointer so you can replace
#include "BaseEntity.h"
with a forward declaration
class BaseEntity;