开发者

Need help solving some error messages

I've gotten up this point and I've looked in couple of places already trying to just solve the first error, but I'm not able to apply the solutions to it, so I'm hoping here will help.

(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 &operator << (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


(84)

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

What is that ',' doing there? Should be:

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

ALSO:

Must be:

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

Not void like in your code.

Or void but void toString(); in declaration either. Definition must match declaration.


Note sure, but I think your copy constructor should take a const SortedLinkedListInt instead of a const SortedLinkedListInt

Moreover, your add method does not follow your prototype (returns void instead of T)


Maybe I miss something, but:

  1. Line 84, where is type "SortedLinkedListInt" defined?
  2. Why is the definition of your copy constructor different from its declaration?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