2011-04-27 110 views
-1

我已經得到了這一點,我已經看過幾個地方已經試圖解決第一個錯誤,但我無法應用它的解決方案,所以我'希望在這裏會有所幫助。需要幫助解決一些錯誤消息

(84): error c4430: missing type specifier - int assumed. Note: C++ does not support default-int 
(84): warning C4346: 'SortedLinkedList<T>::{ctor}' : dependent name is not a type 
(84): error C2143: syntax error : missing ',' before '&' 


(93): error C2244: 'SortedLinkedList<T>::{ctor}' : unable to match function definition to an existing declaration 
(125): error C2244: 'SortedLinkedList<T>::add' : unable to match function definition to an existing declaration 
(42) : see declaration of 'SortedLinkedList<T>::add' 
(150): error C2244: 'SortedLinkedList<T>::toString' : unable to match function definition to an existing declaration 
(48) : see declaration of 'SortedLinkedList<T>::toString' 

#ifndef SORTEDLINKEDLIST_H 
#define SORTEDLINKEDLIST_H 
#include <iostream> 
#include <new>  // Needed for bad_alloc exception 
#include <cstdlib> // Needed for the exit function 
using namespace std; 

//********************** 
// Struct class  * 
//********************** 
template <class T> 
struct Node { 
    int data; 
    Node* next; 
}; 


template <class T> 
class SortedLinkedList{ 
private: 
    T* head; 
    T* node1; 
    T* node2; 
    T* node; 
    T* n; 
    T* ptr; 
    int* size; 
public: 
    //constructor 
    SortedLinkedList(); 

    //copy constructor 
    SortedLinkedList(const SortedLinkedList&); 

    //destructor 
    ~SortedLinkedList(); 

    //destroy function. Calls the destructor. 
    T destroy(); 

    //add function. 
    (42)T add (const int value); 

    // boolean accessor 
    bool exists(int element); 

    // toString accessor 

(43)T toString(); Ť&操作者< <(const int的&); };

//*************************************************** 
// Constructor. Sets the head to null and size to 0 * 
//*************************************************** 

template <class T> 
SortedLinkedList<T>::SortedLinkedList(){ 
    head = NULL; 
    size = 0; 
} 

//*********************************************** 
// Destructor. Creates a temp pointer and moves * 
// the head over till head is NULL    * 
//*********************************************** 

template <class T> 
SortedLinkedList<T>::~SortedLinkedList(){ 

    while (head != NULL) { 
     ptr = head; 
     head = head -> next; 
     delete ptr;  
    } 
    cout << "\nBut oh well...DESTRUCTED!\n" << endl; 

} 

//*********************************************** 
// My Copy Constructor       * 
//*********************************************** 

template <class T> 

(84)

SortedLinkedList<T>::SortedLinkedList(const SortedLinkedListInt, &obj){ 
     size = obj.size; 
     head = obj.head; 
     node = obj.node; 
     node1 = obj.node1; 
     node2 = obj.node2; 
     n = obj.node; 

    cout << "COPIED!" << endl; 

(93)}

//************************************************* 
// And here is where all the fun begins.   * 
// This checks the element and rearranges the list* 
// to its appropriateness       * 
//************************************************* 

template <class T> 
void SortedLinkedList<T>::add(int newElement){ 
    if (head == NULL){ 
     head = new Node; 
     head->next = NULL; 
     head->data = (newElement); 
    } 
    else if(head->data > newElement){ 
     node1 = new Node; 
     node1->data = newElement; 
     node1->next = head; 
     head = node1; 
    } 
    else{ 
     for(node2=head; node2->next!= NULL; node2 = node2->next) 
      if(node2->next->data > newElement) 
       break; 

     node = new Node;  
     node->next = (node2->next); 
     node->data = (newElement); 
     node2->next = (node); 
     ++size; 
    } 

(125)

} 

//*********************************************** 
// Checks to see if inputed number exist in List* 
//*********************************************** 

template <class T> 
bool SortedLinkedList<T>::exists (int element){ 
    for (n = head; n != NULL; n = n -> next) // how to write n.getElement() in c++ 
     if(element == n->data) //analogous to compareTo (java) 
      return true; 
    return false; 
} 

//*********************************************** 
// toString method outputs the whole vector list* 
//*********************************************** 

template <class T> 
void SortedLinkedList<T>::toString(){ 
    for (n = head; n != NULL; n = n->next){ 
     cout << n->data << endl; 
    } 
    cout << "\n"; 

(150)}

#endif 

回答

0

(84)

SortedLinkedList<T>::SortedLinkedList(const SortedLinkedListInt, &obj){ 

那是什麼 '' 做什麼呢? 應該是:

template <class T> 
SortedLinkedList<T>::SortedLinkedList(const SortedLinkedListInt<T>& obj){ 
........... 

ALSO:

必須是:

template <class T> 
T SortedLinkedList<T>::toString(){ 
...... 

void就像在你的代碼。

voidvoid toString();也在聲明中。定義必須匹配聲明。

+0

謝謝!那樣做了。 – Hydride 2011-04-28 04:32:15

0

說明清楚,但我覺得你的拷貝構造函數應該採取一個const SortedLinkedListInt,而不是一個常量SortedLinkedListInt

而且,您的加載方法不會按照你的原型(返回void而不是T)

0

也許我錯過了某些東西,但是:

  1. 第84行,其中是類型「SortedLinkedListInt」的定義?
  2. 爲什麼複製構造函數的定義與聲明不同?