Question

I've basically the following two classes for which I use return-by-value functions to create objects. In the Bar class below, I've two Foo class member objects. How could I initialize correctly, each of those two objects separately? Below, I'm giving an ilustration about the compiler error that is displayed.

template < typename T >
class Foo{

    public:
        Foo( );
        Foo( const Foo<T> & );
        ~Foo();

        friend Foo<T> createFoo1( double, bool );
        friend Foo<T> createFoo2( double, bool );
        friend Foo<T> createFoo3( int );

    private:

        std::vector<T> m_data;

};

template < typename T >
Foo<T> createFoo1( double param1, bool param2 ){
    Foo<T> myFoo;

    // fill myFoo.
    return (myFoo);
}

template < typename T >
class Bar{

    public:

        Bar( );
        Bar( const Foo<T> &, const Foo<T> & );
        Bar( const Bar<T> & );
        ~Bar( );

        friend Bar<T> createBar1( double, bool );

    private:
        Foo<T> m_fooY;
        Foo<T> m_fooX;
};

template < typename T >
Bar<T> createBar1( double param1, bool param2 ){
    Bar<T> myBar( createFoo1<T>(param1, param2), createFoo1<T>(param1, param2) ); //OK
    return (myBar);

    //Bar<T> myBar;
    //myBar.m_fooY(createFoo1<T>(param1, param2)); // <- error C2064: term does not evaluate to a function taking 1 arguments
    //myBar.m_fooX(createFoo1<T>(param1, param2)); // <- error C2064: term does not evaluate to a function taking 1 arguments
    //return (myBar);
}
Était-ce utile?

La solution

Here is how you can set the fields m_fooX and m_fooY other than through the constructor:

template < typename T >
Bar<T> createBar1( double param1, bool param2 ){
  Bar<T> myBar;
  myBar.m_fooY = createFoo1<T>(param1, param2);
  myBar.m_fooX = createFoo1<T>(param1, param2);
  return myBar;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top