ParseThisCode Temat założony przez niniejszego użytkownika |
Błąd kompilacji pod linuxem » 2017-06-26 23:38:17 Krótko i na temat. Windows, g++ 7.1.0 - program kompiluje sie bez problemów. Ubuntu, g++ 7.1.0 - błąd. note: candidate: void core::addCommand(std::string, core::fn) void addCommand(string command, fn callback); ^~~~~~~~~~ core.hpp:30:10: note: no known conversion for argument 2 from ‘<unresolved overloaded function type>’ to ‘core::fn {aka void (core::*)(std::basic_string<char>)}’ core.cpp:10:38: error: invalid use of non-static member function ‘void core::extConfig(std::string)’ addCommand("extConfig", extConfig); ^ In file included from core.cpp:1:0: core.hpp:24:10: note: declared here void extConfig(string fname); ^~~~~~~~~ core.cpp:11:40: error: invalid use of non-static member function ‘void core::console(std::string)’ addCommand("console.run()", console); ^ In file included from core.cpp:1:0: core.hpp:25:10: note: declared here void console(string arg);
|
|
Kinexity |
» 2017-06-27 14:50:57 Kod wrzuć to może ktoś pomoże. |
|
ParseThisCode Temat założony przez niniejszego użytkownika |
» 2017-06-27 16:39:54 core.hpp #pragma once
#ifndef CORE_SYSTEM #define CORE_SYSTEM
#include <iostream> #include <fstream> #include <sstream> #include <string> #include <vector> #include <tuple> #include <algorithm>
using namespace std;
class core { typedef void( core::* fn )( string ); vector < string > names; vector < tuple < string, fn > > callbacks; string cmd, arg; protected: void echo( string arg ); void exec( string arg ); void extConfig( string fname ); void console( string arg ); public: core(); void init(); void addCommand( string command, fn callback ); void exec( string command, string arg ); void readConfig( string fname = "config.ini" ); };
#endif
core.cpp #include "core.hpp"
core::core() { init(); }
void core::init() { addCommand( "echo", echo ); addCommand( "exec", exec ); addCommand( "extConfig", extConfig ); addCommand( "console.run()", console ); }
void core::echo( string arg ) { cout << "=> " << arg << " <=" << endl; }
void core::exec( string arg ) { istringstream iss( arg ); string cmd, args; iss >> cmd; getline( iss, args ); args.erase( 0, 1 ); exec( cmd, args ); }
void core::console( string arg = "" ) { cout << "--- Console Start ---" << endl; cout << "Type \"exit\" to close console" << endl; while( cmd != "exit" ) { cin >> cmd; getline( cin, arg ); arg.erase( 0, 1 ); if( cmd != "exit" ) exec( cmd, arg ); } cout << "--- Console End ---" << endl; }
void core::extConfig( string fname ) { this->readConfig( fname ); }
void core::addCommand( string command, fn callback ) { auto pos = find( names.begin(), names.end(), command ); if( pos == names.end() ) { names.push_back( command ); callbacks.push_back( { command, callback } ); } else cerr << "\"" << command << "\" is paired already" << endl; } void core::exec( string command, string arg ) { auto pos = find( names.begin(), names.end(), command ); if( pos != names.end() ) { ( this->* get < 1 >( callbacks[ pos - names.begin() ] ) )( arg ); } else if( command[ 0 ] == '#' ) { } else { cerr << "\"" << command << "\" is not function callback" << endl; } } void core::readConfig( string fname ) { ifstream io( fname ); if( !io ) { cerr << "\"" << fname << "\" can't be loaded" << endl; return; } while( io >> cmd ) { getline( io, arg ); arg.erase( 0, 1 ); this->exec( cmd, arg ); } io.close(); }
main.cpp #include "core.hpp"
int main() { core * c( new core ); c->readConfig(); }
|
|
mateczek |
» 2017-06-27 18:49:49 addCommand( "echo", & core::echo );
Zapoznaj się z std::functon ; std::bind; większe możliwości i wygoda ja kiedyś zrobiłem tak: 1. zadelarowełem kolejkę zadań. Tutaj kontener Qt ale można użyć STL QQueue < std::function < void() >> kolejkaZadan;
2. dodanie metody do kolejki - można dodawać metody z parametrami kolejkaZadan.enqueue( std::bind( & PlcQtLib::metoda, this, dana1, dana2 ) ); 3. wywołanie wszystkich zadań z kolejki while( !kolejkaZadan.empty() ) { kolejkaZadan.dequeue()(); }
|
|
carlosmay |
» 2017-06-27 18:57:42 Windows, g++ 7.1.0 - program kompiluje sie bez problemów. |
Czyżby? |
|
mateczek |
» 2017-06-27 19:06:45 Możliwe że się kompiluje. W przykładzie który podałem wyżej pierwszy mój kod był bez znaku "&" kolejkaZadan.enqueue( std::bind( PlcQtLib::metoda, this, dana1, dana2 ) ); kolejkaZadan.enqueue( std::bind( & PlcQtLib::metoda, this, dana1, dana2 ) );
|
|
ParseThisCode Temat założony przez niniejszego użytkownika |
» 2017-06-27 23:01:48 Problem rozwiązany. addCommand( "echo", & core::echo );
Jeśli zrobie tak: addCommand( "echo", & echo );
To pojawia się inny błąd, ale tylko na ubuntu ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say ‘&core::echo’ [-fpermissive] addCommand("echo", & echo); ^~~~~~~
Nieistotne, bo pierwsze rozwiązanie mnie satysfakcjonuje. Dzięki za pomoc. |
|
« 1 » |