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