合并排序 C++ Windows 窗体应用程序

发布于 2024-12-07 09:13:19 字数 9415 浏览 0 评论 0原文

我正在开发一个基于 GUI 的合并排序应用程序。我正在尝试打印递归合并排序移动时的步骤。我在访问 mergesort/print_arr() 中的“richTextBox1”组件时遇到问题,因此我可以将当​​前数组打印到窗口。我收到这些错误,我对此有一点了解。我认为它必须将 merge_sort 作为尝试访问类组件的静态方法。我很困惑,对此有什么建议或解决方法,而不必完全重新开始吗?

以下是错误:

Form1.h(185): 错误 C2227: '->Text' 的左侧必须指向类/结构/联合/泛型类型

Form1.h(187): 错误 C2227: '->Text' 的左侧Text' 必须指向类/结构/联合/泛型类型

Form1.h(189): 错误 C2227: '->Text' 的左侧必须指向class/struct/union/generic type

这些错误来自 print_arr(arr[],size) 方法。

和代码......

#pragma once
#include <iostream>
#include <time.h>
#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>

namespace prog1 {

using namespace std;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace msclr::interop;

/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
    Form1(void)
    {
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //
    }

    static void randomize(int* a, int size){
        srand ( time(NULL) );
        for(unsigned i = 0; i < size; i++){
            a[i] = rand()%45 + 1;
        }
        for(unsigned i = 0; i < size; i++){
            cout << a[i] << " ";
        }
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~Form1()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::Label^  label1;
protected: 
private: System::Windows::Forms::TextBox^  textBox1;
private: System::Windows::Forms::Button^  randButton;
private: System::Windows::Forms::Button^  increaseButton;
private: System::Windows::Forms::Button^  decreaseButton;
private: System::Windows::Forms::Label^  label2;
private: System::Windows::Forms::RichTextBox^  richTextBox1;
private: System::Windows::Forms::Button^  clearButton;

private:
    /// <summary>
    /// Required designer variable.
    /// </summary>
    System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    void InitializeComponent(void)
    {
        this->label1 = (gcnew System::Windows::Forms::Label());
        this->textBox1 = (gcnew System::Windows::Forms::TextBox());
        this->randButton = (gcnew System::Windows::Forms::Button());
        this->increaseButton = (gcnew System::Windows::Forms::Button());
        this->decreaseButton = (gcnew System::Windows::Forms::Button());
        this->label2 = (gcnew System::Windows::Forms::Label());
        this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
        this->clearButton = (gcnew System::Windows::Forms::Button());
        this->SuspendLayout();
        // 
        // label1
        // 
        this->label1->AutoSize = true;
        this->label1->Location = System::Drawing::Point(13, 13);
        this->label1->Name = L"label1";
        this->label1->Size = System::Drawing::Size(65, 13);
        this->label1->TabIndex = 0;
        this->label1->Text = L"Enter a size:";
        // 
        // textBox1
        // 
        this->textBox1->Location = System::Drawing::Point(84, 13);
        this->textBox1->Name = L"textBox1";
        this->textBox1->Size = System::Drawing::Size(27, 20);
        this->textBox1->TabIndex = 1;
        // 
        // randButton
        // 
        this->randButton->Location = System::Drawing::Point(118, 9);
        this->randButton->Name = L"randButton";
        this->randButton->Size = System::Drawing::Size(75, 23);
        this->randButton->TabIndex = 2;
        this->randButton->Text = L"Random";
        this->randButton->UseVisualStyleBackColor = true;
        this->randButton->Click += gcnew System::EventHandler(this, &Form1::randButton_Click);
        // 
        // increaseButton
        // 
        this->increaseButton->Location = System::Drawing::Point(200, 9);
        this->increaseButton->Name = L"increaseButton";
        this->increaseButton->Size = System::Drawing::Size(75, 23);
        this->increaseButton->TabIndex = 3;
        this->increaseButton->Text = L"Increasing";
        this->increaseButton->UseVisualStyleBackColor = true;
        // 
        // decreaseButton
        // 
        this->decreaseButton->Location = System::Drawing::Point(282, 9);
        this->decreaseButton->Name = L"decreaseButton";
        this->decreaseButton->Size = System::Drawing::Size(75, 23);
        this->decreaseButton->TabIndex = 4;
        this->decreaseButton->Text = L"Decreasing";
        this->decreaseButton->UseVisualStyleBackColor = true;
        // 
        // label2
        // 
        this->label2->AutoSize = true;
        this->label2->Location = System::Drawing::Point(363, 14);
        this->label2->Name = L"label2";
        this->label2->Size = System::Drawing::Size(91, 13);
        this->label2->TabIndex = 5;
        this->label2->Text = L"# of comparisons:";
        // 
        // richTextBox1
        // 
        this->richTextBox1->Location = System::Drawing::Point(16, 44);
        this->richTextBox1->Name = L"richTextBox1";
        this->richTextBox1->Size = System::Drawing::Size(473, 238);
        this->richTextBox1->TabIndex = 6;
        this->richTextBox1->Text = L"";
        // 
        // clearButton
        // 
        this->clearButton->Location = System::Drawing::Point(411, 289);
        this->clearButton->Name = L"clearButton";
        this->clearButton->Size = System::Drawing::Size(75, 23);
        this->clearButton->TabIndex = 7;
        this->clearButton->Text = L"Clear";
        this->clearButton->UseVisualStyleBackColor = true;
        // 
        // Form1
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(501, 319);
        this->Controls->Add(this->clearButton);
        this->Controls->Add(this->richTextBox1);
        this->Controls->Add(this->label2);
        this->Controls->Add(this->decreaseButton);
        this->Controls->Add(this->increaseButton);
        this->Controls->Add(this->randButton);
        this->Controls->Add(this->textBox1);
        this->Controls->Add(this->label1);
        this->Name = L"Form1";
        this->Text = L"CS4413 MergeSort";
        this->ResumeLayout(false);
        this->PerformLayout();

    }
#pragma endregion

static void print_arr(int* arr, int size){
        richTextBox1->Text = "[ ";
        for(int i = 0; i < size; i++){
            richTextBox1->Text = arr[i] + ", ";
        }
        richTextBox1->Text = " ]";    
    }

static void merge_arrays(int h, int m, int arr[], int arrA[], int arrB[]){ 
int i = 0,j=0,k=0;
while(i < h && j < m){
    if(arrA[i] < arrB[j]){
        arr[k] = arrA[i];
        i++;
    }else{
        arr[k] = arrB[j];
        j++;
    }
    k++;
}
    if(i > h){
        for(int x = j; x < m; x++){
            arr[k] = arrB[x];
            k++;
        }
    }else{
        for(int x = i; x < h; x++){
            arr[k] = arrA[x];
            k++;
        }
    }
}

static int* merge_sort(int* arr, const int size){    
if ( size == 1 ) 
    return arr;
int h = size/2;
int m = size/2;
int arrayAHsize = h;
int arrayBMsize = size - m;    
//cout << "h: " << h << "arr[h]: " << arr[h] << "m: " << m << " arraryBMsize" <<
//arrayBMsize<< endl;        
int *arrA = (int*)malloc(h);     
int *arrB = (int*)malloc(arrayBMsize);
int* pa;
int* pb;            
for(int i = 0; i < h; i++){        
    arrA[i] = arr[i];
}
for(int i = 0; i < arrayBMsize; i++){        
    arrB[i] = arr[i + h];
}
cout << endl;
print_arr(arrA, size/2);    
cout << "----";
print_arr(arrB, arrayBMsize);
cout << endl;
//l1 = mergesort( l1 )
pa = merge_sort(arrA,h);
//l2 = mergesort( l2 )
pb = merge_sort(arrB, arrayBMsize);
merge_arrays( h, arrayBMsize,arr, arrA, arrB);    
}
private: System::Void randButton_Click(System::Object^  sender, System::EventArgs^  e) {
            String^ s = textBox1->Text;
            string Ssize = marshal_as<std::string>(s);
            const int size = atoi(Ssize.c_str());
            //int a[atoi(Ssize.c_str())];
            int *a = (int*)malloc(size);
            int* pa = a;
            //int* pa;
            randomize(a, size);
            richTextBox1->Text += "Your set: \n";
            for(int i = 0; i < size; i++){
                richTextBox1->Text += a[i] + ", ";
            }
            pa = merge_sort(a,size);



         }
};
}

