связанный список — C ++ & quot; Нет соответствующей функции для вызова & quot; ошибка

Я расширяю код, данный нам. Я должен закончить создание программы, которая может добавить два полинома вместе. Когда я пытаюсь скомпилировать, я получаю эти сообщения сборки.

Line 17: error: no matching function for call to 'Term::Term()'

Line 10: note: candidate: Term::Term(float,int)

Line 10: note: candidate expects 2 arguments, 0 provided

Line 6: note: candidate: Term::Term(const Term&)

line 6: note: candidate expects 1 argument, 0 provided.

Я не совсем уверен, что не так, это наше первое задание по программированию на C ++. TA предоставил большую часть этого кода, я добавил все в заключительном операторе while. Я также добавил float coef; и `int exp; к структуре узла.

Кодировка, которую я использовал в операторе while, заключалась в том, как профессор объяснил, как пройти через многочлены и проверить. У кого-нибудь есть идеи как решить мою ошибку?

#include <iostream>
using namespace std;

class Term {
float coef;
int exp;
public:
Term(float c, int e) { coef = c; exp = e; }
void set_values(float c, int e) { coef = c; exp = e; }
float get_coef() { return coef; }
int get_exp() { return exp; }
};

template <class T>
struct Node {
float coef;
int exp;
T data;
Node * next;
};

template <class T>
class LinkedList {
public:

LinkedList() : head(NULL), size(0) {};
~LinkedList();
bool addNode(T data);
bool deleteNode(T data);
Node<T> * searchNode(T data);
void printList();
void reverseList();
Node<T>*  get_head() { return head; };
private:
Node<T> * head;
int size;

};

template <class T>
LinkedList<T>::~LinkedList() {
Node<T> * tmp = NULL;
while (head) {
tmp = head;
head = head->next;
//cout << "deleting data " << tmp->data << std::endl;
delete(tmp);
}
};

template <class T>
bool LinkedList<T>::addNode(T data) {
try {
Node<T> * tmp = new Node<T>();
tmp->data = data;
tmp->next = head;
head = tmp;
++size;
return true;
}
catch (std::exception & ex) {
return false;
}
}

template <class T>
bool LinkedList<T>::deleteNode(T data) {
Node<T> *current = head, *prev = NULL;

while (current) {
if (current->data == data)
break;

prev = current;
current = current->next;
}

if (current) {
if (prev)
prev->next = current->next;
else
head = current->next;

delete(current);
--size;
return true;
}
else {
return false;
}
}

template <class T>
Node<T> * LinkedList<T>::searchNode(T data) {
Node<T> * tmp = head;
while (tmp) {
if (tmp->data == data) {
return tmp;
}
tmp = tmp->next;
}
return NULL;
}template <class T>
void LinkedList<T>::printList() {
Node<T> * tmp = head;
bool printNewLine = (tmp) ? true : false;
while (tmp) {
std::cout << "(" << tmp->data.get_coef() << "," << tmp->data.get_exp() << ")";
tmp = tmp->next;
if (tmp)
std:cout << ", ";

}

if (printNewLine) {
std::cout << std::endl;
}
}

template <class T>
void LinkedList<T>::reverseList() {
Node<T> *curr = head, *prev = head, *save = NULL;

while (curr) {
save = curr->next;
curr->next = prev;
prev = curr;
curr = save;
}

head->next = NULL;
head = prev;
}

int main() {
LinkedList<Term> p, q, s;
Term pt1(2.0, 4), pt2(3.0, 3), pt3(-7, 0);
p.addNode(pt3);
p.addNode(pt2);
p.addNode(pt1);

Term qt1(-5.0, 5), qt2(3.0, 3), qt3(-7.0, 2), qt4(5.0, 1), qt5(-13.0, 0);
q.addNode(qt5);
q.addNode(qt4);
q.addNode(qt3);
q.addNode(qt2);
q.addNode(qt1);

p.printList();
q.printList();

Node<Term> * p_ptr, *q_ptr, *s_ptr;
p_ptr = p.get_head();
q_ptr = q.get_head();
s_ptr = s.get_head();

cout << "Coef: " << p_ptr->data.get_coef() << " Exp: " << p_ptr->data.get_exp() << endl;
p_ptr = p_ptr->next;
cout << "Coef: " << p_ptr->data.get_coef() << " Exp: " << p_ptr->data.get_exp() << endl;
while (1) {
if (!p_ptr && !q_ptr)
break;
else if (p_ptr->exp==q_ptr->exp){
if (p_ptr->coef + q_ptr->coef != 0) {
s_ptr->coef = p_ptr->coef+q_ptr->coef;
s_ptr->exp = p_ptr->exp;
p_ptr=p_ptr->next;
q_ptr=q_ptr->next;
}
else {
p_ptr=p_ptr->next;
q_ptr=q_ptr->next;
}
}
else if (p_ptr->exp > q_ptr->exp){
s_ptr->coef = p_ptr->coef;
s_ptr->exp = p_ptr->exp;
p_ptr=p_ptr->next;
}
else (q_ptr->exp > p_ptr->exp); {
s_ptr->coef = q_ptr->coef;
s_ptr->exp = q_ptr->exp;
q_ptr=q_ptr->next;
}

//else if exp of p_ptr > exp of q_ptr
//copy the current term of p to r
//p_ptr = p_ptr->next

//else if exp of q_ptr > exp of p_ptr
//copy the current term of q to r
//q_ptr = q_ptr->next

//else if exp of p_ptr == exp of q_ptr
//add coef of p to coef of q
//copy the resultant term in r
//p_ptr = p_ptr->next
//q_ptr = q_ptr->next
}
return 0;
}

-1

Решение

Term Класс не имеет конструктора по умолчанию.

Node Шаблонный класс имеет только конструктор по умолчанию. Node класс не может быть использован с Term в качестве параметра шаблона; возможно, за исключением особого случая инициализации агрегата.

Так, например:

Node<T> * tmp = new Node<T>();

Это создает новый экземпляр Node шаблон класса, используя его конструктор по умолчанию.

Если параметр класса шаблона является вашим Term это, конечно, попытка построить Term с конструктором по умолчанию. поскольку Term не имеет конструктора по умолчанию, вы получаете ошибку компиляции:

Line 17: error: no matching function for call to 'Term::Term()'

И это именно то, что говорит это сообщение об ошибке. Была предпринята попытка создать экземпляр Term используя конструктор по умолчанию, но у класса его нет.

Вы должны либо определить конструктор по умолчанию для Termили есть Nodeконструктор дубль T в качестве параметра, чтобы скопировать-построить его data член.

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector