W jaki sposób używać metod innych klas w klasie TForm1?
Komponenty zachowują się tak, jakby nie widziały obiektów z 'zewnątrz'. Mój problem opiszę na prostym przykładzie.
Konstruktor klasy TForm1 generuje komponent Form1. W nim wywołuję również 4 zmienne typu int oraz obiekt typu MojaKlasa, której konstruktor nadaje wartości 3 zmiennym.
Problemy pojawiają się gdy próbuję odnieść się do obiektu i zmiennych np z innych komponentów.
Tworzę GroupBox1 z RadioButton1 i RadioButton2, gdzie kliknięcie na jeden z RadioButtonów wywołuje funkcje Suma(int &a, int &b, int &c, int wynik) lub Iloczyn(int &a, int &b, int &c, int wynik), które są metodami w MojaKlasa.
Kompilator wyświetla błędy w liniach, w których te metody są wywoływane i wyświetla
[C++ Error] Unit1.cpp(25): E2451 Undefined symbol 'Obiekt'
..
[C++ Error] Unit1.cpp(25): E2451 Undefined symbol 'c'
Mogę liczyć na pomoc?
Unit1.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "MojaKlasa.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
int a, b, c;
int wynik=0;
MojaKlasa Obiekt(a, b, c);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton1Click(TObject *Sender)
{
Obiekt.Suma(a, b, c);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton2Click(TObject *Sender)
{
Obiekt.Iloczyn(a, b, c);
}
//---------------------------------------------------------------------------
Unit.h
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TGroupBox *GroupBox1;
TRadioButton *RadioButton1;
TLabel *Label1;
TRadioButton *RadioButton2;
TLabel *Label2;
void __fastcall RadioButton1Click(TObject *Sender);
void __fastcall RadioButton2Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include "MojaKlasa.hpp"
MojaKlasa::MojaKlasa(int &a, int &b, int &c)
{
a=1;
b=2;
c=3;
}
MojaKlasa::~MojaKlasa(){}
int MojaKlasa::Suma(int &a, int &b, int &c, int &wynik)
{
wynik = a + b + c;
return wynik;
}
int MojaKlasa::Iloczyn(int &a, int &b, int &c, int &wynik)
{
wynik = a * b * c;
return wynik;
}
//---------------------------------------------------------------------------
#ifndef MOJAKLASA_HPP
#define BMOJAKLASA_HPP
class MojaKlasa
{
public:
MojaKlasa(int &a, int &b, int &c);
virtual ~MojaKlasa();
int Suma(int &a, int &b, int &c, int wynik);
int Iloczyn(int &a, int &b, int &c, int wynik);
private:
int a;
int b;
int c;
};
#endif
Z góry dziękuję