다항식의 구성 요소를 저장하는지도가 있습니다. 키는 지수이고 값은 계수입니다. 정수 키와 정수 값을 갖는 맵. 내 인쇄 기능에서 iterating 오전, 내 반복 할 때 프로그램이 충돌합니다. 키를 배열에 집어 넣는 동안 모든 것이 체크 아웃 된 것처럼 보였습니다. 입력 파일이 각 쌍은 형식 -1 0 5 1 20 -2 3 -3 2 1 1 2 3 1 -2 9 (계수 지수) .`지도 C 반복을 통해
//header
#ifndef poly_h
#define poly_h
#include <map>
class poly {
private:
std::map<int, int>* pol;
public:
poly(char* filename);
poly& operator+(poly& rhs);
poly& operator-(poly& rhs);
poly& operator*(poly& rhs);
void print();
~poly();
};
#endif
//cpp file
#include "poly.h"
#include <iostream>
#include <fstream>
using namespace std;
poly::poly(char* filename)
{
map<int, int>* pol = new map<int, int>;
ifstream input_file;
input_file.open("input.txt");
int coe;
int exp;
while (input_file >> coe) {
input_file >> exp;
cout << coe << " ^" << exp << " ";
map<int, int>::iterator it = pol->find(exp);
if (it == pol->end()) {
(*pol)[exp] = coe;
}
else {
(*pol)[exp] += coe;
if ((*pol)[exp] == 0) {
pol->erase(it);
}
}
cout << (*pol)[exp];
//print();
cout << endl;
}
input_file.close();
}
void poly::print()
{
cout << "inside print<<endl;
for (map<int, int>::iterator outer_iter = pol->begin(); outer_iter != pol->end(); ++outer_iter);
cout << "done";
}
poly::~poly()
{
delete pol;
}
을 갖는다
'std :: map * pol;'- 포인터가 될 필요가 없으며 나중에'new'와'delete'를 사용합니다. 필요한 것은'std :: map pol;'입니다. 또한 서식을 정리할 수 있습니까? 모든 곳에서 코드를 읽는 것이 어렵습니다. –
PaulMcKenzie
또한 [메인] 기능을 의미하는 [mcve]를 게시하십시오. 그 이유는 간단하게 2 또는 3 라인 프로그램을 사용하여 프로그램 (모든 포인터로 인해)을 쉽게 끊을 수 있기 때문입니다. 또한 operator +, -, *는 참조가 아닌 새로운'poly' 객체를 반환해야합니다. – PaulMcKenzie
cout << (* pol) [특급]; 위의 몇 줄을 지우므로 값이 0이되면 오류가 발생합니다. – Alex