当我尝试将新节点添加到列表中时,我收到错误消息"no matching function call"
When I try to add a new node to the list, I get error message "no matching function call"
我正在尝试编写一个函数,该函数将具有等效指数值添加到多项式项中,并将该项放在新的链表中。我知道我这样做的逻辑还不正确,但我至少希望看到我可以将新术语添加到新列表中。每次我尝试这样做时,我都会收到错误消息no matching function call to Polynomial::Term::Term
这是我正在尝试编写的函数。我在将头设置为等于新术语的行上出现错误。
Polynomial Polynomial::operator+( const Polynomial &other ) const
{
double sum;
shared_ptr<Polynomial> result = shared_ptr<Polynomial>(new Polynomial()); // where you are going to store your results
shared_ptr<Polynomial::Term> a = this->head;
shared_ptr<Polynomial::Term> b = other.head;
while(a != nullptr && b != nullptr)
{
for(a; a != nullptr; a=a->next)
{
for(b; b!=nullptr; b=b->next)
{
if(a->exponent == b->exponent)
{
sum = a->coeff+b->coeff;
head = shared_ptr<Term>(new Term( sum, exp, head ));
}
}
}
}
头文件
#ifndef H_POLYNOMIAL_H
#define H_POLYNOMIAL_H
#include <ostream> // to be able to declare overloaded << operator as a friend
#include <string> // input type to the main constructor
#include <tr1/memory> // for shared_ptr
#include <tr1/shared_ptr.h>
using namespace std;
class Polynomial
{
friend ostream &operator<<( ostream &, const Polynomial & );
public:
Polynomial(); // default polynomial is empty (= 0)
Polynomial( string & ); // Set the polynomial according to the string
Polynomial &operator=( const Polynomial & ); // assignment
bool operator==( const Polynomial & ) const; // equality test
bool operator!=( const Polynomial & ) const; // not equal test
Polynomial operator+( const Polynomial & ) const; // addition
double eval( double x ) const ; // evaluate the polynomial at x
private:
class Term // a Term of the polynomial
{
public:
Term( double c, int e, shared_ptr<Term> n );
double coeff; // the coefficient
int exponent; // the exponent
shared_ptr<Term> next;
};
shared_ptr<Term> head; // The head of the list
static double TOL; // Tolerance for floating point equality
};
#endif
类实现
Polynomial::Term::Term( double c, int e, shared_ptr<Term> n )
{
coeff = c; exponent = e; next = n;
}
//+--------------------------------------+
//| Default Constructor: Polynomial is 0 |
//+--------------------------------------+
Polynomial::Polynomial()
{
head = nullptr;
}
//+-------------------------------------------------------------+
//| Constructor: The input string contains coefficient-exponent |
//| pairs where everything is separated by whitespace |
//+-------------------------------------------------------------+
Polynomial::Polynomial( string & str )
{
stringstream ss( str ); // stringstream lets us extract items separated by
// whitespace simply by using the >> operator
double coefficient; // to hold the coefficient
int exp; // to hold the exponent
head = nullptr; // initialize head to null
// read in coefficient-exponent pairs and add each term to the list
// ----------------------------------------------------------------
while (ss >> coefficient >> exp)
if (coefficient != 0) // don't make a 0 term
head = shared_ptr<Term>(new Term( coefficient, exp, head ));
}
据我所知,exp
当您尝试
new Term( sum, exp, head )
我猜你的意思是b->exponent
什么的。
除了您没有声明exp
变量并且您似乎对值语义有一些混淆之外,这是无效的:
Polynomial Polynomial::operator+( const Polynomial &other ) const
{
//...
head = shared_ptr<Term>(new Term( sum, exp, head ));
}
此赋值无效,因为函数标记为 const
,因此head
是一个const std::shared_ptr
。你可能不想要一个变异的operator+
,所以我建议复制Polynomial
并对其进行操作。最好通过定义operator+=
并根据数据副本实现
相关文章:
- C++错误消息*成员参考.**初学者*
- 如何通过参数抛出错误消息
- glad 导致 glfwSwapBuffers 返回错误消息
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 重新定义预定义的 errno 错误消息 (E2BIG)
- SDL 映像:无法打开映像,仅显示错误消息
- 错误消息:使用"string* +="后"no match for 'operator+='"
- 错误消息"expected expression"....有人知道它为什么这么说吗?
- 在 Eclipse: "error: no match for 'operator='" 中获取错误消息
- GCC 警告和 gcc 错误消息之间的区别
- 为 c++ cin 上的输入数无效生成错误消息
- 来自带有mbedtls TLS的Mongoose Web服务器的错误消息
- 如何使用realsense摄像头调试ROS错误消息
- C++ 概念 - 需要括号中的概念会导致 2 条冲突的错误消息
- 编译器收到错误消息(textc.exe已停止)
- 结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
- 使用 c++/WinRT Direct3D UWP Game DR 模板 Direct X11 的错误消息
- max_element() 给出非常奇怪的错误消息
- 不断收到错误消息,并非所有控制路径都返回值