help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: a method that returns std::vector&


From: Lionel B
Subject: Re: a method that returns std::vector&
Date: Mon, 9 Feb 2009 09:56:00 +0000 (UTC)
User-agent: Pan/0.133 (House of Butterflies)

On Sun, 08 Feb 2009 09:36:09 -0800, ArbolOne wrote:

> I am learning std::vector, like this:
>
> class MyClass{
>       private:
>          std::vector<MyContainer> record;
>       public:
>       MyClass();
> 
>       void setRecord(std::vector& obj);
                       ^^^^^^^^^^^  
//      std::vector is a template - must specify
//      template parameter. Also best to make the
//      parameter const, as it is (should) not be
//      changed by a "set" method.

        void setRecord(const std::vector<MyContainer>& obj) {record = obj;}

>       std::vector<MyClass>& getRecord(){ return record;}
                    ^^^^^^^
//      Template parameters must match - std::vector<typeA> and
//      std::vector<typeB> are different types. Also best to
//      make a "get" method const, as it doesn't (shouldn't)
//      change the invoking object.

        std::vector<MyContainer>& getRecord() const {return record;}

>    };

I'd suggest that in future you try to compile your code and look carefully 
at any compiler error messages (admittedly they can be quite obscure, 
particularly where templates are involved...).

I'd also recommend that you post queries about C++ language issues (ie. 
not g++-specific) to a C++ language newsgroup, such as comp.lang.c++.

Best,

-- 
Lionel B


reply via email to

[Prev in Thread] Current Thread [Next in Thread]