Jump to content

C/C++ include/preprocessor question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
totonex

totonex

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
So, here's my problem:
I have a file called program.hpp. In this file i have defined a large amount of constants with #define, and i have two function definitions.
In my main file, program.cpp, i have another #define, which i want to depend on my current program, not the header file , program.hpp. Let's call this constant TEMPO ( it's the actual name of the constant).
In program .hpp, my 2 functions have a block of code which looks like this


void function(int arg1, int arg2, int arg 3)

{

float time;

#ifdef TEMPO

    time=TEMPO;

#else

    time=500;

#endif

//rest of code

}

Notice again, TEMPO is defined in the main file, program.cpp.
Now, in the include list of program.cpp, i have also included music.hpp (duh).
But when i call the functions i need in program.cpp, they act as if i have not defined TEMPO, so they put the default value of 500, which i have set in case i forgot to define TEMPO.
Any ideas? Why isn't the function recognising the constant, and if this is how it works, how can i find a way around to solve this issue? thank you very much.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It depends on the order of the statements in program.cpp. Also, why on earth are you using so many #defines?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
totonex

totonex

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
Ok, here's it all:

I made a pseudo-music player in borland c++ 3.1 :-)
The header file, music.hpp, contains the definitions of the hertz value of some notes over a 3 octave range ( more than 36 defines, since C# = D flat ).
The first function is void play(float note, float duration).
//I know, i said in the first post they were ints, but it doesn't matter
Duration takes its value from tempo:
#ifdef TEMPO
duration=tempo
#else
duration=500 //standard value
#endif
//blabla code, sound(note), delay(..), nosound().
//Yes, i am using CONIO.H and DOS.H, included in program.cpp
So, #define TEMPO can very easily be placed in music.hpp, but i want it in program.cpp, because should i want different variants of the song speed-wise, i wouldn't have to modify the header file.

Edited by WingedPanther, 26 October 2009 - 02:46 PM.
add code tags (the # button)


#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
1) without seeing program.cpp (or at least the first few lines) I can't really help you. At a guess, you need to define TEMPO before your include.
2) you still haven't explained WHY you are using #define instead of const's.
3) please use code tags (the # button) when posting code.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
totonex

totonex

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts

#include <dos.h>

#include <conio.h>

#include "music.hpp"

//#define TEMPO 1000 - here's i would like to define it, rather than 

//the header file. But defining it here makes my header-declared functions

//work improperly ( ignoring this definition).

void main()

{

clrscr();

	play(FA,8);

	play(FA,8,1);

	play(FA,16);

//bla,bla

}

That was 1) and 3)
2) The only c/c++ i know is what i learned in high school (basics, only basics !) plus what i learnt from google searches, it did not occur to me to use const ( did not know about its existence; did a google search ), sure, i can rewrite the program using const, it will work, but #define looks tidier, and even if i switch, the question still remains, at least aesthetiqually.
Nevertheless, thank you for the const suggestion.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Try this:
#include <dos.h>
#include <conio.h>
#define TEMPO 1000 - here's i would like to define it, rather than 
#include "music.hpp"
//more here

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
totonex

totonex

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
It works !!! thanks ! ;)

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Good. I still suggest not using #define's, though.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog