お世話になります。
異なるクラス間で、同じ型のvectorをコピーしたいのですが
下記のように、書きましたが、クラスのメンバー変数にすると、値の受け渡しが
できません、巧い方法はないでしょうか、ご教示願えないでしょうか。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Lineedit{
    int line_num;
    string instruction;
};
// vector <Lineedit> lhs;	// グローバルに置けばOK
class Foo
{
public:
	vector <Lineedit> lhs;
	void DataSet();
	vector <Lineedit>& GetData();
};
void Foo::DataSet()
{
	Lineedit le;
	le.line_num = 1;
	le.instruction = A;
	lhs.push_back(le);
	le.line_num = 2;
	le.instruction = B;
	lhs.push_back(le);
}
vector <Lineedit>& Foo::GetData()
{
	return lhs;
}
class Hoge
{
public:
	Foo foo;
	vector <Lineedit> rhs;	// クラスのメンバー変数にすると、値の受け渡しができない。
	void SetData();
};
void Hoge::SetData()
{
	rhs.assign(foo.GetData().begin(), foo.GetData().end());
}
int main()
{
	Foo foo; Hoge hoge;
	foo.DataSet();
	hoge.SetData();
	cout << hoge.rhs[0].line_num << hoge.rhs[0].instruction << endl;
	cout << hoge.rhs[1].line_num << hoge.rhs[1].instruction << endl;
    return 0;
}
クラスと変数が区別ついてないからでしょ。
以下は理由
void Hoge::SetData()
{
    rhs.assign(foo.GetData().begin(), foo.GetData().end());
    /*
    このHoge::SetDataからは、
    main関数が呼ばれたらスタック上に作成され
    そしてmain関数が終わると消えてしまう
    foo変数にアクセスできない。
    そもそもfooの存在すら知らない。
    */
}
int main()
{
    Foo foo; Hoge hoge;
    省略
}
たぶんこういうことかな。
動作確認はしてない。
void Hoge::SetData(vector<Lineedit>& als)
{
    rhs.assign(als.begin(), als.end());
}
int main()
{
    Foo foo; Hoge hoge;
    foo.DataSet();
    hoge.SetData(foo.GetData());
    省略
}
wclrp ( 'o') さん、ありがとうございました。
良く理解できました。

 
  
  
  
  