I am at work on a gui based mergesort application. I am trying to print the steps as the recursive mergesort moves along. I am having trouble with accessing the "richTextBox1" component within my mergesort/print_arr() so I can print the current array out to the window. I am getting these errors which I understand a little bit. I think it has to do merge_sort being a static method trying to access a class component. I am stumped, any suggestions or workarounds for this without having to start completely over?

Here are the errors:

Form1.h(185): error C2227: left of '->Text' must point to class/struct/union/generic type

Form1.h(187): error C2227: left of '->Text' must point to class/struct/union/generic type

Form1.h(189): error C2227: left of '->Text' must point to class/struct/union/generic type

These errors are coming from the print_arr(arr[],size) method.

and the code......

#pragma once
#include <iostream>
#include <time.h>
#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>

namespace prog1 {

using namespace std;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace msclr::interop;

/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
    Form1(void)
    {
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //
    }

    static void randomize(int* a, int size){
        srand ( time(NULL) );
        for(unsigned i = 0; i < size; i++){
            a[i] = rand()%45 + 1;
        }
        for(unsigned i = 0; i < size; i++){
            cout << a[i] << " ";
        }
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~Form1()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::Label^  label1;
protected: 
private: System::Windows::Forms::TextBox^  textBox1;
private: System::Windows::Forms::Button^  randButton;
private: System::Windows::Forms::Button^  increaseButton;
private: System::Windows::Forms::Button^  decreaseButton;
private: System::Windows::Forms::Label^  label2;
private: System::Windows::Forms::RichTextBox^  richTextBox1;
private: System::Windows::Forms::Button^  clearButton;

private:
    /// <summary>
    /// Required designer variable.
    /// </summary>
    System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    void InitializeComponent(void)
    {
        this->label1 = (gcnew System::Windows::Forms::Label());
        this->textBox1 = (gcnew System::Windows::Forms::TextBox());
        this->randButton = (gcnew System::Windows::Forms::Button());
        this->increaseButton = (gcnew System::Windows::Forms::Button());
        this->decreaseButton = (gcnew System::Windows::Forms::Button());
        this->label2 = (gcnew System::Windows::Forms::Label());
        this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
        this->clearButton = (gcnew System::Windows::Forms::Button());
        this->SuspendLayout();
        // 
        // label1
        // 
        this->label1->AutoSize = true;
        this->label1->Location = System::Drawing::Point(13, 13);
        this->label1->Name = L"label1";
        this->label1->Size = System::Drawing::Size(65, 13);
        this->label1->TabIndex = 0;
        this->label1->Text = L"Enter a size:";
        // 
        // textBox1
        // 
        this->textBox1->Location = System::Drawing::Point(84, 13);
        this->textBox1->Name = L"textBox1";
        this->textBox1->Size = System::Drawing::Size(27, 20);
        this->textBox1->TabIndex = 1;
        // 
        // randButton
        // 
        this->randButton->Location = System::Drawing::Point(118, 9);
        this->randButton->Name = L"randButton";
        this->randButton->Size = System::Drawing::Size(75, 23);
        this->randButton->TabIndex = 2;
        this->randButton->Text = L"Random";
        this->randButton->UseVisualStyleBackColor = true;
        this->randButton->Click += gcnew System::EventHandler(this, &Form1::randButton_Click);
        // 
        // increaseButton
        // 
        this->increaseButton->Location = System::Drawing::Point(200, 9);
        this->increaseButton->Name = L"increaseButton";
        this->increaseButton->Size = System::Drawing::Size(75, 23);
        this->increaseButton->TabIndex = 3;
        this->increaseButton->Text = L"Increasing";
        this->increaseButton->UseVisualStyleBackColor = true;
        // 
        // decreaseButton
        // 
        this->decreaseButton->Location = System::Drawing::Point(282, 9);
        this->decreaseButton->Name = L"decreaseButton";
        this->decreaseButton->Size = System::Drawing::Size(75, 23);
        this->decreaseButton->TabIndex = 4;
        this->decreaseButton->Text = L"Decreasing";
        this->decreaseButton->UseVisualStyleBackColor = true;
        // 
        // label2
        // 
        this->label2->AutoSize = true;
        this->label2->Location = System::Drawing::Point(363, 14);
        this->label2->Name = L"label2";
        this->label2->Size = System::Drawing::Size(91, 13);
        this->label2->TabIndex = 5;
        this->label2->Text = L"# of comparisons:";
        // 
        // richTextBox1
        // 
        this->richTextBox1->Location = System::Drawing::Point(16, 44);
        this->richTextBox1->Name = L"richTextBox1";
        this->richTextBox1->Size = System::Drawing::Size(473, 238);
        this->richTextBox1->TabIndex = 6;
        this->richTextBox1->Text = L"";
        // 
        // clearButton
        // 
        this->clearButton->Location = System::Drawing::Point(411, 289);
        this->clearButton->Name = L"clearButton";
        this->clearButton->Size = System::Drawing::Size(75, 23);
        this->clearButton->TabIndex = 7;
        this->clearButton->Text = L"Clear";
        this->clearButton->UseVisualStyleBackColor = true;
        // 
        // Form1
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(501, 319);
        this->Controls->Add(this->clearButton);
        this->Controls->Add(this->richTextBox1);
        this->Controls->Add(this->label2);
        this->Controls->Add(this->decreaseButton);
        this->Controls->Add(this->increaseButton);
        this->Controls->Add(this->randButton);
        this->Controls->Add(this->textBox1);
        this->Controls->Add(this->label1);
        this->Name = L"Form1";
        this->Text = L"CS4413 MergeSort";
        this->ResumeLayout(false);
        this->PerformLayout();

    }
#pragma endregion

static void print_arr(int* arr, int size){
        richTextBox1->Text = "[ ";
        for(int i = 0; i < size; i++){
            richTextBox1->Text = arr[i] + ", ";
        }
        richTextBox1->Text = " ]";    
    }

static void merge_arrays(int h, int m, int arr[], int arrA[], int arrB[]){ 
int i = 0,j=0,k=0;
while(i < h && j < m){
    if(arrA[i] < arrB[j]){
        arr[k] = arrA[i];
        i++;
    }else{
        arr[k] = arrB[j];
        j++;
    }
    k++;
}
    if(i > h){
        for(int x = j; x < m; x++){
            arr[k] = arrB[x];
            k++;
        }
    }else{
        for(int x = i; x < h; x++){
            arr[k] = arrA[x];
            k++;
        }
    }
}

static int* merge_sort(int* arr, const int size){    
if ( size == 1 ) 
    return arr;
int h = size/2;
int m = size/2;
int arrayAHsize = h;
int arrayBMsize = size - m;    
//cout << "h: " << h << "arr[h]: " << arr[h] << "m: " << m << " arraryBMsize" <<
//arrayBMsize<< endl;        
int *arrA = (int*)malloc(h);     
int *arrB = (int*)malloc(arrayBMsize);
int* pa;
int* pb;            
for(int i = 0; i < h; i++){        
    arrA[i] = arr[i];
}
for(int i = 0; i < arrayBMsize; i++){        
    arrB[i] = arr[i + h];
}
cout << endl;
print_arr(arrA, size/2);    
cout << "----";
print_arr(arrB, arrayBMsize);
cout << endl;
//l1 = mergesort( l1 )
pa = merge_sort(arrA,h);
//l2 = mergesort( l2 )
pb = merge_sort(arrB, arrayBMsize);
merge_arrays( h, arrayBMsize,arr, arrA, arrB);    
}
private: System::Void randButton_Click(System::Object^  sender, System::EventArgs^  e) {
            String^ s = textBox1->Text;
            string Ssize = marshal_as<std::string>(s);
            const int size = atoi(Ssize.c_str());
            //int a[atoi(Ssize.c_str())];
            int *a = (int*)malloc(size);
            int* pa = a;
            //int* pa;
            randomize(a, size);
            richTextBox1->Text += "Your set: \n";
            for(int i = 0; i < size; i++){
                richTextBox1->Text += a[i] + ", ";
            }
            pa = merge_sort(a,size);



         }
};
}

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

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

发布评论

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

评论(1

清泪尽 2024-12-14 09:13:19
static void print_arr(int* arr, int size){
    richTextBox1->Text = "[ ";
    for(int i = 0; i < size; i++){
        richTextBox1->Text = arr[i] + ", ";
    }
    richTextBox1->Text = " ]";    
}

错误是因为 richTextBox1 是类的数据成员,并且静态函数无法访问类数据成员。数据成员用于类的实例,因此没有实例的静态函数将不知道要引用哪些实例数据成员。

使这个方法成为非静态的,你就会没事的。

static void print_arr(int* arr, int size){
    richTextBox1->Text = "[ ";
    for(int i = 0; i < size; i++){
        richTextBox1->Text = arr[i] + ", ";
    }
    richTextBox1->Text = " ]";    
}

Is wrong because richTextBox1 is a data member of the class, and a static function cannot access a class data member. Data members are for instances of the class, and thus a static function, not having an instance, would not know which instances data members to refer to.

Make this method non-static and you'll be fine.

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