Помогите позвонить перегруженному оператору вставки - PullRequest
0 голосов
/ 28 апреля 2011

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

#include <iostream>
#include "SortedLinkedListInt.h"
#include <sstream>
using namespace std;

//CONSTRUCTOR
SortedLinkedListInt::SortedLinkedListInt(){
    head = NULL;
    size = 0;   
}

//DESTRUCTOR
SortedLinkedListInt::~SortedLinkedListInt(){    
    while (head != NULL) {
        Node* ptr = head;
        head = head -> next;
        delete ptr;     
    }

}
//COPY CONSTRUCTOR
SortedLinkedListInt::SortedLinkedListInt(const SortedLinkedListInt &obj){
    for(Node* n = obj.head; n!=NULL; n=n->next)
        add(n->data);
}

void SortedLinkedListInt::add(int newElement){  
    if (head == NULL){
        head = new Node;
        head->next = NULL;
        head->data = (newElement);
    }
    else if(head->data > newElement){
        Node* node1 = new Node;
        node1->data = newElement;
        node1->next = head;
        head = node1;
    }
    else{
        Node* node2;
        for(node2=head; node2->next!= NULL; node2 = node2->next)
            if(node2->next->data > newElement)
                break;

        Node* node = new Node;  
        node->next = (node2->next);
        node->data = (newElement);
        node2->next = (node);
        ++size;
    }

}

bool SortedLinkedListInt::exists (int element){
    for (Node* 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;
}

void SortedLinkedListInt::toString(){
    for (Node* n = head; n != NULL; n = n->next){
        cout << n->data << endl;
    }
    cout << "\n";

}

void SortedLinkedListInt::operator <<(const int &sub){
    add(sub);
    for (Node* n = head; n != NULL; n = n->next){
        cout << n->data << endl;
    }
    cout << "\n";
}

Функция находится внизу заголовочного файла выше.Ниже приведен main.cpp

#include <iostream>
#include "SortedLinkedListInt.h"
#include <cstdio>
using namespace std;


int main(){

    SortedLinkedListInt *sll = new SortedLinkedListInt();
    //SortedLinkedList <int> *sll = new SortedLinkedList<int>;
    /*SortedLinkedList<int> sll2 = *sll;*/


    sll->add(5);
    sll->add(1);
    sll->add(3);
    sll->add(9);
    sll->add(2);
    sll->add(5);

    cout << 5;
    cout << 3;

    //sll->toString();

    int n = 4;

    printf("%d does%s exist in list.\n", n, sll->exists(n) ? "": " not");


    system("PAUSE");

}

cout << 5 или любой номер, который не будет вызывать перегруженный оператор вставки.Я хотел, чтобы он выполнял ту же функцию, что и sll -> (5).Поэтому вместо использования sll -> (x) все, что будет сделано, это cout << x; </p>

1 Ответ

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

Я не уверен, чего вы пытаетесь достичь, но

cout << 5 </p>

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

...