Results 1 to 3 of 3

Thread: sequences of classes cause C++ compile error (but compiles ok as sequence of structs)

  1. #1
    lancepw is offline Registered User
    Name: Lance Welsh
    Organization: Booyah
    Project: iPhone
    Join Date
    Sep 2005
    Posts
    14

    sequences of classes cause C++ compile error (but compiles ok as sequence of structs)

    This simple ice test code that compiles ok:

    // Ice (slices fine):
    module M {
    struct S {
    string name ;
    } ;
    sequence<S> Ss ;
    interface I {
    Ss getSs( ) ;
    };
    };
    ===
    // C++ (compiles ok):
    class II : public M::I {
    public:
    II();
    void func() ;
    Ss _ss ;
    };
    void II::func( ) {
    // compiles ok:
    S s ;
    _ss.push_back( s ) ;
    }

    but when the the struct is changed to a class, it won't compile:

    // Ice (slices fine):
    module M {
    class C {
    string name ;
    } ;
    sequence<C> Cs ;
    interface I {
    Cs getCs( ) ;
    };
    };
    // C++ (won't compile):
    class II : public M::I {
    public:
    II();
    void func() ;
    Cs _cs ;
    };
    void II::func( ) {
    C c ;
    // C++ compiler error:
    _cs.push_back( c ) ;
    }

    The C++ compiler error above is:
    c++ -c -I. -I/usr/src/packages/BUILD/Ice-3.1.1/include -I../ice2 -ftemplate-depth-128 -Wall -D_REENTRANT -fPIC -g ContentI.cpp
    ContentI.cpp: In member function ‘void II::func()’:
    ContentI.cpp:15: error: no matching function for call to ‘std::vector<IceInternal::Handle<M::C>, std::allocator<IceInternal::Handle<M::C> > >:ush_back(M::C&)’
    /usr/include/c++/4.1.0/bits/stl_vector.h:602: note: candidates are: void std::vector<_Tp, _Alloc>:ush_back(const _Tp&) [with _Tp = IceInternal::Handle<M::C>, _Alloc = std::allocator<IceInternal::Handle<M::C> >]

    I'd like to use a class for this particular structure because I want to make a simple derivation (no methods or anything fancy). I don't understand why the class-method won't compile while the struct-method compiles and works just fine. It looks as though with a class it's creating a sequence of pointers to objects instead of a sequence of objects as I believe is specified (and as what happens with structs).

    I could change all my code to treat the data in the sequence/vector as pointers, but that would be a pain as well as inconsitent with the rest of my similar code using structs; and, I'm not confident I understand why it is not working and will just perpetuate into other problems like allocation.

    I could also work-around by replicating the members of the desired base class/struct into a new struct with the new desired data, or I could compose the desired base class/struct into a new struct with the new desired data. Both these work-arounds avoid the problem by avoiding the need for inheritance and hence classes, but then also avoid the benefit I'm expecting from an OO-capable architecture.

    Sorry if this is dumb question, and I'm probably doing something wrong - but I've read most of the manual, and I used to do something like this years ago in corba. Sorry if I'm missing something, but any advice is appreciated. -L.
    Lance Welsh
    Seascape
    www.seacoms.com
    Interested in using Ice on iPhone

  2. #2
    matthew's Avatar
    matthew is offline ZeroC Staff
    Name: Matthew Newhook
    Organization: ZeroC, Inc.
    Project: Internet Communications Engine
    Join Date
    Feb 2003
    Location
    NL, Canada
    Posts
    1,458
    You must use a smart pointer not a class instance -- please see the C++ mapping section of the Ice manual for details. The code below then would be something like:

    Code:
    CPtr c = new C();
    Cs seq;
    seq.push_back(c);

  3. #3
    lancepw is offline Registered User
    Name: Lance Welsh
    Organization: Booyah
    Project: iPhone
    Join Date
    Sep 2005
    Posts
    14

    Thanks

    Thank you - your guidance is very-much apprediated! -L.
    Lance Welsh
    Seascape
    www.seacoms.com
    Interested in using Ice on iPhone

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Python: repr() of Slice structs/classes
    By cebix in forum Comments
    Replies: 2
    Last Post: 04-15-2008, 08:24 AM
  2. Feature Request: partial classes / structs in .net
    By programmerdad in forum Comments
    Replies: 2
    Last Post: 08-09-2006, 10:24 PM
  3. Latency: structs vs. classes
    By n2503v in forum Help Center
    Replies: 7
    Last Post: 10-09-2005, 12:08 AM
  4. C#: Attributes with Ice generated classes and structs
    By DeepDiver in forum Help Center
    Replies: 2
    Last Post: 07-23-2005, 02:16 AM
  5. Replies: 1
    Last Post: 01-25-2005, 07:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •