I have this class:
#pragma once
#ifndef _DEFINES_H_
#include "Defines.h"
#endif
#ifndef _GAMETIME_H_
#include "GameTime.h"
#endif
#ifndef _UTILITIES_H_
#include "Utilities.h"
#endif
#ifndef _GAME_H_
using namespace System;
namespace BSGameFramework
{
public ref class Game
{
public:
Game();
virtual ~Game();
void Run(HINSTANCE instance);
string Title;
int WindowWidth;
int WindowHeight;
protected:
virtual void Initialize();
virtual void LoadContent();
virtual void UnloadContent();
virtual void Update(GameTime^ gameTime);
virtual void Draw(GameTime^ gameTime);
private:
HINSTANCE windowHandler;
HWND window;
DateTime lastTime;
TimeSpan totalGameTime;
D3D_DRIVER_TYPE driverType_;
D3D_FEATURE_LEVEL featureLevel_;
ID3D11Device* d3dDevice_;
ID3D11DeviceContext* d3dContext_;
IDXGISwapChain* swapChain_;
ID3D11RenderTargetView* backBufferTarget_;
void Shutdown();
};
}
#define _GAME_H_
#endif
and this is its child:
#pragma once
using namespace BSGameFramework;
public ref class MyGame : Game
{
public:
MyGame()
{
}
};
Then when on my Main I call my Run function:
#include <Windows.h>
#include "MyGame.h"
using namespace BSGameFramework;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MyGame ^game = gcnew MyGame();
game->Run(hInstance); // Here the error
}
I get this error:
Error 1 error C3767: 'BSGameFramework::Game::Run': candidate function(s) not accessible
C:\Users\Nicola\Desktop\directx prove\BSGameFramework\FrameworkTestCpp\Main.cpp 10 1 FrameworkTestCpp
I've tried to remove the HINSTANCE from Run parameters and all is working fine, but I need it so somebody can explain me why I'm getting this error and how can I solve? Thanks in advance!
I have solved in this way:
inline void Game::Run(IntPtr instance)
{
windowHandler = (HINSTANCE)instance.ToPointer();
// other code
}
now I'm passing an IntPtr that is not a native type, so on main function I have this:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MyGame ^game = gcnew MyGame();
IntPtr instance(hInstance);
game->Run(instance);
}
Related
Having checked posts with similar errors. None of the solutions proposed helped overcoming this issue.
I have checked all my classes for ';' at the end of the definition.....all of them are properly defined.
I have checked the include headers file for header guards. All of them have guards
This is output of building a QT project (desktop GUI app) .
What are the typical causes of these errors aside from what's mentioned above ?
below is the output of the error :
include\ConfigServer.h(85): error C2236: unexpected token 'struct'. Did you forget a ';'?
include\ConfigServer.h(85): error C2332: 'struct': missing tag name
This 'ConfigServer.h' file include 'BlockParam.h' , 'CommsInfo.h' and 'GeoInfo.h' which I have compiled with previously on a separate console project to test their use. They work on a console program.
Any insight ?
#ifndef CONFIGSERVER_H
#define CONFIGSERVER_H
#include <iostream>
#include <iterator>
#include <QObject>
#include <QMap>
#include <QString>
#include <QVector>
#include <QFile>
#include <QXmlStreamReader>
#include <QDebug>
#include "BlockParam.h"
#include "CommsInfo.h"
#include "GeoInfo.h"
#define _delete(x) { if(x) delete x; x = nullptr;}
#define DEBUG 1
#define SHOW(X,B) { if(DEBUG) { std::cout << X << B <<std::endl ; } }
#define DISPLAY(X) { if(DEBUG) { std::cout << X <<std::endl ; } }
enum ENUM_PLATFORM {
NOTSET = 0,
SILSIM = 1, // Desktop Platform
PILSIM = 2, // PIL Platform
HILSIM = 3 // HIL Platform
};
enum ENUM_CONFIG
{
GEOINFO = 1,
COMMS = 2,
MDLPARAM = 3
};
typedef QMap<QString,ConfigInfo*> CfgMap;
class ConfigServer
{
public:
ConfigServer();
~ConfigServer();
bool LoadXmlFile(const QString xmlParmFileName);
bool Validate(Xpci* xpc);
bool IsValidated();
bool errorsOccurred();
QVector<QString> getErrorStrings();
template<class T>
ConfigInfo *GetConfigInfo(QString _inface,QString _pty,bool ebl);
template<class T>
void DisplayContent(T* Cfg) const;
CfgMap *getMap() const;
QVector<CfgMap> *getConfigList() const;
ENUM_PLATFORM PLATFORM;
ENUM_CONFIG CONFIGURATION;
QVector<QString> ErrStringsVector;
private:
void readModelXmlToMap(QXmlStreamReader* reader,CfgMap* ConfigMap_);
template<class T>
bool readCurrentInterfaceElement(QXmlStreamReader* reader,CfgMap* ConfigMap_);
template<class T>
bool readCurrentPropertyElement(QXmlStreamReader* reader,QString interface,CfgMap* ConfigMap_);
bool readCurrentDimensionElement(QXmlStreamReader* reader,unsigned &rowDim,unsigned &colDim);
BlockParam nullBlockParam;
CommsInfo nullCommsInfo;
GeoInfo nullGeoInfo;
CfgMap* ConfigMap = nullptr;
QVector<CfgMap> *ConfigList = nullptr;
unsigned requisite;
bool validated = false;
bool errorFlag = false;
};
template<> bool ConfigServer::readCurrentInterfaceElement<BlockParam>(QXmlStreamReader* reader,CfgMap* ConfigMap_) ;
template<> bool ConfigServer::readCurrentInterfaceElement<CommsInfo>(QXmlStreamReader* reader,CfgMap* ConfigMap_) ;
template<> bool ConfigServer::readCurrentInterfaceElement<GeoInfo>(QXmlStreamReader* reader,CfgMap* ConfigMap_) ;
template<> bool ConfigServer::readCurrentPropertyElement<BlockParam>(QXmlStreamReader *reader,QString interface,CfgMap* ConfigMap_);
template<> bool ConfigServer::readCurrentPropertyElement<CommsInfo>(QXmlStreamReader *reader,QString interface,CfgMap* ConfigMap_);
template<> bool ConfigServer::readCurrentPropertyElement<GeoInfo>(QXmlStreamReader *reader,QString interface,CfgMap* ConfigMap_);
template<> ConfigInfo *ConfigServer::GetConfigInfo<BlockParam>(QString _inface,QString _pty,bool ebl);
template<> ConfigInfo *ConfigServer::GetConfigInfo<CommsInfo>(QString _inface,QString _pty,bool ebl);
template<> ConfigInfo *ConfigServer::GetConfigInfo<GeoInfo>(QString _inface,QString _pty,bool ebl);
template<> void ConfigServer::DisplayContent<BlockParam>(BlockParam* Cfg) const;
template<> void ConfigServer::DisplayContent<CommsInfo>(CommsInfo* Cfg) const;
template<> void ConfigServer::DisplayContent<GeoInfo>(GeoInfo* Cfg) const;
#endif // CONFIGSERVER_H
In Windows headers (which you indirectly include via Qt headers), there are macro definitions that boil down to #define interface struct. Your function declarations then get mangled to
template<> bool ConfigServer::readCurrentPropertyElement<BlockParam>(
..., QString struct, ...);
The QString struct part is obviously nonsense, and the compiler complains.
The easiest workaround would be to avoid using interface as an identifier.
Started using QtRO and generated files inherently complain about vtable:
#ifndef REP_REMOTEEXAMPLE_H
#define REP_REMOTEEXAMPLE_H
// This is an autogenerated file.
// Do not edit this file, any changes made will be lost the next time it is generated.
#include <QtCore/qobject.h>
#include <QtCore/qdatastream.h>
#include <QtCore/qvariant.h>
#include <QtCore/qmetatype.h>
#include <QtRemoteObjects/qremoteobjectnode.h>
#include <QtRemoteObjects/qremoteobjectpendingcall.h>
#include <QtRemoteObjects/qremoteobjectreplica.h>
class remoteExampleReplica : public QRemoteObjectReplica
{
Q_OBJECT
Q_CLASSINFO(QCLASSINFO_REMOTEOBJECT_TYPE, "remoteExample")
Q_CLASSINFO(QCLASSINFO_REMOTEOBJECT_SIGNATURE, "5e40a606abdd95f709878d419edaa735fce25d0d")
public:
remoteExampleReplica() : QRemoteObjectReplica() { initialize(); }
static void registerMetatypes()
{
static bool initialized = false;
if (initialized)
return;
initialized = true;
}
private:
remoteExampleReplica(QRemoteObjectNode *node, const QString &name = QString())
: QRemoteObjectReplica(ConstructWithNode)
{
initializeNode(node, name);
}
void initialize() override
{
remoteExampleReplica::registerMetatypes();
QVariantList properties;
properties.reserve(0);
setProperties(properties);
}
public:
virtual ~remoteExampleReplica() {}
Q_SIGNALS:
void Close(QString a);
void Open(QString b);
public Q_SLOTS:
void onClosed()
{
static int __repc_index = remoteExampleReplica::staticMetaObject.indexOfSlot("onClosed()");
QVariantList __repc_args;
send(QMetaObject::InvokeMetaMethod, __repc_index, __repc_args);
}
void onOpened()
{
static int __repc_index = remoteExampleReplica::staticMetaObject.indexOfSlot("onOpened()");
QVariantList __repc_args;
send(QMetaObject::InvokeMetaMethod, __repc_index, __repc_args);
}
private:
friend class QT_PREPEND_NAMESPACE(QRemoteObjectNode);
};
#if (QT_VERSION < QT_VERSION_CHECK(5, 5, 0))
#endif
QT_BEGIN_NAMESPACE
QT_END_NAMESPACE
#endif // REP_REMOTEEXAMPLE_H
The issue points to the private constructor. I only included the replica header in my code without really using the remote object as a test run. I've read a lot about vtables how they work and when the linker complains about the vtable but in this case, with the private constructor, I'm not sure what the issue is. Am I missing an implementation of the remote object? Did I not generate the files correctly?
.rep:
class remoteExample
{
SIGNAL(Close(QString a));
SIGNAL(Open(QString b));
SLOT(onClosed());
SLOT(onOpened());
};
I have a qt thread in my application which emits a Mat type image so that other threads can use it. the image is coming from camera by using VideoCapture Object of opencv library. now what i intend to do is getting this image from a rostopic, not from camera directly. in order to do that i must create a ros node in my thread and here i am stuck. is there anyone who has the experience to integrate ros node and qt?
here is my thread:
#include "../include/Ground_Station/camera.h"
#include <iostream>
using namespace std;
Camera::Camera()
{
}
void Camera::run()
{
VideoCapture cap;
cap.open(0);
while(1){
Mat image;
cap >> image;
cvtColor(image,image,CV_BGR2RGB);
emit ImgSignal(&image);
QThread::msleep(30);
}
}
and Camera.h:
#ifndef CAMERA_H
#define CAMERA_H
#include <QObject>
#include <QThread>
#include <QtCore>
#include <QtGui>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <ros/ros.h>
#include <opencv2/opencv.hpp>
using namespace cv;
class Camera: public QThread
{
Q_OBJECT
public:
Camera();
void run();
bool Stop = false;
signals:
void ImgSignal(Mat*);
private:
public slots:
};
#endif // THREAD_H
Basically, your executable file containing the main() function must be your ros node and your QT application at the same time. In your "main.cpp"
you call ros::init(...) and subscribe to the topic you want to listen to. Your subscriber callback function may convert the ros image to Mat and emmit an ImgSignal every time it is called. To do all that, I'd create a RosImageProvider class, something along the lines ..
class RosImageProvider: public QObject
{
Q_OBJECT
public:
void ImageSubscriberCallback(const sensor_msgs::Image::ConstPtr& img);
...
signals:
void ImgSignal(Mat*);
};
I encountred the same problem when dealing with TCP/IP connection from a ROS node within Qt and my solution was to inherit directly from the QThread object, thus when initializing the class, you initialize the ROS node and implement the work TODO in callbacks and the thread run() function.
So finally, my code looked something like this :
#ifndef INCLUDE_TCPHANDLER_HPP_
#define INCLUDE_TCPHANDLER_HPP_
#include <ros/ros.h>
#include <QThread>
#include <string>
class TCP_Handler : public QThread
{
Q_OBJECT
private:
int init_argc;
char** init_argv;
ros::Publisher cmd_control_publisher;
ros::Subscriber feedback_subscriber;
public:
TCP_Handler()
{}
virtual ~TCP_Handler(){}
/**
* #brief ROS methods
*
*/
inline bool init_ros()
{
int argc =0; char** argv = new char*();
ros::init(argc,argv,"tcp_handler");
ros::NodeHandle n;
cmd_control_publisher = n.advertise<robot_common::cmd_control>("cmd_control", 1000);
feedback_subscriber = n.subscribe<robot_common::feedback>("wifibot_pose", 4, &TCP_Handler::FeedbackCallback , this);
return true;
}
void FeedbackCallback(const robot_common::feedback::ConstPtr& pose)
{
//.....
}
/**
* #brief Threading methods
*
*/
virtual void init(int, std::string ip_addr = "127.0.0.1") = 0;
virtual void run() = 0;
virtual void stop() = 0;
/**
* #brief TCP methods (Server/Client have to implement these folks)
*
*/
virtual bool Send_data(char* data, int size) = 0;
virtual int Receive_data(char* in_data, int size) = 0;
virtual bool Open_connection() = 0;
virtual void Close_connection() = 0;
};
#endif /* INCLUDE_TCPHANDLER_HPP_ */
This code is just a template of a Qt-threaded-ROS node for TCP connction as I don't know your specific needs. feel free to build your own !
Cheers,
ALL,
I'm using Anjuta to do my development.
I created a project for my main application, and then made 2 more: 1 for the static library (libdbinterface.a) and 1 for the dynamic library (libsqlite_lib.so).
Both those libraries contains one exported class each: libdbinterface.a - class Database, libsqlite_lib.so - public SQLiteDatabase : public Database.
Now I'm trying to link libdbinterface.a to libsqlite_lib.so.
So in Anjuta I added following to the "Linker Option" for the target libsqlite_lib.so:
-L/home/igor/dbhandler/Debug/dbinterface -ldbinterface
However, trying to compile I received following error from linker:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/../../../../x86_64-pc-linux-gnu/bin/ld: /home/igor/dbhandler/Debug/dbinterface/libdbinterface.a(database.o): relocation R_X86_64_32S against `_ZTV8Database' can not be used when making a shared object; recompile with -fPIC
I tried to recompile libsqlite_lib.so with -fPIC explicitely added to "C++ Options" of that project but that didn't solve it - I still receive the same error.
Unfortunately trying to Google on how to link .a and .so is not helpful.
Can someone sched some light on how to fix this error?
TIA.
[EDIT]
libsqlite_lib.so Makefile - https://bpaste.net/show/1495231e58cc
libdbinterface.a Makefile - https://bpaste.net/show/3a71c119d0fc
libdbinterface.a contains 2 files databse.h:
#ifndef DBMANAGER_DATABASE
#define DBMANAGER_DATABASE
class Field
{
public:
Field(const std::string &columnName, const std::string &columnType, const std::string &columnDefaultValue = "", const bool columnIsNull = false, const bool columnPK = false)
{
column_name = columnName;
column_type = columnType;
column_defaultValue = columnDefaultValue;
column_isNull = columnIsNull;
column_pk = columnPK;
}
private:
std::string column_name, column_type, column_defaultValue;
bool column_isNull, column_pk;
};
struct FKField
{
FKField(const std::string &table_name, const std::string &original_field, const std::string &referenced_field)
{
tableName = table_name;
originalField = original_field;
referencedField = referenced_field;
}
std::string tableName, originalField, referencedField;
};
class Table
{
public:
Table(const std::string &tableName, const std::vector<Field> &tableFields, const std::map<int,std::vector<FKField> > &foreignKeys)
{
table_name = tableName;
table_fields = tableFields;
foreign_keys = foreignKeys;
}
const std::string &GetTableName() { return table_name; }
std::map<int,std::vector<FKField> > &GetForeignKeyVector() { return foreign_keys; }
private:
std::string table_name;
std::vector<Field> table_fields;
std::map<int,std::vector<FKField> > foreign_keys;
};
#ifdef WIN32
class __declspec(dllexport) Database
#else
class Database
#endif
{
private:
struct Impl;
Impl *pimpl;
public:
Database();
virtual ~Database();
Impl &GetTableVector();
static void *operator new(std::size_t size);
static void operator delete(void *ptr, std::size_t size);
virtual int Connect(const char *selectedDSN, std::vector<std::wstring> &errorMsg);
virtual int GetTableListFromDb(std::string &) { return 0; }
};
#endif
and database.cpp:
#ifdef WIN32
#include <windows.h>
#endif
#include <map>
#include <vector>
#include <string>
#include <sqlext.h>
#include "database.h"
struct Database::Impl
{
std::vector<Table> m_tables;
};
Database::Database() : pimpl( new Impl )
{
}
Database::~Database()
{
delete pimpl;
}
void *Database::operator new(std::size_t size)
{
return ::operator new( size );
}
void Database::operator delete(void *ptr, std::size_t size)
{
return ::operator delete( ptr );
}
Database::Impl &Database::GetTableVector()
{
return *pimpl;
}
int Database::Connect(const char *selectedDSN, std::vector<std::wstring> &errorMsg)
{
selectedDSN = selectedDSN;
errorMsg = errorMsg;
return 0;
}
libsqlite_lib.so has also 2 files: database_sqlite.h
#ifndef DBMANAGER_SQLITE
#define DBMANAGER_SQLITE
#ifdef WIN32
class __declspec(dllexport) SQLiteDatabase : public Database
#else
class SQLiteDatabase : public Database
#endif
{
public:
SQLiteDatabase();
~SQLiteDatabase();
virtual int Connect(const char *selectedDSN, std::vector<std::wstring> &errorMsg);
virtual int GetTableListFromDb(std::vector<std::wstring> &errorMsg);
protected:
void GetErrorMessage(int code, std::wstring &errorMsg);
private:
sqlite3 *m_db;
};
#endif
and database_sqlite.cpp with the actual implementation.
[/EDIT]
Well, apparently the solution is to rebuild static library with "-fPIC", not the dynamic one.
Thank you for reading.
I'm trying to use custom classes in QAbstractListModel, and the Q_DECLARE_METATYPE doesn't work at all!
To test where the problem is, I've simplified the code as the following:
#include <QMetaType>
#include <QVariant>
#include <QDebug>
typedef int x;
Q_DECLARE_METATYPE(x)
void main() {
QVariant v;
qDebug() << v.canConvert<x>();
}
and the output is still false!
btw, the code I want to implement is like:
namespace ns{
class a {
public:
a(); //default constructor
a(const a&); //copy constructor
~a();
}
}
Q_DECALRE_METATYPE(ns::a);
and when I try to reimplement QAbstractListModel::data like this:
QList<ns::s> list; //this is actually a member field of the custom model.
QVariant MyListModel::data(const QModelIndex& index, int role) const {
Q_UNUSED(role)
return list.at(index.row());
}
the compiler will report and error like:
cannot convert const ns::a to QVariant::Type
Your example is overly simplified as the docs quite clearly state that the class/struct being passed to Q_DECLARE_METATYPE must have a default constructor, a copy constructor and a public destructor: http://qt-project.org/doc/qt-5.0/qtcore/qmetatype.html#Q_DECLARE_METATYPE
That being said, here's a pretty simple example that shows Q_DECLARE_METATYPE working:
#include <QMetaType>
#include <QVariant>
#include <QDebug>
namespace MyNS {
class MyClass {
public:
MyClass() : value(0) { }
MyClass(int value) : value(value) { }
MyClass(const MyClass &other) { value = other.value; }
~MyClass() { }
int getValue() const { return value; }
private:
int value;
};
};
Q_DECLARE_METATYPE(MyNS::MyClass);
int main(int argc, char *argv[])
{
MyNS::MyClass m(15);
QVariant v = QVariant::fromValue(m);
qDebug() << v.canConvert<MyNS::MyClass>();
qDebug() << v.value<MyNS::MyClass>().getValue();
}