Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3218588
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:32:43+00:00 2026-05-17T15:32:43+00:00

Hey, I’m Trying to make a programa in C++ that generate triangle, square and

  • 0

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);
}
//---------------------------------------------------------------------------
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:32:44+00:00Added an answer on May 17, 2026 at 3:32 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, I'm trying to make an Address book for my site and I was
Hey, today I tried to create a photoshop automation that saves me an area
Hey Forum, So i'm trying to find out how to use the source files
Hey all, i am trying to create a report to show how much is
Hey guys, I just started learning C++ and I have this code that I
Hey all, I'm reviewing the IntelliJ ability related to JPA ER Diagrams (http://www.jetbrains.com/idea/features/jpa_hibernate.html), and
Hey, I have some trouble using the stream_publish method, more exactly with the attachment
Hey everybody, i am deploying my code from a cluster running on ubuntu onto
Hey, Sorry if this is a dumb question. I'm an amateur programmer and new
Hey, I'm developing a GWT app and now facing the CSS part. I read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.