473,796 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can I make #define macros visible in another file?

Hi everyone, I have a simple question. I'm trying to make a macro in
one file so I can use it in main.cpp. The idea is that I the user of
my code has simple to type the macro definition to replace a more
complicated code. Something like the following:

// file.h
#define OBJECTIVE template <class T\
void operator()(T& chrom_,double* fitness_);

// file main.cpp

OBJECTIVE
{
// definition for objective
}

I've been trying to do this but it doesn't work. It seems that the
macros can only be used in the same file? Any suggestions? Thank you
all.

a^2

Feb 11 '07 #1
7 13081
aaragon wrote:
Hi everyone, I have a simple question. I'm trying to make a macro in
one file so I can use it in main.cpp. The idea is that I the user of
my code has simple to type the macro definition to replace a more
complicated code. Something like the following:

// file.h
#define OBJECTIVE template <class T\
void operator()(T& chrom_,double* fitness_);
Delete the semicolon.

However, in general, this is not a good idea.
// file main.cpp

OBJECTIVE
{
// definition for objective
}

I've been trying to do this but it doesn't work. It seems that the
macros can only be used in the same file? Any suggestions? Thank you
all.

did you #include "file.h"
Feb 11 '07 #2
red floyd wrote:
aaragon wrote:
>Hi everyone, I have a simple question. I'm trying to make a macro in
one file so I can use it in main.cpp. The idea is that I the user of
my code has simple to type the macro definition to replace a more
complicated code. Something like the following:

// file.h
#define OBJECTIVE template <class T\
void operator()(T& chrom_,double* fitness_);

Delete the semicolon.

However, in general, this is not a good idea.
>// file main.cpp

OBJECTIVE
{
// definition for objective
}
Let me clarify my "not a good idea". I'm not talking about killing the
semicolon. I'm saying that this sort of usage is in general not a good
idea.

What problem are you trying to solve?
Feb 11 '07 #3
On Feb 11, 12:49 pm, red floyd <no.s...@here.d udewrote:
aaragon wrote:
Hi everyone, I have a simple question. I'm trying to make a macro in
one file so I can use it in main.cpp. The idea is that I the user of
my code has simple to type the macro definition to replace a more
complicated code. Something like the following:
// file.h
#define OBJECTIVE template <class T\
void operator()(T& chrom_,double* fitness_);

Delete the semicolon.

However, in general, this is not a good idea.
why not?
>
// file main.cpp
OBJECTIVE
{
// definition for objective
}
I've been trying to do this but it doesn't work. It seems that the
macros can only be used in the same file? Any suggestions? Thank you
all.

did you #include "file.h"

Feb 11 '07 #4
On Feb 11, 12:53 pm, "aaragon" <alejandro.ara. ..@gmail.comwro te:
On Feb 11, 12:49 pm, red floyd <no.s...@here.d udewrote:
aaragon wrote:
Hi everyone, I have a simple question. I'm trying to make a macro in
one file so I can use it in main.cpp. The idea is that I the user of
my code has simple to type the macro definition to replace a more
complicated code. Something like the following:
// file.h
#define OBJECTIVE template <class T\
void operator()(T& chrom_,double* fitness_);
Delete the semicolon.
However, in general, this is not a good idea.

why not?
// file main.cpp
OBJECTIVE
{
// definition for objective
}
I've been trying to do this but it doesn't work. It seems that the
macros can only be used in the same file? Any suggestions? Thank you
all.
did you #include "file.h"
Well, I'm writing a library and I want it user-friendly for people
who're gonna use it. This library uses a functor that must be written
by the user. Therefore, instead of allowing the user to write
completely the code for the functor, I just wrote the functor and the
declaration of operator in a .h file that will be included by the
user:

// file.h
class Objective
{
public:

template <class T>
void operator()(T& chrom_,double* fitness_);

};

#define OBJECTIVE template <class T\
void Objective::oper ator()(T& chrom_,double* fitness_)
Therefore, the user of the library has to type OBJECTIVE in main.cpp
to write the templated syntax for operator(). The way I use the macro
here is just for string replacement so I thought it was a good idea.
Do you have a better suggestion?

Feb 11 '07 #5
aaragon wrote:
On Feb 11, 12:53 pm, "aaragon" <alejandro.ara. ..@gmail.comwro te:
>>On Feb 11, 12:49 pm, red floyd <no.s...@here.d udewrote:

>>>aaragon wrote:

Hi everyone, I have a simple question. I'm trying to make a macro in
one file so I can use it in main.cpp. The idea is that I the user of
my code has simple to type the macro definition to replace a more
complicat ed code. Something like the following:
>>>>// file.h
#define OBJECTIVE template <class T\
void operator()(T& chrom_,double* fitness_);
>>>Delete the semicolon.
>>>However, in general, this is not a good idea.

why not?

>>>>// file main.cpp
>>>>OBJECTIVE
{
// definition for objective
}
>>>>I've been trying to do this but it doesn't work. It seems that the
macros can only be used in the same file? Any suggestions? Thank you
all.
>>>did you #include "file.h"


Well, I'm writing a library and I want it user-friendly for people
who're gonna use it. This library uses a functor that must be written
by the user. Therefore, instead of allowing the user to write
completely the code for the functor, I just wrote the functor and the
declaration of operator in a .h file that will be included by the
user:

// file.h
class Objective
{
public:

template <class T>
void operator()(T& chrom_,double* fitness_);

};

#define OBJECTIVE template <class T\
void Objective::oper ator()(T& chrom_,double* fitness_)
Therefore, the user of the library has to type OBJECTIVE in main.cpp
to write the templated syntax for operator(). The way I use the macro
here is just for string replacement so I thought it was a good idea.
Do you have a better suggestion?
Macros can of course be defined in one file and used in another.
Standard header files define several macros.

I suspect you are running into another problem. Templates must be fully
defined in header files. You cannot get you user to write a templated
method in a cpp file and expect it to link with your library code.
You're going to have to find some other solution.

John
Feb 11 '07 #6
"aaragon" <al************ **@gmail.comwro te in message
news:11******** **************@ k78g2000cwa.goo glegroups.com.. .
On Feb 11, 12:53 pm, "aaragon" <alejandro.ara. ..@gmail.comwro te:
>On Feb 11, 12:49 pm, red floyd <no.s...@here.d udewrote:
aaragon wrote:
Hi everyone, I have a simple question. I'm trying to make a macro in
one file so I can use it in main.cpp. The idea is that I the user of
my code has simple to type the macro definition to replace a more
complicated code. Something like the following:
// file.h
#define OBJECTIVE template <class T\
void operator()(T& chrom_,double* fitness_);
Delete the semicolon.
However, in general, this is not a good idea.

why not?
Apart from the problem with the templates that John already pointed out,
define macros can be the source of hours of tiresome "debugging" , where
there is actually no bug. The issue is that the preprocessor might modify
your code in a way that you do not intend and this can lead for example to
confusing error messages that have absolutely nothing to do with the actual
problem that you're facing. I just had an example of old code which fixed
the non compliance of for statements of an old compiler with such a define.
Upgrading to a newer version of the compiler where I used "for" in a #pragma
got me this "for" replaced by the define and this resulted in an absolutely
nonsencial error message, which took me quite some time to figure out.

