C++ 中的错误构建器 CreateThread()

发布于 2024-09-25 11:12:11 字数 9440 浏览 7 评论 0原文

嘿,我正在尝试用 C++ 编写一个程序来生成三角波、方波和正弦波。我输入频率、幅度等,它会计算波的平均值。我可以通过选择单选按钮来选择生成什么波。

这是一个实时系统,因此,如果正在绘制一个波,并且如果我选择与另一种类型的波相对应的单选按钮,它将实时变化。

错误是这样的:

[C++ Error] FormularioPrincipal.cpp(171): E2297 'this' can only be used inside a member function

它发生在我创建线程的行上。有人可以帮助我吗?

谢谢!

//---------------------------------------------------------------------------

#include <vcl.h>
#include <io.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <limits>
#include <complex>
#include <math.h>
#pragma hdrstop

#include "FormularioPrincipal.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HANDLE thread1;
HANDLE thread2;
HANDLE thread3;
HANDLE thread4;
HANDLE thread5;
HANDLE Mutex;

int pipe[2];
double freq;
double per;
double freqAngular;
double pi;
double taxaAmostragem;
double tempofinal;
double deslocamento;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
DWORD WINAPI geraSinalSenoide(void *parametro){

    TForm1 *FormularioPrincipal = (TForm1*) parametro;
    int i;
    double valorGerado;
    per = StrToFloat(FormularioPrincipal->Edit2->Text);
    freq = 1/per;
    FormularioPrincipal->Edit3->Text = freq;
    pi = 3.141592654;
    taxaAmostragem = 20;
    tempofinal = 1000;
    freqAngular = (2 * pi)/per;
    double amp = StrToFloat(FormularioPrincipal->Edit1->Text);

            while(true){
                    for (double time=0; time<=freq; time = time + (1 / taxaAmostragem)){

                            WaitForSingleObject(Mutex, INFINITE);
                            ReleaseMutex(Mutex);
                        deslocamento = ( sin(freqAngular * time)  * amp);
                            write(pipe[1], &deslocamento, sizeof(int));
                        FormularioPrincipal->Series1->AddXY(per, deslocamento, "", clRed);
                            per = per + StrToFloat(FormularioPrincipal->Edit2->Text);
                            //FormularioPrincipal->Label7->Caption = time;

                    }
                     Sleep(1000);
                     return 0;
            }


};

DWORD WINAPI geraSinalQuadrado(void *parametro){

    TForm1 *FormularioPrincipal = (TForm1*) parametro;
    int i;
    int x=0;
    float valorGerado;
    double per = StrToFloat(FormularioPrincipal->Edit6->Text);
    freq = 1/per;
    FormularioPrincipal->Edit2->Text = freq;
    while(true){
            for(i=0; i<freq;i++){
            WaitForSingleObject(Mutex, INFINITE);
            ReleaseMutex(Mutex);

                        valorGerado = rand() % (FormularioPrincipal->Edit1->Text);
                        write(pipe[1], &valorGerado, sizeof(int));
                        FormularioPrincipal->Series1->AddXY(per, valorGerado, "", clRed);
                        per = per + StrToFloat(FormularioPrincipal->Edit6->Text);
                        FormularioPrincipal->Series1->AddXY(per, valorGerado, "", clRed);
            }    Sleep(1000);
    }
}

DWORD WINAPI geraSinalTriangular(void *parametro){

     TForm1 *FormularioPrincipal = (TForm1*) parametro;
    int i;
    int x=0;
    float valorGerado;
    double per = StrToFloat(FormularioPrincipal->Edit6->Text);
    freq = 1/per;
    FormularioPrincipal->Edit2->Text = freq;
    while(true){
            for(i=0; i<freq;i++){
            WaitForSingleObject(Mutex, INFINITE);
            ReleaseMutex(Mutex);

                        valorGerado = rand() % (FormularioPrincipal->Edit1->Text);
                        write(pipe[1], &valorGerado, sizeof(int));
                        FormularioPrincipal->Series1->AddXY(per, valorGerado, "", clRed);
                        per = per + StrToFloat(FormularioPrincipal->Edit6->Text);

            }    Sleep(1000);
    }

};

DWORD WINAPI processaNumeros(void *parametros){

   TForm1* FormularioPrincipal = (TForm1*) parametros;
             int dados[10];
             int qtdDadosLidosBuffer=0;
             float media = 0;
             float soma =0;
             float dif= 0;
             while(true){
                       soma  = 0;
                       dif = 0;
                       int i;
                       for(i=0; i<10; i++)//ele vai pegar de 10 em 10 numeros e calcular a media
                       {
                          int qtdBytesLidos;
                          //ler os dados do pipe
                          qtdBytesLidos = read(pipe[0], &dados[i], sizeof(int));
                          if   (qtdBytesLidos == 0)
                             break;
                       }
                       qtdDadosLidosBuffer = i-1;
                         ReleaseMutex(Mutex);
                         for(int i=0; i<qtdDadosLidosBuffer;i++){
                                soma += dados[i];
                                }
                         if(qtdDadosLidosBuffer != 0)
                            soma/=qtdDadosLidosBuffer;//calcula a media


                         for(int i=0; i<qtdDadosLidosBuffer;i++){
                             dif = dados[i] - soma;
                             dif+=dif;
                         }
                         FormularioPrincipal->Edit4->Text = soma;
                         FormularioPrincipal->Edit5->Text = dif;
                         FormularioPrincipal->Edit6->Text = sqrt(dif);
                         Sleep(1000);//tbm espera 1 segundo
             }
};

DWORD WINAPI main(void *parametro){

    Mutex = CreateMutex(NULL, false, NULL);
    TForm1 *FormularioPrincipal = (TForm1*) parametro;
    DWORD prioridade;
    prioridade = THREAD_PRIORITY_NORMAL;

    if((FormularioPrincipal->RadioButton1->Checked == true) && (FormularioPrincipal->RadioButton2->Checked             == false) && (FormularioPrincipal->RadioButton3->Checked == false)){
            DWORD thread1ID;
            thread1 = CreateThread(NULL, 0, geraSinalSenoide, this, CREATE_SUSPENDED, &thread1ID);
            DWORD thread4ID;
            thread4 = CreateThread(NULL, 0, processaNumeros, this, CREATE_SUSPENDED, &thread4ID);

            GetExitCodeThread(thread2, &exitCode);
            TerminateThread(thread2, exitCode);
            GetExitCodeThread(thread3, &exitCode);
            TerminateThread(thread3, exitCode);

            SetThreadPriority(&thread1ID, prioridade);
    }
    else if((FormularioPrincipal->RadioButton1->Checked == false) && (FormularioPrincipal->RadioButton2->Checked == true) && (FormularioPrincipal->RadioButton3->Checked == false)){
            DWORD thread2ID;
            thread2 = CreateThread(NULL, 0, geraSinalTriangular, this, CREATE_SUSPENDED, &thread2ID);
            DWORD thread4ID;
            thread4 = CreateThread(NULL, 0, processaNumeros, this, CREATE_SUSPENDED, &thread4ID);

            GetExitCodeThread(thread1, &exitCode);
            TerminateThread(thread1, exitCode);
            GetExitCodeThread(thread3, &exitCode);
            TerminateThread(thread3, exitCode);

            SetThreadPriority(&thread2ID, prioridade);
    }
    else{
            DWORD thread3ID;
            thread3 = CreateThread(NULL, 0, geraSinalQuadrado, this, CREATE_SUSPENDED, &thread3ID);
            DWORD thread4ID;
            thread4 = CreateThread(NULL, 0, processaNumeros, this, CREATE_SUSPENDED, &thread4ID);

            GetExitCodeThread(thread1, &exitCode);
            TerminateThread(thread1, exitCode);
            GetExitCodeThread(thread2, &exitCode);
            TerminateThread(thread2, exitCode);

            SetThreadPriority(&thread3ID, prioridade);

    }

    if(_pipe(pipe, sizeof(int)*500, O_BINARY) == -1){//cria o pipe
            MessageBox(NULL, "Erro ao criar pipe", "Aviso", 0);
            return;
   }



}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
DWORD prioridade;
prioridade = THREAD_PRIORITY_NORMAL;

DWORD thread5ID;
thread5 = CreateThread(NULL, 0, main, this, CREATE_SUSPENDED, &thread5ID);
SetThreadPriority(&thread5ID, prioridade);
ResumeThread(thread1);
ResumeThread(thread2);
ResumeThread(thread3);
ResumeThread(thread4);
ResumeThread(thread5);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    unsigned long exitCode;
    GetExitCodeThread(thread1, &exitCode);
    TerminateThread(thread1, exitCode);
    GetExitCodeThread(thread2, &exitCode);
    TerminateThread(thread2, exitCode);
    GetExitCodeThread(thread3, &exitCode);
    TerminateThread(thread3, exitCode);
    GetExitCodeThread(thread4, &exitCode);
    TerminateThread(thread4, exitCode);
    GetExitCodeThread(thread5, &exitCode);
    TerminateThread(thread5, exitCode);
}
//---------------------------------------------------------------------------

Hey, I'm Trying to make a programa in C++ that generate triangle, square and sine, waves. I enter the frequence, amplitude, etc, and it calculates the average of the wave. And i cah choose what wave generate by selecting a radio button.

