2012-11-23 25 views
6

मैं एक लिंक की गई सूची टेम्पलेट कक्षा पर काम कर रहा हूं ताकि विभिन्न प्रकार के चर के साथ एक ही काम किया जा सके और अधिकांश मुद्दों को हल करने में कामयाब रहे। जब मैं संकलित करता हूं, मुझे यह भी मिलता है:सी ++ टेम्पलेट "क्लास टाइप" त्रुटियां

g++ -Wall -o template_test template_test.cpp 
In file included from template_test.cpp:1:0: 
LinkedList.h:50:11: error: declaration of ‘class Type’ 
LinkedList.h:7:11: error: shadows template parm ‘class Type’ 
LinkedList.h:51:30: error: invalid use of incomplete type ‘class LinkedList<Type>’ 
LinkedList.h:8:7: error: declaration of ‘class LinkedList<Type>’ 
LinkedList.h:56:11: error: declaration of ‘class Type’ 
LinkedList.h:7:11: error: shadows template parm ‘class Type’ 
LinkedList.h:57:51: error: invalid use of incomplete type ‘class LinkedList<Type>’ 
LinkedList.h:8:7: error: declaration of ‘class LinkedList<Type>’ 
LinkedList.h:92:11: error: declaration of ‘class Type’ 
LinkedList.h:7:11: error: shadows template parm ‘class Type’ 
LinkedList.h:93:31: error: invalid use of incomplete type ‘class LinkedList<Type>’ 
LinkedList.h:8:7: error: declaration of ‘class LinkedList<Type>’ 
LinkedList.h:102:11: error: declaration of ‘class Type’ 
LinkedList.h:7:11: error: shadows template parm ‘class Type’ 
LinkedList.h:103:34: error: invalid use of incomplete type ‘class LinkedList<Type>’ 
LinkedList.h:8:7: error: declaration of ‘class LinkedList<Type>’ 
LinkedList.h:119:11: error: declaration of ‘class Type’ 
LinkedList.h:7:11: error: shadows template parm ‘class Type’ 
LinkedList.h:120:48: error: invalid use of incomplete type ‘class LinkedList<Type>’ 
LinkedList.h:8:7: error: declaration of ‘class LinkedList<Type>’ 
LinkedList.h:158:11: error: declaration of ‘class Type’ 
LinkedList.h:7:11: error: shadows template parm ‘class Type’ 
LinkedList.h:159:37: error: invalid use of incomplete type ‘class LinkedList<Type>’ 
LinkedList.h:8:7: error: declaration of ‘class LinkedList<Type>’ 
LinkedList.h:193:11: error: declaration of ‘class Type’ 
LinkedList.h:7:11: error: shadows template parm ‘class Type’ 
LinkedList.h:194:34: error: invalid use of incomplete type ‘class LinkedList<Type>’ 
LinkedList.h:8:7: error: declaration of ‘class LinkedList<Type>’ 
LinkedList.h:210:11: error: declaration of ‘class Type’ 
LinkedList.h:7:11: error: shadows template parm ‘class Type’ 
LinkedList.h:211:34: error: invalid use of incomplete type ‘class LinkedList<Type>’ 
LinkedList.h:8:7: error: declaration of ‘class LinkedList<Type>’ 
make: *** [template_test] Error 1 

यह मेरे पास हर टेम्पलेट फ़ंक्शन के लिए एक ही चीज़ है। क्या कोई मेरे लिए समझ सकता है कि इन संदेशों का क्या अर्थ है, संकलक इन संदेशों को फेंकने का कारण क्या बना रहा है, और मैं इसे कैसे ठीक कर सकता हूं? मैं फिर से पढ़ रहा हूं, और इस दौरान मेरे कोड को फिर से लिखना संभव है।

#include <string> 
#include <iostream> 
using namespace std; 

