-1

I'm expanding off of code given to us. I have to finish creating a program that can add two polynomials together. When I try and compile though, I get these build messages.

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.

I'm not quite sure what is wrong, this is our first C++ programming assignment. The TA provided most of this code, I added everything in the final while statement. I also added float coef; and `int exp; to the Node structure.

The coding I used in the while statement was how the Professor explained how to run through the polynomials and check. Does anyone have any idea on how to solve my error?

#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;
}
CabooseMSG
  • 51
  • 10

1 Answers1

0

The Term class does not have a default constructor.

The Node template class only has a default constructor. The Node class cannot be used with Term as a template parameter; perhaps excepting the special case of aggregate initialization.

So, for example:

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

This constructs a new instance of the Node template class, using its default constructor.

If the template class's parameter is your Term this will, of course, attempt to construct Term with a default constructor. Since Term does not have a default constructor, you get your compilation error:

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

And this is exactly what this error message says. An attempt was made to construct an instance of Term using its default constructor, but the class does not have one.

You must either define a default constructor for Term, or have Node's constructor take T as a parameter, to copy-construct its data member.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Ahh okay, I see. I added a line in the public section of the Term class that reads simply Term() {}. Is this good enough for what I'm doing? Or is there a more proper way to do this? – CabooseMSG Jul 12 '16 at 01:33
  • @CabooseMSG, Now you have a `Term` with uninitialized data. Is that appropriate? Depends on its purpose (i.e., that's ultimately for you to decide). – chris Jul 12 '16 at 01:38