This is a real-time system, so, if a wave is being plotted and if I choose a radio buton correspondent to another type of wave, it shall change in real time.

The error is this:

[C++ Error] FormularioPrincipal.cpp(171): E2297 'this' can only be used within a member function

It happens on the line that I'm creating the thread. Can someone help me?

Thanks!

//---------------------------------------------------------------------------

#include <vcl.h>
#include <io.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <limits>
#include <complex>
#include <math.h>
#pragma hdrstop

#include "FormularioPrincipal.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HANDLE thread1;
HANDLE thread2;
HANDLE thread3;
HANDLE thread4;
HANDLE thread5;
HANDLE Mutex;

int pipe[2];
double freq;
double per;
double freqAngular;
double pi;
double taxaAmostragem;
double tempofinal;
double deslocamento;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
DWORD WINAPI geraSinalSenoide(void *parametro){

    TForm1 *FormularioPrincipal = (TForm1*) parametro;
    int i;
    double valorGerado;
    per = StrToFloat(FormularioPrincipal->Edit2->Text);
    freq = 1/per;
    FormularioPrincipal->Edit3->Text = freq;
    pi = 3.141592654;
    taxaAmostragem = 20;
    tempofinal = 1000;
    freqAngular = (2 * pi)/per;
    double amp = StrToFloat(FormularioPrincipal->Edit1->Text);

            while(true){
                    for (double time=0; time<=freq; time = time + (1 / taxaAmostragem)){

                            WaitForSingleObject(Mutex, INFINITE);
                            ReleaseMutex(Mutex);
                        deslocamento = ( sin(freqAngular * time)  * amp);
                            write(pipe[1], &deslocamento, sizeof(int));
                        FormularioPrincipal->Series1->AddXY(per, deslocamento, "", clRed);
                            per = per + StrToFloat(FormularioPrincipal->Edit2->Text);
                            //FormularioPrincipal->Label7->Caption = time;

                    }
                     Sleep(1000);
                     return 0;
            }


};

DWORD WINAPI geraSinalQuadrado(void *parametro){

    TForm1 *FormularioPrincipal = (TForm1*) parametro;
    int i;
    int x=0;
    float valorGerado;
    double per = StrToFloat(FormularioPrincipal->Edit6->Text);
    freq = 1/per;
    FormularioPrincipal->Edit2->Text = freq;
    while(true){
            for(i=0; i<freq;i++){
            WaitForSingleObject(Mutex, INFINITE);
            ReleaseMutex(Mutex);

                        valorGerado = rand() % (FormularioPrincipal->Edit1->Text);
                        write(pipe[1], &valorGerado, sizeof(int));
                        FormularioPrincipal->Series1->AddXY(per, valorGerado, "", clRed);
                        per = per + StrToFloat(FormularioPrincipal->Edit6->Text);
                        FormularioPrincipal->Series1->AddXY(per, valorGerado, "", clRed);
            }    Sleep(1000);
    }
}

DWORD WINAPI geraSinalTriangular(void *parametro){

     TForm1 *FormularioPrincipal = (TForm1*) parametro;
    int i;
    int x=0;
    float valorGerado;
    double per = StrToFloat(FormularioPrincipal->Edit6->Text);
    freq = 1/per;
    FormularioPrincipal->Edit2->Text = freq;
    while(true){
            for(i=0; i<freq;i++){
            WaitForSingleObject(Mutex, INFINITE);
            ReleaseMutex(Mutex);

                        valorGerado = rand() % (FormularioPrincipal->Edit1->Text);
                        write(pipe[1], &valorGerado, sizeof(int));
                        FormularioPrincipal->Series1->AddXY(per, valorGerado, "", clRed);
                        per = per + StrToFloat(FormularioPrincipal->Edit6->Text);

            }    Sleep(1000);
    }

};

DWORD WINAPI processaNumeros(void *parametros){

   TForm1* FormularioPrincipal = (TForm1*) parametros;
             int dados[10];
             int qtdDadosLidosBuffer=0;
             float media = 0;
             float soma =0;
             float dif= 0;
             while(true){
                       soma  = 0;
                       dif = 0;
                       int i;
                       for(i=0; i<10; i++)//ele vai pegar de 10 em 10 numeros e calcular a media
                       {
                          int qtdBytesLidos;
                          //ler os dados do pipe
                          qtdBytesLidos = read(pipe[0], &dados[i], sizeof(int));
                          if   (qtdBytesLidos == 0)
                             break;
                       }
                       qtdDadosLidosBuffer = i-1;
                         ReleaseMutex(Mutex);
                         for(int i=0; i<qtdDadosLidosBuffer;i++){
                                soma += dados[i];
                                }
                         if(qtdDadosLidosBuffer != 0)
                            soma/=qtdDadosLidosBuffer;//calcula a media


                         for(int i=0; i<qtdDadosLidosBuffer;i++){
                             dif = dados[i] - soma;
                             dif+=dif;
                         }
                         FormularioPrincipal->Edit4->Text = soma;
                         FormularioPrincipal->Edit5->Text = dif;
                         FormularioPrincipal->Edit6->Text = sqrt(dif);
                         Sleep(1000);//tbm espera 1 segundo
             }
};