template <class Type> 
class LinkedList{ 
public: 

* default constructor and a copy constructor that creates a copy 
* of the given object*/ 
LinkedList(); //default construtor 
LinkedList(const LinkedList& lst); //copy constructor 
~LinkedList();//destructor 

void add(Type data); 

void insertAt(int pos, Type data); 

bool remove(Type data); 

void removeAll(); 

void printList(); 

template <class Type> 
LinkedList<Type>::LinkedList(){ 
head = NULL; 
size = 0; //Don't forget to do this!!! 
} 

template <class Type> 
LinkedList<Type>::LinkedList(const LinkedList& lst){ 
if (lst.head == NULL){ 
    head = NULL; 
    size = 0; 
} 
else{ 
    head = new Node; 
    head->data = lst.head->data; 
    Node *pNewNode = head; 
    Node *pOldNode = lst.head->next; 
    while (pOldNode != NULL){ 
     pNewNode->next = new Node; 
     pNewNode = pNewNode->next; 
     pNewNode->data = pOldNode->data;; 
     pOldNode = pOldNode->next; 
    } 
    pNewNode->next = NULL; 
    size = lst.size; 
} 
} 

template <class Type> 
LinkedList<Type>::~LinkedList(){ 
    removeAll(); 
} 

template <class Type> 
void LinkedList<Type>::add(Type x){ 
Node *p = new Node; //temporary node 
// Assign appropriate values to the new node 
p -> data = x; 
p -> next = head; 
// Make the head point to the new node 
head = p; 
size++; 
} 

template <class Type> 
void LinkedList<Type>::insertAt(int pos, Type x){ 
Node *p; 
Node *newNode; 

// Check that pos is a valid index 
if (pos <= size){ 
    newNode = new Node; //new node 
    newNode->data = x; 

    // Deal with case when item is to be inserted at the head of the list 
    if (pos == 0){ 
     newNode->next = head; 
     head = newNode; 
    }// if(pos == 0) 
    else{ 
     p = head; 
     // Move to position BEFORE insertion point 
     for(int i = 0; i < pos-1; i++){ 
      // Check that p points to a node 
      // Note using exception handling should throw an exception   
      if(p == NULL){ 
       return; 
      } 
      p = p->next; 
     }//for 
     // Insert node 
     newNode->next = p->next; 
     p->next = newNode; 
    }//else (pos != 0) 
    size++; 
}//else (pos >= size) do nothing 
} 

template <class Type> 
bool LinkedList<Type>::remove(Type x){ 
Node *p = head; 
Node *temp; 
if (head == NULL){ 
    return false; 
} 
else if (head->data == x){ 
    head = head->next; 
    delete p; //currently assigned head 
    size--; 
    return true; 
} 
else{ 
    while(p->next != NULL){ 
     // Check next node for target 
     if(p->next->data == x){ 
      temp = p->next; 
      p->next = p->next->next; 
      delete temp; 
      return true; 
     } 
     p = p->next; 
    } 
} 
return false; 
} 

template <class Type> 
void LinkedList<Type>::removeAll(){ 
Node *p = head; 
// Traverse the list deleting nodes 
while (p!= NULL){ 
    head = head->next; // Mustn't "lose" the next node 
    delete p; // De-allocate memory 
    p = head; // Go to next node 
} 
head = NULL; 
size = 0; 
} 

template <class Type> 
void LinkedList<Type>::printList(){ 
Node *p = head; 
cout << "["; //Nice format! 
// Traverse the list 
while (p != NULL){ 
    cout << p -> data; // Print data 
    p = p -> next; // Go to next node 
    if (p != NULL){ 
     cout << ","; // Print a comma unless at the end of the list 
    } 
} 
cout << "]"; // Don't print a newline - it might not be wanted 
} 


private: 
struct Node { 
    Type data; //list data 
    Node *next; //pointer to next item in the list 
}; 

Node *head; //Pointer to the first node in the list 
int size; //Records the number of nodes in the list 
}; 

उत्तर

2

आप सिंटैक्स का उपयोग कर रहे हैं एक क्लास के भीतर एक वर्ग के दायरे से बाहर एक सदस्य समारोह को परिभाषित करने के:
मेरे कोड इस प्रकार है। यह काम नहीं करता है।

यह करना चाहिए:

template<typename X> 
class Foo { 
public: 
    void f(); 
}; // notice end of class scope here 

template<typename X> 
void Foo<X>::f() { 
    // impl 
} 
+0

आह! मैं देखता हूं, मुझे "मांस" डालने से पहले लिंक किए गए सूची वर्ग शीर्षलेख को बंद करना चाहिए। क्या मुझे निजी "भाग" के साथ-साथ "मांस" से पहले वर्ग के दायरे में शामिल करना चाहिए? – user1846359

संबंधित मुद्दे