boost::function



  • 'N Abend Leute!

    Folgendes Problem:

    #include <iostream>
    #include <boost/function.hpp>
    /* 003 */
    /* 004 */ int foo( int a )
    /* 005 */ {
    /* 006 */    return a;
    /* 007 */ }
    /* 008 */ 
    /* 009 */ int main( )
    /* 010 */ {
    /* 011 */     boost::function< int( int a ) > f = foo;
    /* 012 */     std::cout << f( 1 ) << std::endl;
    /* 013 */ }
    

    MSVC v7.1 schrieb:

    main.cpp
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(11) : error C2039: 'function' : Ist kein Element von 'boost'
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(11) : error C2065: 'function' : nichtdeklarierter Bezeichner
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(11) : error C2144: Syntaxfehler : 'int' sollte auf ')' folgen
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(11) : error C2059: Syntaxfehler : ')'
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(13) : error C2065: 'f' : nichtdeklarierter Bezeichner
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(13) : error C2593: 'Operator <<' ist mehrdeutig

    Hat jemand eine Idee?

    Greetz, Swordfish



  • #include <boost/function/function.hpp>
    

    😉



  • boost::function< int( int ) >



  • welchen vorteil hat das gegenüber wenn man die function selber aufruft?



  • Hallo

    Das zur Laufzeit festgelegt werden kann welche Funktion ausgeführt werden soll, und nicht schon zur Compilezeit.

    bis bald
    akari



  • 1310-Logik schrieb:

    #include <boost/function/function.hpp>
    

    😉

    #include <iostream>
    #include <boost/function/function.hpp>
    
    int foo( int a )
    {
    	return a;
    }
    
    int main( )
    {
    	boost::function< int( int a ) > f = foo;
    
    	std::cout << f( 1 ) << std::endl;
    }
    

    MSVC v7.1 schrieb:

    main.cpp
    d:\root\projects\_Visual Studio\test\main.cpp(2) : fatal error C1083: Include-Datei kann nicht geöffnet werden: 'boost/function/function.hpp': No such file or directory

    dir C:\programme\boost\include\boost-1_33_1\boost\function /B schrieb:

    detail
    function0.hpp
    function1.hpp
    function10.hpp
    function2.hpp
    function3.hpp
    function4.hpp
    function5.hpp
    function6.hpp
    function7.hpp
    function8.hpp
    function9.hpp
    function_base.hpp
    function_template.hpp

    😕

    boost::function schrieb:

    boost::function< int( int ) >

    http://www.boost.org/doc/html/function/tutorial.html#id2688219 schrieb:

    Preferred syntax

    • GNU C++ 2.95.x, 3.0.x, 3.1.x
    • Comeau C++ 4.2.45.2
    • SGI MIPSpro 7.3.0
    • Intel C++ 5.0, 6.0
    • Compaq's cxx 6.2
    • Microsoft Visual C++ 7.1

    [...]

    Basic Usage

    Preferred syntax
    boost::function<float (int x, int y)> f;

    😕

    ricky schrieb:

    welchen vorteil hat das gegenüber wenn man die function selber aufruft?

    http://www.boost.org/doc/html/function/misc.html#id2699572 schrieb:

    Boost.Function vs. Function Pointers

    Boost.Function has several advantages over function pointers, namely:

    • Boost.Function allows arbitrary compatible function objects to be targets (instead of requiring an exact function signature).
    • Boost.Function may be used with argument-binding and other function object construction libraries.
    • Boost.Function has predictible behavior when an empty function object is called.

    And, of course, function pointers have several advantages over Boost.Function:

    • Function pointers are smaller (the size of one pointer instead of three)
    • Function pointers are faster (Boost.Function may require two calls through function pointers)
    • Function pointers are backward-compatible with C libraries.
    • More readable error messages.

    💡

    Greetz, Swordfish



  • #include <boost/function/function1.hpp>
    
    boost::function< int(int a) >;
    

    Schau in die Docu, welches besser passt.
    function0 tut bei mir gute Arbeit..

    Edit:
    Sorry werd auch nicht recht schlau aus der Docu.
    Die Zahl (functionN) bezieht sich auf die Anzahl Paramter.



  • aber

    boost::function< int( int a ) > f = foo;
    
        std::cout << f( 1 ) << std::endl;
    

    aber hier wird ja auch schon zur compilezeit bestimmt was aufgerufen wird?



  • #include <iostream>
    #include <boost/function/function0.hpp>
    /* 003 */ 
    /* 004 */ int foo( int a )
    /* 005 */ {
    /* 006 */     return a;
    /* 007 */ }
    /* 008 */ 
    /* 009 */ int main( )
    /* 010 */ {
    /* 011 */     boost::function0< int(int a) > f = foo;
    /* 012 */ 
    /* 013 */     std::cout << f( 1 ) << std::endl;
    /* 014 */ }
    

    MSVC v7.1 schrieb:

    main.cpp
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(11) : error C2144: Syntaxfehler : 'int' sollte auf ')' folgen
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(14) : error C2143: Syntaxfehler : Es fehlt ';' vor '}'
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(43) : error C2143: Syntaxfehler : Es fehlt ';' vor '}'
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(43) : fatal error C1004: Unerwartetes Dateiende gefunden

    ➡

    #include <iostream>
    #include <boost/function/function0.hpp>
    /* 003 */ 
    /* 004 */ int foo( int a )
    /* 005 */ {
    /* 006 */     return a;
    /* 007 */ }
    /* 008 */ 
    /* 009 */ int main( )
    /* 010 */ {
    /* 011 */     boost::function0< int, int > f = foo;
    /* 012 */ 
    /* 013 */     std::cout << f( 1 ) << std::endl;
    /* 014 */ }
    

    MSVC v7.1 schrieb:

    main.cpp
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(11) : error C2440: 'Initialisierung' : 'int (__cdecl *)(int)' kann nicht in 'boost::function0' konvertiert werden
    with
    [
    R=int,
    Allocator=int
    ]
    Quelltyp konnte von keinem Konstruktor angenommen werden, oder die Überladungsauflösung des Konstruktors ist mehrdeutig
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(13) : error C2660: 'boost::function0::operator`()'' : Funktion akzeptiert keine 1 Parameter
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(13) : error C2064: Ausdruck ergibt keine Funktion

    😕

    Greetz, Swordfish



  • War zu langsam mit Editieren

    function1 sollte es bei Dir sein:

    #include <boost/function/function1.hpp>
    
    boost::function1< int(int a) >;
    


  • ricky schrieb:

    aber

    boost::function< int( int a ) > f = foo;
    
        std::cout << f( 1 ) << std::endl;
    

    aber hier wird ja auch schon zur compilezeit bestimmt was aufgerufen wird?

    #include <iostream>
    #include <boost/function.hpp>
    
    int foo( int a )
    {
        return a;
    }
    
    int bar( int a )
    {
        return 42;
    }
    
    int main( )
    {
        int a;
        std::cin >> a;
    
        boost::function< int( int a ) > f;
    
        if( a < 10 ) {
    
            f = foo;
    
        } else {
    
            f = bar;
        }
    
        std::cout << f( a ) << std::endl;
    }
    

    💡

    Greetz, Swordfish



  • vielleicht so @Swordfish:

    #include <iostream>
    #include <boost/function/function1.hpp>
    
    int foo( int a )
    {
         return a;
    }
    
    int main( )
    {
         boost::function1< int, int> f;
    	 f = foo;
         std::cout << f( 1 ) << std::endl;
    }
    


  • @swordfish
    Dein letztes Beispiel compiliert und läuft bei mir einwandfrei mit VC8.0 😕



  • 1310-Logik schrieb:

    War zu langsam mit Editieren

    function1 sollte es bei Dir sein:

    #include <boost/function/function1.hpp>
    
    boost::function1< int(int a) >;
    
    #include <iostream>
    #include <boost/function/function1.hpp>
    /* 003 */ 
    /* 004 */ int foo( int a )
    /* 005 */ {
    /* 006 */     return a;
    /* 007 */ }
    /* 008 */ 
    /* 009 */ int main( )
    /* 010 */ {
    /* 011 */     boost::function1< int, int > f = foo;
    /* 012 */ 
    /* 013 */     std::cout << f( 1 ) << std::endl;
    /* 014 */ }
    

    MSVC v7.1 schrieb:

    main.cpp
    d:\root\projects\_Visual Studio\test\main.cpp(11) : error C2440: 'Initialisierung' : 'int (__cdecl *)(int)' kann nicht in 'boost::function1' konvertiert werden
    with
    [
    R=int,
    T0=int,
    Allocator=int
    ]
    Quelltyp konnte von keinem Konstruktor angenommen werden, oder die Überladungsauflösung des Konstruktors ist mehrdeutig

    😕

    Greetz, Swordfish



  • #include <iostream>
    #include <boost/function.hpp>
    
    int foo( int a )
    {
        return a;
    }
    
    int bar( int a )
    {
        return 42;
    }
    
    int main( )
    {
        int a;
        std::cin >> a;
    
        boost::function< int( int a ) > f;
    
        if( a < 10 ) {
    
            f = foo;
    
        } else {
    
            f = bar;
        }
    
        std::cout << f( a ) << std::endl; 
    
    	std::cout << std::endl << "Hit enter to close window" << std::endl;
    	std::cin.clear();
    	std::cin.ignore( std::cin.rdbuf()->in_avail() );
        std::cin.get();
    	return 0;
    }
    

    Compiling...
    Main.cpp
    Compiling manifest to resources...
    Linking...
    LINK : C:\...\Visual Studio 2005\Projects\Boost test\Debug\Boost test.exe not found or not built by the last incremental link; performing full link
    Embedding manifest...
    Build log was saved at "file://c:\Dokumente und Einstellungen\Sebi\Eigene Dateien\Visual Studio 2005\Projects\Boost test\Boost test\Debug\BuildLog.htm"
    Boost test - 0 error(s), 0 warning(s)
    ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

    😕



  • #include <iostream>
    #include <boost/function.hpp>
    /* 003 */
    /* 004 */ int foo( int a )
    /* 005 */ {
    /* 006 */     return a;
    /* 007 */ }
    /* 008 */
    /* 009 */ int main( )
    /* 010 */ {
    /* 011 */     boost::function< int(int a) > f = foo;
    /* 012 */
    /* 013 */     std::cout << f( 1 ) << std::endl;
    /* 014 */ }
    

    ------ Rebuild All started: Project: Boost test, Configuration: Debug Win32 ------
    Deleting intermediate and output files for project 'Boost test', configuration 'Debug|Win32'
    Compiling...
    Main.cpp
    Compiling manifest to resources...
    Linking...
    LINK : C:\...\Visual Studio 2005\Projects\Boost test\Debug\Boost test.exe not found or not built by the last incremental link; performing full link
    Embedding manifest...
    Build log was saved at "file://c:\Dokumente und Einstellungen\Sebi\Eigene Dateien\Visual Studio 2005\Projects\Boost test\Boost test\Debug\BuildLog.htm"
    Boost test - 0 error(s), 0 warning(s)
    ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

    Ausgabe

    1

    😕 😕 😕



  • #include <iostream>
    #include <boost/function/function1.hpp>
    
    int foo( int a )
     {
         return a;
    }
    
     int main( )
     {
         boost::function1< int, int > f = foo;
    
         std::cout << f( 1 ) << std::endl;
    }
    
    ------ Build started: Project: Boost_Test, Configuration: Release Win32 ------
    Compiling...
    main.cpp
    Linking...
    Generating code
    Finished generating code
    Embedding manifest...
    Build log was saved at "file://c:\Boost_Test\Release\BuildLog.htm"
    Test2 - 0 error(s), 0 warning(s)
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
    


  • ricky schrieb:

    vielleicht so @Swordfish:

    #include <iostream>
    #include <boost/function/function1.hpp>
    
    int foo( int a )
    {
         return a;
    }
    
    int main( )
    {
         boost::function1< int, int> f;
    	 f = foo;
         std::cout << f( 1 ) << std::endl;
    }
    

    thx 👍

    @1310-Logik: Hä!? Selber Source:

    MSVC v7.1 (??) schrieb:

    main.cpp
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(19) : error C2039: 'function' : Ist kein Element von 'boost'
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(19) : error C2065: 'function' : nichtdeklarierter Bezeichner
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(19) : error C2144: Syntaxfehler : 'int' sollte auf ')' folgen
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(19) : error C2059: Syntaxfehler : ')'
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(23) : error C2065: 'f' : nichtdeklarierter Bezeichner
    d:\root\projects\_Visual Studio\Kakauautomat\main.cpp(30) : error C2593: 'Operator <<' ist mehrdeutig

    Moment 'mal wie bekomm ich die Compilerversion 'raus?

    Greetz, Swordfish



  • bei mir so: Visual Studio 2005 Command Prompt starten dann folgendes eintippen:

    C:\>vbc
    Microsoft (R) Visual Basic Compiler version 8.0.50727.42
    for Microsoft (R) .NET Framework version 2.0.50727.42
    Copyright (c) Microsoft Corporation.  All rights reserved.
    

    cu



  • ricky schrieb:

    [...]
    Microsoft (R) Visual Basic Compiler version 8.0.50727.42
    [...]
    

    und was mach ich mit der Versionsnummer eines Visual Basic Compilers!?

    Greetz, Swordfish


Anmelden zum Antworten