Ich schrieb diesen Code zum Lesen und Hinzufügen von Polynomen mit Karte und Knoten.Karte und Knoten, Fehler IntelliSense: keine geeignete benutzerdefinierte Konvertierung von "const std :: pair <const int, double>" zu "Node" existiert
Der Fehler tritt in Operator + auf. Ich weiß, dass ich keinen Code habe, der meine Karte mit Knoten verbindet, ich denke, ich sollte etwas in "std :: map PolynomialMap" verwenden; ähnlich wie Liste, aber ich bin mir nicht sicher was. Oder vielleicht sollte ich meine Codes völlig ändern und eine andere Methode verwenden? Bitte lassen Sie mich wissen, wie meine Frage zu verbessern, wenn es nicht gut genug
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <vector>
#include <map>
using namespace std;
typedef struct Node
{
double cof; // coefficient
int deg; // degree
} Node; // the node of polynomial
class CPolynomial
{
private:
std::map<int, double> PolynomialMap;
public:
CPolynomial();
CPolynomial(const string& file);
virtual ~CPolynomial();
CPolynomial operator+(const CPolynomial &right);
CPolynomial& operator=(const CPolynomial &right);
private:
void AddOneTerm(Node term); // add one term into m_Polynomial
};
int main()
{
CPolynomial p1("P3.txt");
CPolynomial p2("P4.txt");
CPolynomial p3;
p3 = p1 + p2;
return 0;
}
CPolynomial::CPolynomial()
{
;
}
CPolynomial::CPolynomial(const string& file)
{
Node term;
fstream MyFile;
string p;
int num;
MyFile.open(file);
if (!MyFile.is_open())
{
cerr << "Unable to open input file" << endl;
exit(EXIT_FAILURE);
}
else
{
MyFile >> p >> num;
map <int, double>::iterator it = PolynomialMap.begin();
for (int i = 0; i < num; i++)
{
MyFile >> term.deg >> term.cof;
AddOneTerm(term);
}
MyFile.close();
}
}
CPolynomial CPolynomial:: operator+(const CPolynomial &right)
{
CPolynomial temp_polynomial;
temp_polynomial.PolynomialMap = PolynomialMap;
map <int, double> ::const_iterator it; it = right.PolynomialMap.begin();
for (; it != right.PolynomialMap.end(); ++it) //
{
AddOneTerm(*it); //error C2664: 'void CPolynomial::AddOneTerm(Node)' : cannot convert argument 1 from 'const std::pair<const _Kty,_Ty>' to 'Node'
//IntelliSense: no suitable user-defined conversion from "const std::pair<const int, double>" to "Node" exists
}
map <int, double> sum_result = PolynomialMap;
PolynomialMap = temp_polynomial.PolynomialMap;
temp_polynomial.PolynomialMap = sum_result;
sum_result.clear();
return temp_polynomial;
}
CPolynomial& CPolynomial:: operator=(const CPolynomial &right)
{
this->PolynomialMap = right.PolynomialMap;
return *this;
}
void CPolynomial::AddOneTerm(Node term)
{
auto it = PolynomialMap.begin();
while (it != PolynomialMap.end() && it->first < term.deg)
{
++it;
}
if (it != PolynomialMap.end() && term.deg == it->first)
{
it->second += term.cof;
}
else
{
PolynomialMap.insert(pair<int, double>(term.deg, term.cof));
}
}
CPolynomial::~CPolynomial()
{
PolynomialMap.clear();
}
Danke Evan, das war eine einfache und nützliche Lösung. Können Sie mir bitte mitteilen, welche andere Methode Sie für dieses Problem verwenden würden? – Saeedeh
Zwei weitere Möglichkeiten, das Problem zu beheben, sind (1) fügen Sie eine weitere Methode zu "CPolynomial" mit der Methodensignatur 'void AddOneTerm (const pair)' hinzu, die einen 'Node' erzeugt und den vorhandenen' AddOneTerm' aufruft Methode und (2) ändern Sie die 'AddOneTerm' Methode, um ein' int' Argument und ein 'double' Argument anstelle eines' Node' Arguments zu haben, entpacken Sie das Paar in 'it-> first' und' it-second' beim Aufruf die 'AddOneTerm'-Methode, und arbeiten direkt mit' deg'- und 'cof'-Variablen im' CPolynomial'-Konstruktor. Mit approach (2) konnte die 'Node' Struktur gelöscht werden. –