a class of polynomials

hey guys.

ive written a class for polynomials. everything is working, except the addition of polynomials.

the following Error occurs:
Debug Assertion Failed!
Program:C: ...
Expression: map/set iterator not dereferencable
For information ...


what is going wrong there? i suppose that the function add isnt correct, but i cant see the bug.

testpolynome.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "polynome.h"
#include <string>

int main()
{
	poly f;
	poly g;
	poly c;
	
	g.input();
	f.input();

	c.add(f,g);
	cout<<"f+g = "<<endl;
	c.output();
	
    return 0;
}


polynome.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//deklarationen

#include <map>
#include <iostream>
using namespace std;

class poly
{
public:
	//constructor
	poly();
	//destructor
	~poly();
	//addition
	 void add(poly ,poly); // this function is to add two polynomials v and w.
	 void output();
	 void input();
private:
  map<int,float> polymap; //exponents=keys are unsigned int type, coefficients=values are float typ

};



polynome.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//implementierungen

#include "polynome.h"

poly::poly()//constructor
{	
	cout<<"an empty polynomial has been created!"<<endl;
}

void poly::input()
{
    cout << "please input a polynomial."  <<endl;

    int key;
    while (cin >> key && key != -1)
        cin >> polymap[key];
	
	cout<<"the polynomial "<<endl;
	output();
	cout<<" has been created."<<endl;
	}


poly::~poly()
{
	cout<<"destructor called"<<endl;
	polymap.clear() ;//deletes the polynomial
	cout <<"the polynomial" <<endl;
	output();
	cout << "will be deleted now."<<endl;
	polymap.clear();
}



void poly::add(poly v,poly w)
{
  map<int,float>::iterator iterv=(v.polymap).begin();
  map<int,float>::iterator iterw=(w.polymap).begin();
 
  polymap.clear();
  
  while(++iterv!=(v.polymap).end())
   polymap[iterv->first]+=iterv->second;
  while(++iterw!=(w.polymap).end())
   polymap[iterw->first]+=iterw->second;
  
}


void poly::output()//uebergabe des iterators
{	
	map<int,float>::iterator iter=polymap.begin();

	cout<<"("<<iter->second <<")*X^"<< iter->first;
	while (++iter!=polymap.end())
		cout <<"+("<< iter->second <<")*X^"<< iter->first;
	cout <<endl;
}


greetings a confused rasputinxiaoshitou
Last edited on
Topic archived. No new replies allowed.