Home Help Center

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

lancepwlancepw Member Lance WelshOrganization: BooyahProject: iPhone
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> > >::push_back(M::C&)’
/usr/include/c++/4.1.0/bits/stl_vector.h:602: note: candidates are: void std::vector<_Tp, _Alloc>::push_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.

Comments

  • matthewmatthew NL, CanadaMember Matthew NewhookOrganization: ZeroC, Inc.Project: Internet Communications Engine ✭✭✭
    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:
    CPtr c = new C();
    Cs seq;
    seq.push_back(c);
    
  • lancepwlancepw Member Lance WelshOrganization: BooyahProject: iPhone
    Thanks

    Thank you - your guidance is very-much apprediated! -L.
Sign In or Register to comment.