views:

275

answers:

3

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());
}
+2  A: 

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:

char stringy[100] = "|12,34,25,2,3,2|(3->2),(2->1),(5->9),(2->1)|";
Graph graph1( stringy );

or even:

Graph graph1( "|12,34,25,2,3,2|(3->2),(2->1),(5->9),(2->1)|" );

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.

anon
+2  A: 

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.

Dan Breslau
What do you mean by breaking in the debugger?
Dave
@Meir: I meant using the debugger to temporarily stop execution, without exiting the program. (This lets you examine the stack, variables, etc. just as if you'd reached a breakpoint.)
Dan Breslau
A: 

Instead of implementing operator-, implement operator-= instead:

Graph& Graph::operator-=(const Graph& rhs)
{
    // remove nodes not in both Graphs

    return *this;
}

Then just implement operator- in terms of that:

Graph Graph::operator-(const Graph& rhs)
{
    Graph temp(*this);
    temp -= rhs;
    return temp;
}

See if this works.

rlbond
Is there a use case for -= or are you adding it because the books say it is a good idea. If there is a use case fine (then defining - in terms of -= is a good idea) but adding operators just because is a bad idea. Only implement what you need!
Martin York
The case is splitting assignment from the actual operation. Moreover, if you are going to overload operators, you'd better do it right. Personally, I don't see a reason for the operator overloading at all, but this operation still is something that is similar and probably needed.
rlbond
I agree if you are going to do it do it right. But that is not an argument for your example. Before you do it you should analyses the use cases to see if it is a valid use case. In this situation there are no use cases for -= and thus it would 'probably' be wasted effort and unused code. Note: Something is only correct in the context within which it is used to argue that it is correct you must first justify its existance.
Martin York