std::vector RTE(Run Time Error),

Discussion in 'C++' started by ArbolOne, Sep 5, 2012.

  1. ArbolOne

    ArbolOne Guest

    class a_class{
    public:
    int id;
    Glib::ustring fname;
    };
    std::vector<a_class>& my_class::my_method(){
    a_class obj;
    obj.id = 1;
    obj.fname = "Me";

    std::vector<a_class> v;
    v.push_back(obj);

    return v;
    }
    void a_method(){
    std::vector<a_class> v;
    my_class obj;
    v = obj.my_method();
    }

    The program above gives me an class a_class{
    public:
    int id;
    Glib::ustring fname;
    };
    std::vector<a_class>& my_class::my_method(){
    a_class obj;
    obj.id = 1;
    obj.fname = "Me";

    std::vector<a_class> v;
    v.push_back(obj);

    return v;
    }
    void a_method(){
    std::vector<a_class> v;
    my_class obj;
    v = obj.my_method();
    }

    The program above gives me an RTE(Run Time Error), what am I doing wrong?what am I doing wrong?
    ArbolOne, Sep 5, 2012
    #1
    1. Advertising

  2. ArbolOne

    Luca Risolia Guest

    On 06/09/2012 00:16, ArbolOne wrote:

    > The program above gives me an RTE(Run Time Error), what am I doing wrong?what am I doing wrong?


    What "Run Time Error"? Your code does not even compile.
    Luca Risolia, Sep 5, 2012
    #2
    1. Advertising

  3. ArbolOne

    Ian Collins Guest

    On 09/ 6/12 10:16 AM, ArbolOne wrote:

    If you want help, explain the context specific terms (such as ustring
    and RTE) and post something that compiles!

    > class a_class{
    > public:
    > int id;
    > Glib::ustring fname;
    > };
    > std::vector<a_class>& my_class::my_method(){
    > a_class obj;
    > obj.id = 1;
    > obj.fname = "Me";
    >
    > std::vector<a_class> v;
    > v.push_back(obj);
    >
    > return v;
    > }


    This return a reference to a temporary object which is a very bad idea.

    > void a_method(){
    > std::vector<a_class> v;
    > my_class obj;
    > v = obj.my_method();
    > }
    >
    > The program above gives me an class a_class{
    > public:
    > int id;
    > Glib::ustring fname;
    > };
    > std::vector<a_class>& my_class::my_method(){
    > a_class obj;
    > obj.id = 1;
    > obj.fname = "Me";
    >
    > std::vector<a_class> v;
    > v.push_back(obj);
    >
    > return v;
    > }


    So does this.

    > void a_method(){
    > std::vector<a_class> v;
    > my_class obj;
    > v = obj.my_method();
    > }
    >
    > The program above gives me an RTE(Run Time Error), what am I doing wrong?what am I doing wrong?


    It should also give you compiler warnings for the two cases above.

    --
    Ian Collins
    Ian Collins, Sep 5, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim

    Java RTE VS. Java SDK

    Tim, Dec 3, 2003, in forum: Java
    Replies:
    2
    Views:
    909
    Petarian
    Dec 3, 2003
  2. Anonymous
    Replies:
    20
    Views:
    4,252
    Pete Becker
    Mar 30, 2005
  3. Jason Heyes
    Replies:
    8
    Views:
    703
    Andrew Koenig
    Jan 15, 2006
  4. Replies:
    8
    Views:
    1,877
    Csaba
    Feb 18, 2006
  5. Rune Allnor
    Replies:
    4
    Views:
    916
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page