Macros and existing function

Hi, I need help with a little problem.
As a source I wrote overloaded func in another source file. Linked to .h and I need to create a macro that runs one of the functions.

Main source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include "another.h"

  long a;

  #ifdef F1
  #define macro() func_in_another(a) // problem - func alredy defined
  #else
  #define macro() func_in_another()  // same problem
  #endif

  int main(array<System::String ^> ^args) {
    ...
    macro();
    ...
  }


Another source
1
2
3
4
5
6
7
  func_in_another(long a) {
    ...
  }

  func_in_another() {
    ...
  }
1
2
3
4
5
6
7
 int main(array<System::String ^> ^args) {
 #ifdef F1
   func_in_another(a);
  #else
  func_in_another();
  #endif
  }


That said, this feels like a problem that shouldn't (or doesn't) exist. What actual problem are you trying to solve?
Last edited on
I need to create a macro

Says who?

1
2
3
4
5
6
7
8
9
10
#include "another.h"

int main() {
  long a;
#ifdef F1
  func_in_another(a);
#else
  func_in_another();
#endif
}
I have 2 functions, for 2 different operating systems and I want them to be called under one name (macro)
1
2
3
4
5
#if MY_OS_DETECTED
void func(long a);
#else
void func();
#endif 


Wrap the definitions of the functions in #if blocks as well.

1
2
3
4
5
6
7
8
9
10
11
#if MY_OS_DETECTED
void func(long a)
{

}
#else
void func()
{

}
#endif 


(Is the site really slow for anyone else?)

Or, make there be no overloading.

1
2
3
4
5
#if A
#define func func_impl_Windows(long a);
#else
#define func func_impl_Unix();
#endif 

OpenGL libraries do things like the above, I think. Though I do not suggest it, because it can make compiler errors must more nasty because you're defining macros.

Or are you saying you want a to be a default argument? Give us more information, it would be better if we knew what the original problem is, and not what you think is an appropriate solution that doesn't work.
Last edited on
That's fine but one is called with a parameter and the other is called without, in the main code I will have to go around and I would rather use one name without parameters to call one or the other with the parameter (param is statically given). And yes, site is slow :-D
A want is not a need.

another.h
1
2
3
4
#ifndef ANOTHER
#define ANOTHER
void func();
#endif 


prog.cpp
1
2
3
4
5
#include "another.h"

int main() {
  func();
}


another_cray.cpp
1
2
3
4
5
6
#include "another.h"

void func()
{
  // code
}


another_irix.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "another.h"

void func(long x)
{
  // code
}

void func() {
  long a;
  func(a);
}


Compile and link only the appropriate another_*.cpp.
For example:
func() -> in win -> func(long, long)
func() -> in linux -> func(char)

and in main:
func() - and here complicator decides .. what os i have and put here(compile) correct func
and in main:
func() - and here complicator decides .. what os i have and put here(compile) correct func

But what would the parameter value be???
42?
What's the point of having parameters if the user never supplies them?

Oh, so you're saying it's filling those values in with some global values?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#define OS_A

#if defined (OS_A)

void func(long a, long b)
{
    
}
void func()
{
    func(42, 43);
}

#elif defined (OS_B)

void func(char ch)
{
    
}
void func()
{
    func('h');
}

#else
    
  #error No OS defined
  
#endif

int main()
{
    func();
}
Last edited on
Topic archived. No new replies allowed.