I am trying to make a simple program to drop files on a list using C++ with a (wxWidgets) wxListCtrl.
Tools I use: Code::Blocks 20.03, MinGW 17.1, wxWidgets 3.1.3, wxFormBuilder 3.9.0.
OS: Windows 10 Pro.
I tried the following: (in a function SetupGUI() which is called in the DnD_SimpleFrame class constructor.)
m_listCtrl1 = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_NO_HEADER|wxLC_REPORT );
m_listCtrl1->DragAcceptFiles(true);
m_listCtrl1->Connect(wxEVT_DROP_FILES, wxDropFilesEventHandler(DnD_SimpleFrame::OnDropFiles), NULL, this);
The function to be called on dropping the files (from explorer) is:
bool DnD_SimpleFrame::OnDropFiles(wxArrayString &filenames)
{
size_t nFiles = filenames.GetCount();
wxString str;
str.Printf( "%d files dropped", (int)nFiles);
m_listCtrl1->DeleteAllItems();
if (m_listCtrl1 != NULL)
{
m_listCtrl1->InsertItem(0, str);
for ( size_t n = 1; n < (nFiles+1); n++ )
m_listCtrl1->InsertItem(n, filenames[n]);
}
return true;
}
The build messages on the m_listCtrl1->Connect(...); line are:
||=== Build: Debug in DnD_Simple (compiler: GNU GCC Compiler) ===|
F:\Data\__C++\wxApps\DnD_Simple\DnD_SimpleMain.cpp||In member function 'void DnD_SimpleFrame::SetupGUI()':|
F:\SDKs\wx313\include\wx\event.h|149|error: invalid static_cast from type 'bool (DnD_SimpleFrame::*)(wxArrayString&)' to type 'wxDropFilesEventFunction' {aka 'void (wxEvtHandler::*)(wxDropFilesEvent&)'}|
F:\SDKs\wx313\include\wx\event.h|4196|note: in expansion of macro 'wxEVENT_HANDLER_CAST'|
F:\Data\__C++\wxApps\DnD_Simple\DnD_SimpleMain.cpp|92|note: in expansion of macro 'wxDropFilesEventHandler'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
What am I doing wrong here (or am forgetting)?
If you carefully look at the error message:
error: invalid static_cast from type 'bool (DnD_SimpleFrame::*)(wxArrayString&)' to type 'wxDropFilesEventFunction' {aka 'void (wxEvtHandler::*)(wxDropFilesEvent&)'}
you can see that it says that it can't convert something from one type to the other one.
The types may be difficult to parse if you're new to C++, but you should be able to see that these are function pointers (in fact, pointers to a class member). And looking at your code, you should also be able to see that the first is them is the type of your DnD_SimpleFrame::OnDropFiles function and hence the problem is that it can't be converted to the expected function type.
Finally, the reason for this is just that your function doesn't have the right parameter type: you're supposed to give wxWidgets something taking wxDropFilesEvent&, but your function takes wxArrayString& instead. You will have to change it to take wxDropFilesEvent& event and then use GetFiles() method of the event object to get the actual files.
On a completely different topic, you should use Bind() and not Connect() in the new code. If you're following a tutorial which uses the latter, it's a good sign that it's very outdated. Your code still wouldn't have compiled with Bind(), but the error messages would have been slightly simpler.
Related
i have Microsoft Visual Studio (MSVS) 2012 Pro and i have set warning level to a slightly elevated level of 4. when doing this i am getting warnings for some of the included header files from the boost library. the message is this:
C:\Users\****\boost/optional/optional.hpp(595): warning C4244: 'initializing' : conversion from 'T_DOUBLE' to 'float', possible loss of data
C:\Users\****\boost/optional/optional.hpp(430) : see reference to function template instantiation 'void boost::optional_detail::optional_base<T>::construct<double>(Expr &&,const void *)' being compiled
with
[
T=T_FLOAT,
Expr=T_DOUBLE
]
C:\Users\****\boost/optional/optional.hpp(430) : see reference to function template instantiation 'void boost::optional_detail::optional_base<T>::construct<double>(Expr &&,const void *)' being compiled
with
[
T=T_FLOAT,
Expr=T_DOUBLE
]
the code in the file leading to this warning is this (line 610 on most recent beta of boost 1.64.0.B2 still resembles it exactly - but i am not on the beta now):
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
// Constructs using any expression implicitly convertible to the single argument
// of a one-argument T constructor.
// Converting constructions of optional<T> from optional<U> uses this function with
// 'Expr' being of type 'U' and relying on a converting constructor of T from U.
template<class Expr>
void construct ( Expr&& expr, void const* )
{
new (m_storage.address()) value_type(boost::forward<Expr>(expr)) ;
m_initialized = true ;
}
what is the reason (=learn to understand) for this warning and how to eliminate it in the boost header for me any anyone else? alternatively thinking: does it make sense to "fix" it in such a global way, or is there a deeper meaning pointing rather to somewhere else (either boost or application codes) to improve or fix those other codes?
You are probably passing a double literal into method that expects float. Something like foo(1.0) instead of foo(1.0f)
In the question How to register member function to lua without lua bind in c++ one answer suggested the following code:
class C {
public:
void blah(lua_State* L);
};
C inst;
lua_pushcclosure(L, std::bind(&C::blah, &inst, std::placeholder::_1), 0);
lua_setglobal(L, "blah");
(Quoted as it stood, including the small error in std::placeholders)
However, I cuold not get that to work. The error message I got back states that the function returned by std::bind can't be converted to a lua_CFunction.
I have also tried changing the return type of blah to int, but I get the same error message. If it's helpful to anyone, the full error message is:
Error C2664 'void lua_pushcclosure(lua_State *,lua_CFunction,int)': cannot convert argument 2 from 'std::_Binder<std::_Unforced,int (__thiscall C::* )(lua_State *),C *,const std::_Ph<1> &>' to 'lua_CFunction'
I even tried to change &C::blah to &inst.blah, but that unsurprisingly didn't work either.
Has anyone gotten it to work? Or is it just not meant to work?
trying to compile the TinyXml++ tutorial with CodeBlocks (16.01) and with VS2013 I get the same error at following line:
ticpp::Element* pElem = doc.FirstChildElement()->NextSibling();
CodeBlocks error:
invalid conversion from 'ticpp::Node*' to 'ticpp::Element*'
[-fpermissive]
VS2013 error:
cannot convert from 'ticpp::Node *' to 'ticpp::Element *'
Any idea?
In case you still want to compile it, regardless of whether there is an error in the tutorial, you can use the auto keyword for variable declaration.
For example:
auto pElem = doc.FirstChildElement()->NextSibling();
This way, the compiler will deduce the variable type at compile time.
The return type of NextSibling() is Node*. If you want Element* as return type, you can use NextSiblingElement() instead.
A friend and I are making a simple multiplayer game to practice our coding. Unfortunately we have encountered a problem. The SFML threading system (Not using std::thread because my compiler doesn't support it yet) is giving me an error.
||=== Build: Debug in Clear Void (compiler: GNU GCC Compiler) ===|
C:\Users\Name\Documents\Coding\SFML work\Clear_Void\src\GameScreen.cpp||In constructor 'GameScreen::GameScreen()':|
C:\Users\Name\Documents\Coding\SFML work\Clear_Void\src\GameScreen.cpp|9|error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&GameScreen::startThread' [-fpermissive]|
C:\SFML-TDM471x32\include\SFML\System\Thread.inl||In instantiation of 'void sf::priv::ThreadFunctor::run() [with T = void (GameScreen::*)()]':|
C:\Users\Name\Documents\Coding\SFML work\Clear_Void\src\GameScreen.cpp|53|required from here|
C:\SFML-TDM471x32\include\SFML\System\Thread.inl|39|error: must use '.' or '->' to call pointer-to-member function in '((sf::priv::ThreadFunctor)this)->sf::priv::ThreadFunctor::m_functor (...)', e.g. '(... -> ((sf::priv::ThreadFunctor*)this)->sf::priv::ThreadFunctor::m_functor) (...)'|
||=== Build failed: 2 error(s), 2 warning(s) (0 minute(s), 4 second(s)) ===|
Here is my code:
GameScreen.h
class GameScreen : public Screen
{
public:
GameScreen();
void handleInput(sf::RenderWindow&) override;
void update(sf::RenderWindow&, sf::View&) override;
void render(sf::RenderWindow&) override;
void startThread();
private:
sf::Vector2f moveVal;
Network network;
Events eventManager;
Map m_map;
sf::Thread networkThread;
};
GameScreen.cpp (Relevant Section)
GameScreen::GameScreen()
: networkThread(&startThread)
{
network.Connect();
}
I checked the SFML threading tutorials and according to them, that should work. Please help.
startThread should be static. In general, thread functions can't call non-static member functions.
Now I'm geting an error:
1>c:\development\document_manager\document_manager\storage_manager.h(7) : error C2079: 'storage_manager::db' uses undefined struct 'sqlite3'
with
#pragma once
#include "sqlite3.h"
class storage_manager
{
sqlite3 db;
sqlite3** db_pp;
public:
void open()
{
sqlite3_open("data.db", db_pp);
}
};
Old Question:
Hi everyone. I downloaded sqlite-amalgamation-3_6_13.zip from http://www.sqlite.org/download.html, but I'm not able to compile it in my project. I receive many errors like:
c:\pathtoproject\sqlite3.c(11337) : error C2440: '=' : cannot convert from 'void *' to 'char *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
c:\pathtoproject\sqlite3.c(12023) : error C2440: '=' : cannot convert from 'void *' to 'sqlite3_int64 *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
What do I need to do to compile my project properly? Thanks!
Edit:
I don't want to compile the whole program as C, I just want to compile three files as c, is this possible?
EDIT: FIXED! I created an new project.
It looks like you might be trying to compile a C program using a C++ compiler. While there is a lot of C code which is also valid C++, they are different languages.
Your compiler may have some switch or setting to compile C code. Check your compiler documentation.
You need to compile the file as C code rather than C++.
Right click on either the project or just the .c file, and in properties, make sure it is set to compile as C, rather than C++. (You may want to set this setting just for the file, not the entire project)
Doesn't the compiler tell you what to do? You need an explicit cast:
void *pv = /* some value */;
char *pc = (char*) pv;
This is of course not a problem in C, but an issue of C++.