std::vector RTE(Run Time Error),

A

ArbolOne

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?
 
L

Luca Risolia

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.
 
I

Ian Collins

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top