0

ねえ、三角波、四角波、正弦波を生成するプログラムを C++ で作成しようとしています。周波数、振幅などを入力すると、波の平均が計算されます。そして、ラジオボタンを選択して、どの波を生成するかを選択できます。

これはリアルタイム システムなので、ウェーブがプロットされている場合、別のタイプのウェーブに対応するラジオ ボタンを選択すると、リアルタイムで変化します。

エラーは次のとおりです。

[C++ エラー] FormularioPrincipal.cpp(171): E2297 'this' はメンバー関数内でのみ使用できます

スレッドを作成している行で発生します。誰かが私を助けることができますか?

ありがとう!

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

#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);
}
//---------------------------------------------------------------------------
4

1 に答える 1

2

(エラーが言うように) to sinceではなくDWORD WINAPI main(void *parametro)passでは、メンバー関数ではなく、フリー関数にいます。parametrothisCreateThread

于 2010-09-30T20:36:01.843 に答える