How to use vector with classes in it as return type?

A

Armin Menzel

The function getVOVector() returns a vector of type vector<TestVO>.

This works:
vector<TestVO> lvTestVOs = loVectorHandler.getVOVector();

That works NOT:
vector<TestVO> lvTestVOs;
lvTestVOs = loVectorHandler.getVOVector();

Why?

Thanks
Armin

P.S.: This is the only operator TestVO has:

TestVO& TestVO::eek:perator=(TestVO& aoTestVO) {

msName = aoTestVO.getName();

return *this;

}

Do I need more?
(I am new to c++-vectors, so forgive me the ugly question)
 
J

John Harrison

Armin Menzel said:
The function getVOVector() returns a vector of type vector<TestVO>.

This works:
vector<TestVO> lvTestVOs = loVectorHandler.getVOVector();

That works NOT:
vector<TestVO> lvTestVOs;
lvTestVOs = loVectorHandler.getVOVector();

Why?

Thanks
Armin

P.S.: This is the only operator TestVO has:

TestVO& TestVO::eek:perator=(TestVO& aoTestVO) {

msName = aoTestVO.getName();

return *this;

}

Do I need more?
(I am new to c++-vectors, so forgive me the ugly question)

You need a copy constructor as well. You also need them to be written
correctly, which is impossible to say from the code posted. I would post the
entire TestVO class, the error is almost certainly in there.

john
 
A

Armin Menzel

Here it goes:

TestVO.cpp:

#include "TestVO.h"
TestVO::TestVO() {
printf("TestVO: Empty constructor called.\n");
}
TestVO::TestVO(string asName) {
setName(asName);
printf("TestVO: Constructor called and msName set to %s.\n",
asName.c_str());
}
TestVO::~TestVO() {
printf("TestVO: Empty destructor called.\n");}
TestVO& TestVO::eek:perator=(TestVO& aoTestVO) {
msName = aoTestVO.getName();
return *this;}
void TestVO::setName(string asName) {msName = asName;}
string TestVO::getName() {return msName;}

TestVO.h:

#ifndef _HEADER_TESTVO_h_
#define _HEADER_TESTVO_h_
#include <string>
using namespace std;
/*! \brief Stores attributes of a preset
*/
class TestVO {
public:
TestVO();
TestVO(string asName);
~TestVO();
TestVO& operator=(TestVO& aoTestVO);
void setName(string asName);
string getName();
private:
string msName; //!< name of preset
};
#endif
 
J

John Harrison

Armin Menzel said:
Here it goes:

TestVO.cpp:

#include "TestVO.h"
TestVO::TestVO() {
printf("TestVO: Empty constructor called.\n");
}
TestVO::TestVO(string asName) {
setName(asName);
printf("TestVO: Constructor called and msName set to %s.\n",
asName.c_str());
}
TestVO::~TestVO() {
printf("TestVO: Empty destructor called.\n");}
TestVO& TestVO::eek:perator=(TestVO& aoTestVO) {
msName = aoTestVO.getName();
return *this;}
void TestVO::setName(string asName) {msName = asName;}
string TestVO::getName() {return msName;}

TestVO.h:

#ifndef _HEADER_TESTVO_h_
#define _HEADER_TESTVO_h_
#include <string>
using namespace std;
/*! \brief Stores attributes of a preset
*/
class TestVO {
public:
TestVO();
TestVO(string asName);
~TestVO();
TestVO& operator=(TestVO& aoTestVO);
void setName(string asName);
string getName();
private:
string msName; //!< name of preset
};
#endif

OK, that's a very simple class. Your class should look like this

class TestVO {
public:
TestVO();
TestVO(const string& asName);
void setName(const string& asName);
string getName() const;
private:
string msName; //!< name of preset
};


TestVO::TestVO() {
}
TestVO::TestVO(const string& asName) : msName(asName) {
}
void TestVO::setName(const string& asName) {msName = asName;}
string TestVO::getName() const {return msName;}

The operator= is not needed because the compiler can generate a perfectly
good operator= for this class. The way you had written the operator= was
unusual because you were not using a const reference for the aoTestVO
parameter. I'm guessing that this is the cause of the problem. I'm also
guessing that the reason you used a non-const reference was becaue you had
not known to declare the getName method as const.

But in any case the simplest thing is to remove the operator=, you do not
need it for this class.

If that doesn't work then post again and explain exactly what does go wrong,
you haven't said upto now.

john
 
A

Armin Menzel

Thank you!

It too me hours not to find it out and I definitely would not have made
it. You saved me some time! The operator was from a tutorial found in
the internet. I should buy a book...

Armin
 

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
474,179
Messages
2,570,956
Members
47,509
Latest member
Jack116

Latest Threads

Top