c++ 重载`-`
我已经为我的类图将运算符重载为“-”。 它的用途并不完全 直观(糟糕的编码 - 我知道)但如果我做 graph3 = graph2-graph1 那么图 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());
}
I've done operator overloading to "-" for my class graph. It's use isn't totally
intuitive (bad coding-I know) but if I do graph3 = graph2-graph1 then graph 3 is
supposed to receive only those vertexes in both graph 2 and graph 1.
So, I've written the code and when I run the debugger, the operator- function seems to create a new "graph" to return and it adds the appropriate vertexes to the new graph
and then the debugger seems to exit the op- function, but never makes it back to
main. It's as if it's waiting for me to enter something.
No error messages appear.
Here is the code:
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;
The operator- function:
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;
}
I'm using codeblocks.
Copy constructor:
Graph(const Graph& g2):
maxVal(g2.maxVal),vertList(g2.vertList),edgeList(g2.edgeList){} ;
Assignment operator:
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());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与您的问题没有特别相关(尽管可能是),您为什么不使用 std::string ? 如果您必须使用字符数组,为什么不:
甚至:
或者您是否将传递到类的构造函数中的指针存储起来? 如果是这样,那是一个坏主意,我对使用 std::strings 的建议加倍。
Not specifically related to your question (though it might be), why are you not using std::string? And if you must use character arrays, why not:
or even:
Or are you storing the pointer passed into the constructor in your class? If so, that is a bad idea, and my advice to use std::strings goes double.
听起来你的代码处于无限循环中。 当程序似乎停止时,您是否尝试过中断调试器?
Graph 类中是否定义了复制构造函数? 您需要它才能使用本地
created
变量成功返回值。Sounds like your code is in an infinite loop. Have you tried breaking in the debugger when the program appears to halt?
Do you have a copy constructor defined in the Graph class? You need that in order to successfully return a value using the local
created
variable.不要实现operator-,而是实现operator-=:
然后就这样实现operator-:
看看这是否有效。
Instead of implementing operator-, implement operator-= instead:
Then just implement operator- in terms of that:
See if this works.