C++ Builder class problem

Started by
5 comments, last by DonceLT 14 years, 6 months ago
I have written simple class with few public variables and public methods. But the problem is that i can't use global program function in my class method. Unit1.h

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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TTimer *Timer1;
        void __fastcall FormMouseMove(TObject *Sender, TShiftState Shift,
          int X, int Y);
        void __fastcall FormCreate(TObject *Sender);
        void __fastcall Timer1Timer(TObject *Sender);
        void __fastcall FormPaint(TObject *Sender);
private:	// User declarations
public:		// User declarations

               
void Valyti(int x1, int y1, int x2, int y2);


        __fastcall TForm1(TComponent* Owner);
};


//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
              
class button {
public :
int pr, g;//y
bool uzejes;

float left;//kiek islindes
int max;//kiek daugiausia islenda

int greitis;//kiek px/piesima

void Pradzia(int pradzia, int galas, int ilg, int speed);
void Progress(int X, int Y);
};


Unit1.cpp(only part of it, because it's quite long)


//---------------------------------------------------------------------------
void TForm1::Valyti(int x1, int y1, int x2, int y2)
{
   TRect kv;
   kv.left = x1;
   kv.right = x2;
   kv.top = y1;
   kv.bottom = y2;

   Form1->Canvas->BrushCopy(kv, fonas->Bitmap, kv, clBlack);
}
//---------------------------------------------------------------------------

void button::Pradzia(int pradzia, int galas, int ilg, int speed)
{
   pr = pradzia;
   g = galas;

   left = 0;
   max = ilg;

   greitis = speed;

   uzejes = 0;
}

void button::Progress(int X, int Y)
{
   X -= 10;
   bool dbr;//ar dbr uzejes
   dbr = ((Y >= pr && Y <= g) && (X <= left));

   if (uzejes)
   {
      if (!dbr)
      {
         //nustojo vaziuoti
         uzejes = 0;


      }
      else
      {
         //toliau vaziuoja
         if (left < max)
         {
           left += (float((max-left))/max*greitis);
           if (left > max)
             left = max;
         }
      }
   }
   else
   {
      if (dbr)
      {
         //pradejo vaziuoti
         uzejes = 1;
      }
      else
      {
         //nera ant jo uzejusio, tikrinti ar vaziuoja atgal
         if (left > 0)
         {
           int pradzioje = left;
           left -= greitis;
           Valyti(left, pr, pradzioje, g);
           if (left < 0)
             left = 0;
         }
      }
   }
}




It works than I call it from not class(i.e. from Timer event OnTimer), but than I try to call it from class method, function is unknown.. Variables, methods ant comments aren't in English, but I think this isn't matter, because you don't need to know what it does, only to see how I use it. I think you can help me :) (If it's better to paste all code, please ask)
Advertisement
First, always use [ source ] tags please.

I can't really tell, but it seems that your function is a part of the class, but when you declare it you don't specify the scope?

Quote:Original post by Crazyfool
First, always use [ source ] tags please.

I can't really tell, but it seems that your function is a part of the class, but when you declare it you don't specify the scope?



My function isn't part of class, its global Form1 function (at least I want to do it).

So can anyone help me?

[Edited by - DonceLT on November 1, 2009 3:34:48 PM]
Which function do you think is global? All the function declarations I see are inside classes.
Quote:Original post by SiCrane
Which function do you think is global? All the function declarations I see are inside classes.


Well, yes, I didn't told corretly, its in Form1 class. I get confused now, how call function, which is in Form1 class, from class button..?
Why I can't call function from Form1 class Form1->Valyti() inside class button method?

[Edited by - DonceLT on November 2, 2009 9:36:59 AM]
To call a member function for the Form1 class, you need an instance of Form1. If you want to call that function from a member function for your Button class, you need to somehow give an instance of Form1 to the button -- possibly by passing it in the constructor of the Button class.

If you want to be able to call the member function in the Form1 class without an instance of Form1, you must make the function static. This means it will not have access to the instance members of Form1, of course.
Thanks, fixed it.

This topic is closed to new replies.

Advertisement