Thus, I'd consider trading transparency to the library user for some more
coding on their part. But I can also understand that you're trying to make
things as nice as possible. Just keep the FAQ in mind
(http://www.parashift.com/c++-faq-lit...s.html#faq-9.5) -
"Macros are bad for your health ;-)"

Cheers
Chris

Feb 12 '07 #7
On Feb 12, 7:25 am, "Chris Theis" <christian.th.. .@nospam.cern.c h>
wrote:
"aaragon" <alejandro.ara. ..@gmail.comwro te in message

news:11******** **************@ k78g2000cwa.goo glegroups.com.. .
On Feb 11, 12:53 pm, "aaragon" <alejandro.ara. ..@gmail.comwro te:
On Feb 11, 12:49 pm, red floyd <no.s...@here.d udewrote:
aaragon wrote:
Hi everyone, I have a simple question. I'm trying to make a macro in
one file so I can use it in main.cpp. The idea is that I the user of
my code has simple to type the macro definition to replace a more
complicated code. Something like the following:
// file.h
#define OBJECTIVE template <class T\
void operator()(T& chrom_,double* fitness_);
Delete the semicolon.
However, in general, this is not a good idea.
why not?

Apart from the problem with the templates that John already pointed out,
define macros can be the source of hours of tiresome "debugging" , where
there is actually no bug. The issue is that the preprocessor might modify
your code in a way that you do not intend and this can lead for example to
confusing error messages that have absolutely nothing to do with the actual
problem that you're facing. I just had an example of old code which fixed
the non compliance of for statements of an old compiler with such a define.
Upgrading to a newer version of the compiler where I used "for" in a #pragma
got me this "for" replaced by the define and this resulted in an absolutely
nonsencial error message, which took me quite some time to figure out.

Thus, I'd consider trading transparency to the library user for some more
coding on their part. But I can also understand that you're trying to make
things as nice as possible. Just keep the FAQ in mind
(http://www.parashift.com/c++-faq-lit...s.html#faq-9.5) -
"Macros are bad for your health ;-)"

Cheers
Chris
I understand your point and now I know that if I run into problems,
the macros I defined are the first place to look at. However, I'm not
using macros as functions but only as pure text replacements. I got my
code working and it seems do the right thing without any problems. I
just want the user to type OBJECTIVE instead of the more complicated
and error-prone

template <class T>
void operator()(T& chrom_,double* fitness_)

Thanks for all your advice.

a^2

Feb 13 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

97
27823
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
14
4464
by: Carl Ribbegaardh | last post by:
What other c++ constructs can I use instead of #define for executing a couple of functions? Example: #define DO_STUFF doThis(); doThat(); I'd guess that I can either use a template function, an inlined function or an inlined static method. //1
0
1645
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an internal interactive debugger that knows the classes within our system and allow us to walk around the objects that exist at the time of the fault. It mostly works fairly well. That catch being that we have to hand implement some of the code...
18
8930
by: Bryan Parkoff | last post by:
"#define" can only be inside the global scope before main() function. "#if" can be tested after "#define" is executed. The problem is that "#define" can't be inside main() function. I do not wish to create multiple functions which they look almost identical in C++ source code. The C++ compiler should be able to compile one Test() function into two almost identical functions before they are translated into the machine language object. ...
1
4210
by: PCB | last post by:
Hi all, Not sure if this is possible, but can I change the controls of a command button on a per record bases in a subform. In my case, I would like to make a command button visible only if certain fields in a table are a certain value. The buttons are adjacent to each record (i.e. - if there are 5 records, there would be 5 buttons; 10 records, 10 buttons; etc.) Here is the subroutine I've written so far: Public Sub IsButtonVisible()
6
1853
by: msigwald | last post by:
The following line of code works, however, since my professor is a real purist of c, I would like to know if this code is valid (is it good code or a piece of crap?): #define DMP_FILE argv This would be use to do something like this: void main(int argc,char *argv) { FILE *p2file=fopen(DMP_FILE,"w"); }
3
3098
by: iler.ml | last post by:
I am writing code that uses two third-party libraries. They both define same macro OP_ENCRYPT, and luckily for me, they both define it to 0. (In one include, it's '#define OP_ENCRYPT 0', in another include it's 0x0). I cannot change contents of those two includes. This generates comipler warning 'macro redefined'. It's easy to suppress the warning with #ifdef. But I want to also cross-check that those two macros have same value. I am...
20
5567
by: subramanian100in | last post by:
Suppose I have #included <stdint.h> #define MAX_LINE_SIZE SIZE_MAX or const size_t maxLineLength = SIZE_MAX ; Between the above #define and const declaration, which should be preferred and why ?
36
2130
by: anon.asdf | last post by:
Hello! Can the proprocessor make conditional decisions. Here's an example of the functionality (not standard C!) #define PUT_BYTE(const_index, val) \ #preprocessor_if (const_index == 0) \ ({ *(77) = (val); }) \ #preprocessor_else_if (const_index == 1) \ ({ *(99) = (val); }) \
0
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.