boost::function



  • 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



  • Vielleicht dasselbe nochmal mit dem Visual C++ Compiler? 🙄



  • obs*g* naja ich glaub kaum das der vc compiler ne andere nummer hat;)
    ruf mal cl onder nmake auf...
    cu



  • @LordJaxom: kar, aber wie heißt der?

    Greetz, Swordfish



  • C:\Programme\Microsoft Visual Studio 8\VC\bin>nmake
    
    Microsoft (R) Program Maintenance Utility Version 8.00.50727.42
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    NMAKE : fatal error U1064: MAKEFILE not found and no target specified
    Stop.
    

    oder?



  • fu*k:

    Microsoft (R) Program Maintenance Utility, Version 7.00.9466
    

    Damit hat sich das Thema mit boost::function wohl erledigt. Kann mir jemand verraten, ob man in ein MSVS 2002 .net einen späteren Compiler einbinden kann?

    Greetz, Swordfish



  • hast du boost mit "-sTOOLS=vc-7_0" compiliert?

    cu



  • Ne, aber jetzt compilier ich 'grad mit "-sTOOLS=vc7".

    Greetz, Swordfish



  • und gehts besser?



  • Hallo,

    Mircosoftprodukte machen nur ärger :D.

    MFG winexec*



  • winexec* schrieb:

    Hallo,

    Mircosoftprodukte machen nur ärger :D.

    MFG winexec*

    Nur wenn man nicht weiß wie man damit umgeht. Und das gilt nicht nur für Microsoft Programme.

    Für mich sieht es einfach so aus als hätte da jemand unteranderem die Include Verzeichnisse falsch gesetzt (edit: Oder überhaupt nicht) und http://www.boost.org/more/getting_started.html nicht gelesen.

    BR
    Vinzenz



  • #include <iostream>
    #include <boost/lambda/lambda.hpp>
    #include <boost/function.hpp>
    #include <boost/bind.hpp>
    
    struct add
    {
    	float operator()(float x, float y) const
    	{
    		return x + y;
    	}
    };
    
    #define PRINT(expr) std::cout << "\t" #expr ": " << (expr) << std::endl;
    
    typedef boost::function<float(float,float)> function2d_t;
    typedef boost::function<float(float)> function1d_t;
    
    int main()
    {
    	using namespace boost;
    	namespace bl = boost::lambda; // use boost lambda to define unnamed functions
    	std::cout << "Adder example f(x,y) = x+y -> g(y) = 10+y" << std::endl;
    	function2d_t f = add(); // f(x,y) = x+y
    	function1d_t g = bind(f,10,_1); // g(y) = 10+y;
    
    	PRINT(f(10,20));
    	PRINT(g(20));
    
    	// classical division example using lambda functions
    	function2d_t div = bl::_1 / bl::_2;
    	function1d_t div_by_4 = bind(div,_1,4.0);
    
    	std::cout << "Division example div(x,y) : x/y" << std::endl;
    	PRINT(div(2,4));
    	PRINT(div_by_4(2));
    }
    

    wie funktioniert das denn? hmm...hat jemand ein besseres beispiel woran man erkennen kann wie boost::function mit boost::bind arbeiten?



  • vielleicht steh ich gerade wieder mal auf dem Schlauch, aber wo ist der Unterschied von

    ... 
    
        boost::function< int( int a ) > f; 
    
        if( a < 10 ) { 
    
            f = foo; 
    
        } else { 
    
            f = bar; 
        } 
    
        std::cout << f( a ) << std::endl;
    

    und

    if( a < 10)
        std::cout << foo(a) << std::endl;
    else
        std::cout << bar(a) << std::endl;
    

    Ich sehe da den Vorteil der Boost::function nicht...

    Gruß Paddy



  • Du stehst auf dem Schlauch. Das sind nur Beispiele, wie man es dynamisch machen kann. Was wäre zum Beispiel, wenn du kein std::endl sondern "asdf\n" anfügen wolltest? Nun stell dir das mal in einem großen switch vor, das gibt Spaß ;).
    Die eigentliche Verwendung von Funktionsobjekten sind aber mehr solche Sachen wie Callbacks.


Anmelden zum Antworten