• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
CW vs Xcode C++ question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

CW vs Xcode C++ question


  • Subject: CW vs Xcode C++ question
  • From: Mark Alldritt <email@hidden>
  • Date: Sat, 29 Apr 2006 07:21:16 -0700
  • Thread-topic: 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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: CW vs Xcode C++ question
      • From: Howard Hinnant <email@hidden>
  • Prev by Date: Bug in PrefPane default project (window width)
  • Next by Date: Re: Single project or two separate projects?
  • Previous by thread: Bug in PrefPane default project (window width)
  • Next by thread: Re: CW vs Xcode C++ question
  • Index(es):
    • Date
    • Thread