クラス グラフの演算子を「-」にオーバーロードしました。その使用は完全に直感的ではありません (悪いコーディング - 私は知っています) が、グラフ 3 = グラフ 2-グラフ 1 を実行すると、グラフ 3 はグラフ 2 とグラフ 1 の両方でそれらの頂点のみを受け取ることになっています。
したがって、コードを記述し、デバッガーを実行すると、演算子関数は新しい「グラフ」を作成して返すように見え、適切な頂点を新しいグラフに追加し、デバッガーは op 関数を終了するように見えます。 、しかし、メインに戻ることはありません。まるで私が何かを入力するのを待っているかのようです。エラー メッセージは表示されません。
コードは次のとおりです。
char stringy[100];
//cin>>stringy;
strcpy(stringy,"|12,34,25,2,3,2|(3->2),(2->1),(5->9),(2->1)|");
char* param= new char[sizeof(stringy)];
strcpy(param,stringy);
Graph graph1(param);
char sstring[20] = "|33,34,11|(2->33)|";
Graph graph2(sstring);
cout<<graph2.outSumm()<<endl;
Graph graph3;
//until here everything works fine
graph3= graph1-graph2; //the debugger does this and then
cout<<graph3.outSumm()<<endl;
演算子関数:
Graph Graph::operator- (const Graph& g2) const
{
Graph created;
//goes through "this" list and if value exists in g2 copies it to created
for(int i=0;i<vertList.getSize();i++)
{
if (g2.vertList.find(vertList.read(i))!=999)
created.addVertex(vertList.read(i).getInt());
}
return created;
}
コードブロックを使用しています。
コピー コンストラクター:
Graph(const Graph& g2):
maxVal(g2.maxVal),vertList(g2.vertList),edgeList(g2.edgeList){} ;
代入演算子:
void Graph::operator= (const Graph& g2)
{
if (this==&g2)
{
cout<<"not the greatest idea"<<endl;
return;
}
vertList.delete_List();
edgeList.delete_List();
maxVal=0;
addValues(g2.outSumm());
}