Нужна помощь в решении некоторых сообщений об ошибках - PullRequest
0 голосов
/ 27 апреля 2011

Я поднялся до этой точки и уже смотрел в нескольких местах, уже пытаясь просто решить первую ошибку, но я не могу применить к ней решения, так что я надеюсь, что здесь это поможет.

(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 &);}; </p>

//***************************************************
// 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 голосов
/ 27 апреля 2011

Может быть, я что-то упускаю, но:

  1. Строка 84, где определен тип "SortedLinkedListInt"?
  2. Почему определение вашего конструктора копирования отличается от его объявления?
0 голосов
/ 27 апреля 2011

(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 как в вашем коде.

Или void, но void toString(); в объявлении тоже.Определение должно соответствовать объявлению.

0 голосов
/ 27 апреля 2011

Обратите внимание, конечно, но я думаю, что ваш конструктор копирования должен принимать const SortedLinkedListInt вместо const SortedLinkedListInt

Более того, ваш метод добавления не соответствует вашему прототипу (возвращает void вместо T)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...