I have Added a new item to a FormProject in VC++ .NET.

This is an includerFile (OneFile.h)

On Form3 I have #include "OneFile.h" and in OneFile.h I have only written this:
(Nothing else is written in OneFile.h)

#define Macro1 Time == Value

This macro works fine using it in OneFile.h

My problem here now is if I also want to declare an int like this in OneFile.h:

int Number100 = 0;

I will have a compiler Error that says:

Form3.obj : error LNK2005: "int Number100" (?Number100@@$$Q3HA) already defined in Form1.obj
C:\Documents and Settings\Jennifer\My Documents\Visual Studio 2008\Projects\Form1\Debug\Form1.exe : fatal error LNK1169: one or more multiply defined symbols found

Why is this happening. It doesn´t matter what I will name the int. The compiler says that this "already is defined in Form1.obj" but this is not true as I can change it to whatever and still have this compile Error ?

in OneFile.h you have to declare that variable with the extern keyword extern int Number100; Then in one of the *.cpp files int Number100 = 0;

Thanks. Is it not possible to just declare ints, doubles, bools like you do within a Form in this Onefile.h. Like:

int Number100 = 0;
bool Word100 = false;

Why I am asking again is because I can almost be 100% sure about it should be possible because I did this for about 2 weeks ago for a whole day, so I remember it quite well and now when I do it, it does not work.
I declared ints and doubles and also bools like usual/above.
I might be wrong though. ( To point out, the OneFile.h is emty except of the declarations above)

in OneFile.h you have to declare that variable with the extern keyword extern int Number100; Then in one of the *.cpp files int Number100 = 0;

Thanks. Is it not possible to just declare ints, doubles, bools like you do within a Form in this Onefile.h. Like:

int Number100 = 0;
bool Word100 = false;

No, you can't initialize variables in a .h file. and if you omit the extern keyword you will get multiple declarations errors if the .h file is included in two or more *.cpp files.

Why I am asking again is because I can almost be 100% sure about it should be possible because I did this for about 2 weeks ago for a whole day, so I remember it quite well and now when I do it, it does not work.

Your memory is faulty :)

Yes perheps my memory is faulty :)

I will try to explain the whole scenario what I will do, to get the right picture.
I do have this OneFile.h with this contents only:

#define Value Time == 1800

So, Value does exist in Form3 as a criteria. Now in OneFile.h I have told that
Value is Time == 1800 and this works fine.

What I really want to do in this OneFile.h(which also is my question if it is possible) is to make a bool out of it like:

#define Value result
bool result() {return Time == 1800;}

So result "contains" Time == 1800 in this case.
Is this possible to do in any way ?

Yes perheps my memory is faulty :)

I will try to explain the whole scenario what I will do, to get the right picture.
I do have this OneFile.h with this contents only:

#define Value Time == 1800

So, Value does exist in Form3 as a criteria. Now in OneFile.h I have told that
Value is Time == 1800 and this works fine.

Yes that works because it doesn't declare any variables.

What I really want to do in this OneFile.h(which also is my question if it is possible) is to make a bool out of it like:

#define Value result
bool result() {return Time == 1800;}

So result "contains" Time == 1800 in this case.
Is this possible to do in any way ?

When a function is put in a c++ header file its normally called an inline function. If the function is not part of a class then use the inline keyword so that the compiler doesn't attempt to define it multiple times.

#define Value result
_inline bool result() {return Time == 1800;}

To use that in a c++ program:

#include <iostream>
using namespace std;

int Time = 0;
#define Value result
_inline bool result() {return Time == 1800;}

int main()
{
    bool x = Value();
    cout << x << "\n";
    return 0;
}

That sounds good. If I now write this in the OneFile.h

#define Value result

_inline bool result() {return Time == Input1;}

The compiler says that Time and Input1 is undeclared identifier. These are declared within Form3 though where I have ´Value´ but doesn´t recognices in OneFile.h.
What could be needed to do here.
Thanks...

you need to declare those variables

#define Value result
extern int Time, Input1;
_inline bool result() {return Time == Input1;}

Yes I did try this and if I declare the variables like that, then I will have compile Error that says this. I dont know what this could meen:

Form3.obj : error LNK2020: unresolved token (0A00008E) "int Input1" (?Input1@@$$Q3HA)
Form1.obj : error LNK2020: unresolved token (0A0000B4) "int Input1" (?Input1@@$$Q3HA)
Form3.obj : error LNK2020: unresolved token (0A00008F) "int Time" (?Time@@$$Q3HA)
Form1.obj : error LNK2020: unresolved token (0A0000B5) "int Time" (?Time@@$$Q3HA)
Form1.obj : error LNK2001: unresolved external symbol "int Input1" (?Input1@@$$Q3HA)
Form3.obj : error LNK2001: unresolved external symbol "int Input1" (?Input1@@$$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: 6 unresolved externals

you need to declare those variables

#define Value result
extern int Time, Input1;
_inline bool result() {return Time == Input1;}

Those errors mean that you failed to declare those two integers in the *.cpp file and without the extern keyword. See my post #2 in this thread.

I have created a file, (OneFile.h) to my Windows Form Application. In this file I have written this:

#define Value result   //change result to: Time == Input1 will compile.

extern int Time;
extern double Input1;

_inline bool result() {return Time == Input1;}

So I want result to "contain" Time == Input1. In Form3 in my project Time and Input1 is declared.

Then in Form3.cpp I have declared Time and Input1 like below.
Am I doing correct here, because with result in the macro above it wont compile but If I instead write Time == Input1 there, it do compile and works as expected.
I do want to write result as I have done though.

What could be missing ?

#include "StdAfx.h"
#include "Form3.h"

int Time = 0;
double Input1 = 0;

The compiler Error consist of this:

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)

The compileError complained about Time. I just tried to change Time to time (lowcase)

Now it does compile but these 2 examples does not obtain the same result:
Working approch: (This is written in OneFile.h)

#define Value Time == Input1

Compiling but not achieving the same result as the working one above.
(This is written in OneFile.h

#define Value result

extern int time;
extern double Input1;

_inline bool result() 
{return time == Input1;}

and this in Form3.cpp

#include "StdAfx.h"
#include "Form3.h"

int time = 0;
double Input1 = 0;

Something must be missing I beleive ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.