-1

テキスト ファイルから単純なリンク リストに整数を読み込みます。次に、整数のリストをバブルソートし、別のファイルに読み込みます。現在、メインを読み込んでいますが、抽出演算子をオーバーロードして読み込もうとしていますが、その方法がわかりません。私のBubblesort機能も多くの問題を引き起こしています。関数をオーバーロードできず、ノード識別子が宣言されていないことがわかります。どんな助けでも大歓迎です

メインファイル

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include "bubble.h"

using namespace std;
struct nodeType
{
    int info;
    nodeType* link;
};

node *head_ptr = NULL;
void Display();
void list_clear(nodeType*& head_ptr);
void list_copy(const nodeType* source_ptr, nodeType*&head_ptr, nodeType*&tail_ptr);
Bubblesort();


int main()
{
    ifstream datld;
    ofstream outld;

    Bubble D3;

    datld.open ("infile2.txt");
    if (!datld)
    {
        cout << "failure to open data.txt" << endl;
        system ("pause");
        return 1;
    }

    datld >> D3;

    while(datld)
    {
        cout << D3<< endl;
        datld >> D3;
    }

    system("pause");
    return 0;

    Bubblesort();
}


void Bubblesort()
{
    node* curr = head_ptr;
    int count = 0;
    while(curr!=NULL)
    {
        count++;
        curr = curr->NEXT;
    }
    for(int i = count ; i > 1 ; i-- )
    {
        node *temp, *swap1;
        swap1 = HEAD;
        for(int j = 0 ; j < count-1 ; j++ )
        {
            if(swap1->DATA > swap1->NEXT->DATA)
            {
                node *swap2 = swap1->NEXT;
                swap1->NEXT = swap2->NEXT;
                swap2->NEXT = swap1;
                if(swap1 == HEAD)
                {
                    HEAD = swap2;
                    swap1 = swap2;
                }
                else
                {
                    swap1 = swap2;
                    temp->NEXT = swap2;
                }
            }
            temp = swap1;
            swap1 = swap1->NEXT;
        }
    }
}


void list_clear(nodeType*& head_ptr)
//Library facilities used:cstdlib
{
    nodeType * removeptr;
    while(head_ptr!=NULL)
    {
        removeptr=head_ptr;
        head_ptr=head_ptr->link;
        delete removeptr;
    }
}

void list_copy(const nodeType* source_ptr, nodeType*&head_ptr, nodeType*&tail_ptr)
{
    nodeType* temp;// to allocate new nodes
    head_ptr=NULL;
    tail_ptr=NULL;

    if(source_ptr==NULL)
        return;

    head_ptr=new nodeType;
    head_ptr->link=NULL;
    head_ptr->info=source_ptr->info;
    tail_ptr=head_ptr;
    source_ptr=source_ptr->link;

    while(source_ptr!=NULL)
    {
        temp = new nodeType;
        temp->link=NULL;
        temp->info =source_ptr-> info;
        tail_ptr->link=temp;
        tail_ptr = tail_ptr->link;
        source_ptr = source_ptr->link;
    }
}

ヘッダーファイル

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;

class Bubble
{
private:
    int manynodes;
public:
    Bubble() { }

    void Bubblesort();

    friend ostream &operator<<( ostream &output, const Bubble &D )
    {
        output << D.manynodes;
        return output;
    }

    friend istream &operator>>( istream  &input, Bubble &D )
    {
        input >> D.manynodes;
        return input;
    }
};
4

1 に答える 1

0
  • 抽出演算子は問題ないようです。それが本当に必要なことをするかどうかはわかりませんが、それは別の問題です。
  • 関数を 2 回宣言していますBubblesort()。最初はヘッダー ファイルで としてvoid Bubblesort()、次にメイン ファイルで単にBubblesort()(これにより、少なくとも を意味すると見なされるという警告が表示されますint Bubblesort())。戻り値だけで関数をオーバーロードできないため、エラーが発生します。
  • 確かに、nodeいくつかの場所で呼び出された型を使用していますが、宣言も定義もしていません。
于 2012-11-28T16:24:47.060 に答える