DWORD WINAPI main(void *parametro){

    Mutex = CreateMutex(NULL, false, NULL);
    TForm1 *FormularioPrincipal = (TForm1*) parametro;
    DWORD prioridade;
    prioridade = THREAD_PRIORITY_NORMAL;

    if((FormularioPrincipal->RadioButton1->Checked == true) && (FormularioPrincipal->RadioButton2->Checked             == false) && (FormularioPrincipal->RadioButton3->Checked == false)){
            DWORD thread1ID;
            thread1 = CreateThread(NULL, 0, geraSinalSenoide, this, CREATE_SUSPENDED, &thread1ID);
            DWORD thread4ID;
            thread4 = CreateThread(NULL, 0, processaNumeros, this, CREATE_SUSPENDED, &thread4ID);

            GetExitCodeThread(thread2, &exitCode);
            TerminateThread(thread2, exitCode);
            GetExitCodeThread(thread3, &exitCode);
            TerminateThread(thread3, exitCode);

            SetThreadPriority(&thread1ID, prioridade);
    }
    else if((FormularioPrincipal->RadioButton1->Checked == false) && (FormularioPrincipal->RadioButton2->Checked == true) && (FormularioPrincipal->RadioButton3->Checked == false)){
            DWORD thread2ID;
            thread2 = CreateThread(NULL, 0, geraSinalTriangular, this, CREATE_SUSPENDED, &thread2ID);
            DWORD thread4ID;
            thread4 = CreateThread(NULL, 0, processaNumeros, this, CREATE_SUSPENDED, &thread4ID);

            GetExitCodeThread(thread1, &exitCode);
            TerminateThread(thread1, exitCode);
            GetExitCodeThread(thread3, &exitCode);
            TerminateThread(thread3, exitCode);

            SetThreadPriority(&thread2ID, prioridade);
    }
    else{
            DWORD thread3ID;
            thread3 = CreateThread(NULL, 0, geraSinalQuadrado, this, CREATE_SUSPENDED, &thread3ID);
            DWORD thread4ID;
            thread4 = CreateThread(NULL, 0, processaNumeros, this, CREATE_SUSPENDED, &thread4ID);

            GetExitCodeThread(thread1, &exitCode);
            TerminateThread(thread1, exitCode);
            GetExitCodeThread(thread2, &exitCode);
            TerminateThread(thread2, exitCode);

            SetThreadPriority(&thread3ID, prioridade);

    }

    if(_pipe(pipe, sizeof(int)*500, O_BINARY) == -1){//cria o pipe
            MessageBox(NULL, "Erro ao criar pipe", "Aviso", 0);
            return;
   }



}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
DWORD prioridade;
prioridade = THREAD_PRIORITY_NORMAL;

DWORD thread5ID;
thread5 = CreateThread(NULL, 0, main, this, CREATE_SUSPENDED, &thread5ID);
SetThreadPriority(&thread5ID, prioridade);
ResumeThread(thread1);
ResumeThread(thread2);
ResumeThread(thread3);
ResumeThread(thread4);
ResumeThread(thread5);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    unsigned long exitCode;
    GetExitCodeThread(thread1, &exitCode);
    TerminateThread(thread1, exitCode);
    GetExitCodeThread(thread2, &exitCode);
    TerminateThread(thread2, exitCode);
    GetExitCodeThread(thread3, &exitCode);
    TerminateThread(thread3, exitCode);
    GetExitCodeThread(thread4, &exitCode);
    TerminateThread(thread4, exitCode);
    GetExitCodeThread(thread5, &exitCode);
    TerminateThread(thread5, exitCode);
}
//---------------------------------------------------------------------------

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

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

发布评论

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

评论(1

念﹏祤嫣 2024-10-02 11:12:11

在 DWORD WINAPI main(void *parametro) 中,将 parametro 而不是 this 传递给 CreateThread (如错误所示) )您处于自由函数中,而不是成员函数中。

In DWORD WINAPI main(void *parametro) pass parametro instead of this to CreateThread since (like the error says) you're in free function and not in a member function.

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