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

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top