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

发布于 2024-11-03 16:48:41 字数 4955 浏览 1 评论 0原文

我已经明白了这一点,并且已经在几个地方查看了已经尝试解决第一个错误的问题,但我无法将解决方案应用于它,所以我希望这里会有所帮助。

(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&运算符<< (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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风铃鹿 2024-11-10 16:48:41

(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(); 。定义必须与声明匹配。

(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.

月下客 2024-11-10 16:48:41

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

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

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)

旧伤慢歌 2024-11-10 16:48:41

也许我错过了一些东西,但是:

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

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?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文