#define

This is a discussion on #define within the C++ Programming forums, part of the General Programming Boards category; I have created a new Item (File.h) In this (File.h) I have written this and in File2.h I have #include ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    383

    #define

    I have created a new Item (File.h)

    In this (File.h) I have written this and in File2.h I have #include "File.h"
    and this works fine. (Time and Input1 are declared in File2.h)

    #define Criteria1 Time == Input1


    What I instead want to achieve is to make a bool out of: Time == Input1
    I have tried something like this but this is not working.

    This is written in File.h
    Code:
    #define Criteria1 Criteria2()
    
    extern int Time;
    extern double Input1;
    
    _inline bool Criteria2() 
    {
    	return Time == Input1;
    }
    Then I have written this in File2.cpp
    Code:
    #include "StdAfx.h"
    #include "File2.h"
    
    int Time = 0;
    double Input1 = 0;
    However this does not compile. I dont know if I am doing completely wrong here.
    Am I on the right track ?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your problem is not very clear.

    Code:
    #define EQUAL a == b
    
    ...
    bool sth = EQUAL;
    This works fine for me (but isn't something I would consider sane).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It helps A LOT if you tell us what the error is when it "won't compile".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It helps a lot more if you explain in more detail what insane thing you're trying and why.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Time == Input1 is a criteria on Form3.h
    So I am trying to make a bool on File.h.
    So instead of writing on File.h.

    Code:
    #define Criteria1 Time == Input1
    I want to put "Time == Input1" in a bool.
    Why I want to do this is because I will change this often and instead if this is possible using a bool.


    The compile Error that I achieve is this below.
    However if I change "Time" to "time" in all varialble declarations above, it will compile but then when running the program this will not achive the same results as if I will write Time == Input1 directly.

    So what I simply want to do is to write "Time == Input1" inside a bool or function in any way and then only need to write for example.

    #define Criteria1 Criteria2

    Where Criteria2 stands for "Time == Input1" (Time and Input1 are already declared variables in Form3.h)
    Code:
    Form3.obj : error LNK2020: unresolved token (0A00008E) "int Time" (?Time@@$$Q3HA)
    Form1.obj : error LNK2020: unresolved token (0A0000B5) "int Time" (?Time@@$$Q3HA)
    Form1.obj : error LNK2001: unresolved external symbol "int Time" (?Time@@$$Q3HA)
    Form3.obj : error LNK2001: unresolved external symbol "int Time" (?Time@@$$Q3HA)
    C:\Documents and Settings\Jennifer\My Documents\Visual Studio 2008\Projects\Form1\Debug\Form1.exe : fatal error LNK1120: 3 unresolved externals
    Last edited by Coding; 03-05-2008 at 03:51 PM.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    If you want the Criteria2() function to be defined by the constant Criteria1, you'll need backslashes to continue the lines in your #define.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Yes, this I did know was possible and this might be the mainreason I am trying to find another solution. Is it true that it is not possible to use a bool in the way I have described or something simular to it.
    Is backslashes really the only solution.
    Thanks..
    Last edited by Coding; 03-05-2008 at 03:58 PM.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,035
    I'm not entirely clear what you're trying to do.

    There is no easy way to have a bool that is automatically updated whenever two other values change, if that's what you're trying to achieve.

    (Time == Input1) can be replaced by a function call to Criteria2(). As written, Criteria1 is the same as Criteria2, except that you don't put parenthesis after it.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Yes, I will try to use this approach, it might be okay to use. Thanks.
    Code:
    #define Criteria1 Time == Input1

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Coding View Post
    Yes, I will try to use this approach, it might be okay to use. Thanks.
    Code:
    #define Criteria1 Time == Input1
    You need to add () - otherwise your marco is ready for different side-effects
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    polo
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Why?!?
    By p3rry in forum C Programming
    Replies: 3
    Last Post: 01-08-2009, 11:52 AM
  3. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  4. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 08:22 AM
  5. float toolbar!
    By c-- in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2003, 08:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21