Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Błąd kompilacji pod linuxem

Ostatnio zmodyfikowano 2017-06-27 23:01
Autor Wiadomość
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);       
P-162855
Kinexity
» 2017-06-27 14:50:57
Kod wrzuć to może ktoś pomoże.
P-162864
ParseThisCode
Temat założony przez niniejszego użytkownika
» 2017-06-27 16:39:54
core.hpp
C/C++
#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_SYSTEM
core.cpp
C/C++
#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 ] == '#' ) {
        /* comments are ignored */
    }
    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
C/C++
#include "core.hpp"

int main() {
    core * c( new core );
    c->readConfig();
}
P-162877
mateczek
» 2017-06-27 18:49:49
C/C++
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
C/C++
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
C/C++
while( !kolejkaZadan.empty() ) {
    kolejkaZadan.dequeue()();
}
P-162878
carlosmay
» 2017-06-27 18:57:42
Windows, g++ 7.1.0 - program kompiluje sie bez problemów.
Czyżby?
P-162879
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 "&"

C/C++
kolejkaZadan.enqueue( std::bind( PlcQtLib::metoda, this, dana1, dana2 ) ); // na widnowsie poszło
kolejkaZadan.enqueue( std::bind( & PlcQtLib::metoda, this, dana1, dana2 ) ); // na linux kompilator się zapluł o operator
P-162880
ParseThisCode
Temat założony przez niniejszego użytkownika
» 2017-06-27 23:01:48
Problem rozwiązany.
C/C++
addCommand( "echo", & core::echo );

Jeśli zrobie tak:
C/C++
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.
P-162888
« 1 »
  Strona 1 z 1