需要帮助解决一些错误消息

时间:2011-04-27 05:50:03

标签: c++ templates

我已经明白了这一点,我已经看过几个已经试图解决第一个错误的地方,但是我无法将解决方案应用到它,所以我希望这里有所帮助。

(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();         T&amp;运营商&lt;&lt; (const int&amp;);     };

//***************************************************
// 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

3 个答案:

答案 0 :(得分:0)

请注意,但我认为您的复制构造函数应该使用const SortedLinkedListInt而不是const SortedLinkedListInt

此外,你的add方法不遵循你的原型(返回void而不是T)

答案 1 :(得分:0)

也许我想念一些东西,但是:

  1. 第84行,其中类型为“SortedLinkedListInt”?
  2. 为什么复制构造函数的定义与其声明不同?

答案 2 :(得分:0)

(84)

SortedLinkedList<T>::SortedLinkedList(const SortedLinkedListInt, &obj){
那是什么','做那里? 应该是:

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

同时

必须:

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

代码中不是void

声明中的

voidvoid toString();。定义必须与声明匹配。