2016-07-12 3 views
-1

나는 코드를 확장하려고합니다. 두 개의 다항식을 함께 추가 할 수있는 프로그램을 작성해야합니다. 내가 시도하고 컴파일 할 때, 나는이 빌드 메시지를 얻는다.C++ "호출 할 일치 함수가 없음"오류

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; 
} 

답변

0

Term 클래스에는 기본 생성자가 없습니다.

Node 템플릿 클래스에는 기본 생성자 만 있습니다. Node 클래스는 Term과 함께 템플릿 매개 변수로 사용할 수 없습니다. 아마도 집계 초기화의 특별한 경우를 제외하고.

따라서, 예를 들어 :

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

이 기본 생성자를 사용하여 Node 템플릿 클래스의 새로운 인스턴스를 구축합니다.

템플릿 클래스의 매개 변수가 Term 인 경우 기본 생성자를 사용하여 Term을 생성하려고합니다.

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

을 그리고 이것은 정확히이 오류 메시지의 말씀입니다 : Term는 기본 생성자를 가지고 있지 않기 때문에, 당신은 당신의 컴파일 오류가 발생합니다. 기본 생성자를 사용하여 Term의 인스턴스를 만들려고했으나 클래스에없는 생성자가 있습니다.

당신은 Term의 기본 생성자를 정의하거나 Node의 생성자가 data 멤버를 복사 구축, 매개 변수로 T을 가지고 있어야 하나.

+0

아, 알았어. Term 클래스의 public 섹션에 단순히 Term() {}이라는 행을 추가했습니다. 이게 내가하고있는 일에 충분한가? 아니면 이것을 할 수있는 더 적절한 방법이 있습니까? – CabooseMSG

+0

@CabooseMSG, 이제 초기화되지 않은 데이터가있는 'Term'이 생겼습니다. 그게 적절한가요? 목적에 따라 달라집니다 (즉 궁극적으로 결정해야 함). – chris

관련 문제