Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

CW vs Xcode C++ question



Hi Folks,

I'm porting a C++ project from CW to Xcode, and I cannot understand the
problem with the following code.  This code compiles and runs in CW, but
Xcode/GCC won't accept it.  Does anyone see what's wrong here:

#include <iostream>
#include <vector>

namespace aux {
    template <class Return, class Type>
    class mem_fun_t
    {
        typedef Return (Type::*Func)();
        
    public:
        explicit mem_fun_t(Func func) :
            func_(func) {}

        Return operator() (Type* obj) const { return (obj->*func_)(); }
        Return operator() (Type& obj) const { return (obj.*func_)(); }

    private:
        Func func_;
    };
    
    template <class Iterator, class Function>
    inline
    Function
    for_each(Iterator first, Iterator last, Function func)
    {
        return std::for_each(first, last, func);
    }

    template <class Container, class Function>
    inline
    Function
    for_each(Container& cont, Function func)
    {
        return aux::for_each(cont.begin(), cont.end(), func);
    }

    template <class Iterator, class Return, class Class>
    inline
    mem_fun_t<Return, Class>
    for_each(Iterator first, Iterator last, Return (Class::*func)())
    {
        return std::for_each(first, last, mem_fun_t<Return, Class>(func));
    }

    template <class Container, class Return, class Class>
    inline
    mem_fun_t<Return, Class>
    for_each(Container& cont, Return (Class::*func)())
    {
        return aux::for_each(cont.begin(), cont.end(), mem_fun_t<Return,
Class>(func));
    }
}

class Abc {
public:
    void    DoSomething() { std::cout << "DoSomething()" << std::endl; }
};

int main (int argc, char * const argv[]) {
    std::vector<Abc*> data;
    
    data.push_back(new Abc);
    aux::for_each(data, Abc::DoSomething);
    
    return 0;
}

/*
/Users/mall/Desktop/CPP/main.cpp:62: error: invalid use of non-static member
function 'void abc::DoSomething()'
/Users/mall/Desktop/CPP/main.cpp:62: error: invalid use of non-static member
function
/Users/mall/Desktop/CPP/main.cpp:62:   instantiated from here
/Users/mall/Desktop/CPP/main.cpp:34: error: invalid use of non-static member
function
/Users/mall/Desktop/CPP/main.cpp:34:   instantiated from 'Function
aux::for_each(Container&, Function) [with Container = std::vector<abc*,
std::allocator<abc*> >, Function = void (abc::)()]'
/Users/mall/Desktop/CPP/main.cpp:62:   instantiated from here
/Users/mall/Desktop/CPP/main.cpp:26: error: invalid use of non-static member
function
/Users/mall/Desktop/CPP/main.cpp:26:   instantiated from 'Function
aux::for_each(Iterator, Iterator, Function) [with Iterator =
__gnu_cxx::__normal_iterator<abc**, std::vector<abc*, std::allocator<abc*> >
>, Function = void (abc::)()]'
/Users/mall/Desktop/CPP/main.cpp:34:   instantiated from 'Function
aux::for_each(Container&, Function) [with Container = std::vector<abc*,
std::allocator<abc*> >, Function = void (abc::)()]'
/Users/mall/Desktop/CPP/main.cpp:62:   instantiated from here
/Developer/SDKs/MacOSX10.4u.sdk/usr/include/c++/4.0.0/bits/stl_algo.h:159:
error: invalid use of non-static member function
*/

Thanks
-Mark

------------------------------------------------------------------------
Mark Alldritt                      Late Night Software Ltd.
Phone: 250-380-1725                Script Debugger 4.0 - AppleScript IDE
WEB:  http://www.latenightsw.com/  FaceSpan 4.3 - AppleScript RAD
                                   Affrus 1.0 - Perl Debugging


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/xcode-users/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.