C++ 映射和节点,错误IntelliSense:没有从;const std::pair<;常数整数,双精度>&引用;至;节点";存在
我编写了这段代码,用于使用映射和节点读取和添加多项式 该错误发生在运算符+。我知道我没有将映射与节点关联起来的代码,我想我应该使用“std::map PolynomialMap”中的某些内容;类似于列表,但我不确定是什么。 或者我应该完全改变我的代码,使用另一种方法? 如果我的问题不够好,请告诉我如何改进C++ 映射和节点,错误IntelliSense:没有从;const std::pair<;常数整数,双精度>&引用;至;节点";存在,c++,dictionary,operator-overloading,nodes,polynomials,C++,Dictionary,Operator Overloading,Nodes,Polynomials,我编写了这段代码,用于使用映射和节点读取和添加多项式 该错误发生在运算符+。我知道我没有将映射与节点关联起来的代码,我想我应该使用“std::map PolynomialMap”中的某些内容;类似于列表,但我不确定是什么。 或者我应该完全改变我的代码,使用另一种方法? 如果我的问题不够好,请告诉我如何改进 #include <iostream> #include <fstream> #include <string> #include <list>
#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();
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
定义表结点
{
双cof;//系数
int deg;//度
}节点;//多项式的节点
类多项式
{
私人:
映射多项式映射;
公众:
CPolynomial();
CPolynomial(常量字符串和文件);
虚~CPolynomial();
CPolynomial运算符+(常数CPolynomial&right);
CPolynomial和运算符=(常数CPolynomial和right);
私人:
void AddOneTerm(节点项);//将一项添加到m_多项式中
};
int main()
{
CPolynomial p1(“P3.txt”);
多项式p2(“P4.txt”);
p多项式p3;
p3=p1+p2;
返回0;
}
CPolynomial::CPolynomial()
{
;
}
CPolynomial::CPolynomial(常量字符串和文件)
{
节点项;
fstream-MyFile;
字符串p;
int-num;
打开(文件);
如果(!MyFile.is_open())
{
cerr p>>num;
map::iterator it=PolynomialMap.begin();
for(int i=0;i>term.deg>>term.cof;
附加条款(期限);
}
MyFile.close();
}
}
CPolynomial CPolynomial::运算符+(常数CPolynomial&右)
{
多项式温度多项式;
temp_Polynomy.PolynomialMap=多项式ALMAP;
map::const_迭代器it;it=right.PolynomialMap.begin();
for(;it!=right.PolynomialMap.end();++it)//
{
AddOneTerm(*it);//错误C2664:'void CPolynomial::AddOneTerm(Node)':无法将参数1从'const std::pair'转换为'Node'
//IntelliSense:不存在从“const std::pair”到“Node”的合适的用户定义转换
}
映射和结果=多项式映射;
多项式映射=临时多项式。多项式映射;
temp_多项式。多项式map=求和结果;
sum_result.clear();
返回温度多项式;
}
CPolynomial和CPolynomial::运算符=(常数CPolynomial和right)
{
此->PolynomialMap=right.PolynomialMap;
归还*这个;
}
void CPolynomial::AddOneTerm(节点项)
{
auto it=多项式映射。begin();
while(it!=多项式映射.end()&&it->firstfirst)
{
it->second+=term.cof;
}
其他的
{
多项式映射插入(成对(术语deg,术语cof));
}
}
CPolynomial::~CPolynomial()
{
多项式映射清除();
}
问题在于,您编写的AddOneTerm
方法期望的参数是节点
结构,但在操作符+
方法中,您试图向其传递的参数是常量std::pair
有多种方法可以解决这个问题。以下代码通过在Node类中提供一个构造函数来修改代码以解决此问题,该构造函数可以将const std::pair
转换为节点
。还需要添加无参数构造函数,因为在通过读取文件构造多项式时,使用的是节点
的隐式无参数构造函数
typedef struct Node
{
double cof; // coefficient
int deg; // degree
Node()
{
deg = 0;
cof = 0.0;
}
Node(const pair<const int, double> & nodePair)
{
deg = nodePair.first;
cof = nodePair.second;
}
} Node; // the node of polynomial
typedef结构节点
{
双cof;//系数
int deg;//度
节点()
{
deg=0;
cof=0.0;
}
节点(常量对和节点对)
{
deg=第一个节点pair;
cof=nodePair.second;
}
}节点;//多项式的节点
谢谢你,埃文,这是一个简单而有用的解决方案。您能告诉我您还将使用什么方法解决此问题吗?解决此问题的其他两种方法是:(1)使用方法签名void AddOneTerm(const pair)
向CPolynomial
添加另一个方法,该方法创建一个节点并调用现有的AddOneTerm
方法;(2)将AddOneTerm
方法更改为具有int
参数和double
参数而不是Node
参数,调用AddOneTerm
方法时,将该对解压为it->first
和it second
,并直接使用CPolynomial
构造函数中的deg
和cof
变量。使用方法(2),<代码>节点< /C>结构可以被删除。请考虑从问题中移除智能感知,因为它不是原因。