Setting up a class

M

Michael Bell

Using Borland Builder 5 I am trying to get a very simple first working
use of a class to work. Following the example of the OU course MT262
as nearly as I can, I have Created files named Class First,
ClassSecond, and ClassThird. And it's still wrong!
It is a windows application, but I don't think that's relevant.
ClassFirst.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
USERES("ClassFirst.res");
USEFORM("ClassFirstU.cpp", Form1);
USEFORM("ClassSecond.cpp", Frame1); /* TFrame: File Type */
USEUNIT("ClassThird.cpp");
USEUNIT("ClassSecond.cpp");
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
}
//-------------------------------------

ClassFirstU.h

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

#ifndef ClassFirstUH
#define ClassFirstUH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "ClassSecond.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TEdit *Box;
TEdit *Operator;
TEdit *Show1;
TEdit *Input1Box;
TLabel *Label1;
TLabel *Label2;
TLabel *Label3;
TEdit *Show2;
TEdit *ShowProduct;
TLabel *Label4;
TLabel *Label5;
TLabel *Label6;
TLabel *Label7;
TButton *ShowButton;
TButton *EnterInput1Button;
TButton *EnterInput2Button1;
TButton *MultiplyButton;
void __fastcall EnterInput1ButtonClick(TObject *Sender);
private: // User declarations
int Input1;
int Input2;
int Product;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

ClassFirstU.cpp

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

#include <vcl.h>
#pragma hdrstop

#include "ClassFirstU.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::EnterInput1ButtonClick(TObject *Sender)
{
Input1 = Input1Box ->Text.ToInt();
Show1->Text = Input1;
}

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

ClassSecond.h

#define ClassSecondH
#include "ClassThird.h"
//---------------------------------------------------------------------------
class NumberBoardClass
{
protected:
NumberBoardType Board();
NumberBoardType ShowOne();
NumberBoardType Multiply(void);
public:
NumberBoardType Show();
};
#endif

ClassSecond.cpp

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

#include <vcl.h>
#pragma hdrstop

#include "ClassSecond.h"

//---------------------------------------------------------------------------
NumberBoardType NumberBoardClass::Multiply(void)
{
int LocalNumber;
LocalNumber = NumberBoardType.Input1 * NumberBoardClass.Input2;
NumberBoardType.Product = LocalNumber;
return Board;
}
#pragma package(smart_init)

ClassThird.h

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

#ifndef ClassThirdH
#define ClassThirdH
//---------------------------------------------------------------------------
struct NumberBoardType
{
int Input1;
int Input2;
int Product;
};
#endif
ŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒ..
And the error messages I get are :-
[C++ Error] ClassSecond.cpp(12): E2108 Improper use of typedef
'NumberBoardType'

[C++ Error] ClassSecond.cpp(12): E2108 Improper use of typedef
'NumberBoardClass'

[C++ Error] ClassSecond.cpp(13): E2108 Improper use of typedef
'NumberBoardType'

[C++ Error] ClassSecond.cpp(14): E2235 Member function must be called
or its address taken

I really don't know what's wrong!

Michael Bell



--
 
V

Victor Bazarov

Michael said:
Using Borland Builder 5 I am trying to get a very simple first working
use of a class to work. Following the example of the OU course MT262
as nearly as I can, I have Created files named Class First,
ClassSecond, and ClassThird. And it's still wrong!
It is a windows application, but I don't think that's relevant.
ClassFirst.cpp
//---------------------------------------------------------------------------
[..lots of irrelevant cruft removed..]
#endif
OOOOOOOOOOOOOOO..
And the error messages I get are :-
[C++ Error] ClassSecond.cpp(12): E2108 Improper use of typedef
'NumberBoardType'

Apparently you defined 'NumberBoardType' as a typedef (to what? you
didn't show), and then are trying to use the same name to define
a class or something.
[C++ Error] ClassSecond.cpp(12): E2108 Improper use of typedef
'NumberBoardClass'

Same here.
[C++ Error] ClassSecond.cpp(13): E2108 Improper use of typedef
'NumberBoardType'

Same here.
[C++ Error] ClassSecond.cpp(14): E2235 Member function must be called
or its address taken

Most likely you use wrong syntax (like 'object.func', without the
following parens, or 'type::member' without the preceding '&'), but
there is no way to be certain because you didn't post the code that
gives you the error, you posted some other code, which is full of
non-standard constructs which prevents us from trying to compile it.
I really don't know what's wrong!

Whole lot of things, probably. No way to know for sure. See the
FAQ, section 5 ("How to Post").

V
 
G

Guest

Using Borland Builder 5 I am trying to get a very simple first working
use of a class to work. Following the example of the OU course MT262
as nearly as I can, I have Created files named Class First,
ClassSecond, and ClassThird. And it's still wrong!

Your first problem is that the "very simple first working use of a
class" is very complex (at least for being simple use of classes). Your
second problem is that whatever material MT262 uses it is not very
portable and contains much stuff that is specific to that material
(Borland specific perhaps?). If I were you and had the choice I would
pick up a gooc C++ book and use that instead.
It is a windows application, but I don't think that's relevant.

That is very relevant, since third part libraries are off-topic in this
group and will limit the amount of help you can expect.

NumberBoardType NumberBoardClass::Multiply(void)
{
int LocalNumber;
LocalNumber = NumberBoardType.Input1 * NumberBoardClass.Input2;

Here you use NumberBoardType as if it was an object, but it is a type
(as defined below). You need to create an object of type NumberBoardType
and perform the operations on that.
 
M

Michael Bell

In message <[email protected]>
[snip]

Here you use NumberBoardType as if it was an object, but it is a type
(as defined below). You need to create an object of type NumberBoardType
and perform the operations on that.

So, I ought to declare NumberBoardType as an object. How do I do that?

Michael Bell



--
 
V

Victor Bazarov

Michael said:
In message <[email protected]>
[snip]

Here you use NumberBoardType as if it was an object, but it is a type
(as defined below). You need to create an object of type
NumberBoardType and perform the operations on that.

So, I ought to declare NumberBoardType as an object. How do I do that?

class SomeClass {};
...
SomeClass an_object;

What is it you're trying to accomplish? Maybe you need to define a data
member in your 'NumberBoardClass'?

Also, consider that 'NumberBoardType' and 'NumberBoardClass' are WAAAAY
too similar for a casual reader to discern any difference between them.
Your naming can surely be improved.

V
 
K

Keith Willis

In message <[email protected]>


So, I ought to declare NumberBoardType as an object. How do I do that?

No. Read what is there. The code above decares that there is a
struct (or class - nearly the same thing) called NumberBoardType.
That's fine. Now you need to create an _object_ of that type -
perhaps:

struct NumberBoardType MyNBT;

Now you can use that _object_:

MyNBT.Input1 = valueA;
MyNBT.Input2 = valueB;
etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top