• 0

[C++] Global Variable and procedures


Question

Hi,

I'm new to C++ programming and currently using procedures in my project. What i need help with is declaring a variable that can be used by the main cpp files and other cpp files. Procedures are set up properly and using threads to run them but i can never get the procedures to use the variable i declared in the main .cpp files.

Anyone know how?

Thanks

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

A global variable is one declared outside of any class or function, at the root level of the file if you want. In order to use the variable in other files, you need to #include the one where the global variable was declared. For instance :

main.cpp :

#include <iostream> // etc.
const float pi = 3.14159265;

int main() {
	//...
}

myothercppfile.cpp

#include main.cpp

void someMethod() {
	cout << pi; // should work !
}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.