For an assignment I must implement an operator -= to remove points from a canvas (that have been plotted broken line graph style).

Here's the part where the problem is occuring. It certainly removes points, just not the right ones seemingly and not all of them. It doesn't make much sense to me...

CPlotter & CPlotter::operator-=(CPt const & rhs)
{
	bool bExists(false);
	vector<CPt> tmpPts;
	for (unsigned int i(0); i < _pts.size(); ++i)
	{
		if (_pts[i] == rhs)
			bExists = true;
		else
			tmpPts.push_back(_pts[i]);
	}
	if (!bExists)
		throw exception("CPlotter::operator-= - Point does not exist");
	_pts = tmpPts;
	return *this;
}

CPlotter & CPlotter::operator-=(CPlotter const & rhs)
{
	for (unsigned int i(0); i < rhs._pts.size(); ++i)
	{
		try
		{
			*this -= rhs._pts[i];
		}
		catch (exception exc)
		{
		}
	}
	return *this;
}

CPlotter operator-(CPlotter const & lhs, CPlotter const & rhs)
{
	CPlotter tmp(lhs);
	tmp-=rhs;
	return tmp;
}

Here's the header file for the CPlotter and CPt:

#pragma once
#include <iostream>
#include "Drawer.h"
#include "Pt.h"
#include <vector>
#include <algorithm>
using namespace std;

class CPlotter
{
	vector<CPt> _pts;
	COLORREF	_COLOR;
public:
	CPlotter & operator<<(CPt const &);
	CPlotter(COLORREF const & = RGB(255,255,255));
	~CPlotter(void);
	
	CPlotter & operator-=(CPlotter const &);
	CPlotter & operator-=(CPt const &);
	bool operator==(CPlotter const &) const;
	CPlotter operator+(CPlotter const &);
	friend CDrawer & operator<<(CDrawer &, CPlotter &);
};

CPlotter & operator+=(CPlotter &, CPlotter const &);
bool operator!=(CPlotter const &, CPlotter const &);
CPlotter operator-(CPlotter const &, CPlotter const &);


#pragma once
#include "Drawer.h"

class CPt
{
	int _iX,
		_iY;
	COLORREF _COLOR;
	COLORREF GetRandCol(void);
public:
	bool operator<(CPt const &) const; 
	CDrawer const & LineTo(CDrawer &, CPt const &);
	void SetColor(COLORREF const &);
	CPt(int, int);
	~CPt(void);

	friend bool operator==(CPt const &, CPt const &);
};

bool operator!=(CPt const &, CPt const &);

I realise it might be a bit hard to debug with this info (and no comments) but if anyone needs any more info just ask. Help would be really appreciated

Member Avatar for jencas

For an assignment I must implement an operator -= to remove points from a canvas (that have been plotted broken line graph style).

Here's the part where the problem is occuring. It certainly removes points, just not the right ones seemingly and not all of them. It doesn't make much sense to me...

CPlotter & CPlotter::operator-=(CPt const & rhs)
{
    bool bExists(false);
    vector<CPt> tmpPts;
    for (unsigned int i(0); i < _pts.size(); ++i)
    {
        if (_pts[i] == rhs)
            bExists = true;
        else
            tmpPts.push_back(_pts[i]);
    }
    if (!bExists)
        throw exception("CPlotter::operator-= - Point does not exist");
    _pts = tmpPts;
    return *this;
}

end quote.

What about using STL algorithms and avoiding the temporary copy of the vector????
Considering this should result in code like (recite from memory, not tested!!!):

CPlotter & CPlotter::operator-=(CPt const & rhs)
{
        if (pts_.erase(remove_if(pts_.begin(), pts_.end(), bind2nd(equal_to<CPt>(), rhs))) == pts_.end())
        throw exception("CPlotter::operator-= - Point does not exist");
    return *this;
}
commented: To the point and timely +1

Hah thanks. The problem is already solved now though. The reason we we're doing it the inefficient way is because our teacher was too lazy to go through all the vector class specs =P.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.