Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

project wide variables in VS2008

Featured Replies

Posted

hi guys,

Here`s a little question that has been bugging me for quiet some time...: How can i declare a project wide variable?

Say, main.cpp holds the function int main(){...}.

In a second file, second.cpp, i have a second function: void second(){...}.

How can these two functions share a string/int/long/... variable?

thanx,

deep :)

p.s. to makes this clear, this is in no way related to windows environment variables (%tmp%, %%appdata%,...). That`s mostly what i got when using google... :S

Edited by deepzero

main.cpp: int test;

second.cpp: extern int test;

  • Author

that seems to be it.

Funny, none of my books ever mentioned it.

thanks a lot, k11! :)

I wanted to use a common header file which contained declarations of variables but including it in multiple .cpp files yielded duplicate declaration errors, is there a '#pragma' or 'IFDEF' which makes it so that the variables are declared only the once but the function prototypes and defines can still be used from the same file?

HR,

Ghandi

you could try

/* function prototypes */

#ifndef MYHEADER

#define MYHEADER

/* variables */

#endif

Funny, none of my books ever mentioned it.

Probably because you don't need "extern" if you use the OO features of C++. With classes you don't have such problems...

Thanks k11. I've tried that before so i must have done something else wrong which caused the compile errors, it seems that MSVC was compiling each .cpp file as a separate module and each module contained these symbols hence the duplicate errors.

HR,

Ghandi

Edited by ghandi

Ghandi's method of using the extern is good for related project scope variables.

You can also setup a class in a *.h file

and use:

#pragma once

#include "myclass.h" in every *.h file in your project

remembering to use the #pragma directive where you reference more than once.

Then use something like the example below from your class where you need it:

CmyclassData *pData = new CmyclassData(myclassInfo, myclassmoreinfo);

CZ

Edited by CondZero

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.