 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
johnp Guest
|
Posted: Sat Apr 21, 2007 8:11 am Post subject: How to use "extern" |
|
|
Problems with Extern
Is this problem something to do with an
external declaration becoming a definition ?.
I have a project with several units.
Unit5 declares and defines a function member of a Form -
int __fastcall TForm5::interrogate_dlls(TMouseButton Button, int X , int Y )
{ . . . }
Unit2.cpp redeclared the function as extern to make it visible -
extern interrogate_dlls(TMouseButton, int , int ) ;
Unit2 describes Form2 which has an event handler
which calls the function -
void __fastcall TForm2::get_curr_filter(TObject *Sender)
{
interrogate_dlls(mbLeft, 1 , 1) ; //COMPILER ERROR
}
The compiler error is
"Unit2.cpp E2189 extern variable cannot be initialised"
Why ?
Can anyone direct me to a decent & comprehensive
description of the syntax/use of extern on the web ?
John |
|
| Back to top |
|
 |
Clayton Arends Guest
|
Posted: Sat Apr 21, 2007 8:02 pm Post subject: Re: How to use "extern" |
|
|
| Quote: | Problems with Extern
|
extern is not required for functions and is invalid syntax when used with
classes or methods of classes. extern is only required when declaring a
variable to make the linker aware that it has been defined somewhere else in
code.
To make a method "visible" from a class it needs to be declared in a
"public" section:
// Unit5.h
class TForm5 : public TForm
{
public:
// ... other methods
int __fastcall interrogate_dlls(TMouseButton Button,
int X, int Y);
};
Any other file that includes "Unit5.h" will be able to access the public
methods of TForm5 if it has an object of type TForm5.
| Quote: | void __fastcall TForm2::get_curr_filter(TObject *Sender)
{
interrogate_dlls(mbLeft, 1 , 1) ; //COMPILER ERROR
}
|
You need an object of type TForm5 to invoke the method interrogate_dlls().
If you are using the global variable that C++Builder (or BDS, or Turbo C++)
defines when you create a new form then this will work:
Form5->interrogate_dlls(mbLeft, 1, 1);
If you are not using that global variable then I will need to see more of
your code to help you with the correct way to invoke that method.
| Quote: | Can anyone direct me to a decent & comprehensive
description of the syntax/use of extern on the web ?
|
The help file has a decent description of what extern is used for. I'm not
sure it can get more comprehensive.
If this post doesn't help you with your problem then please post more of
your code and I (or someone else) will try to help you though this.
- Clayton |
|
| Back to top |
|
 |
johnp Guest
|
Posted: Sun Apr 22, 2007 1:49 pm Post subject: Re: How to use "extern" |
|
|
Clayton
Thanx for solving my problem
John |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|