Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
02-09-2003, 11:25 PM
|
#1
|
Member
Registered: Jan 2003
Posts: 167
Rep:
|
question about scope in c++
I'm having trouble understanding what is going on with the whole issue of scope in c++. I am writing a program to calculate the velocity of an aircraft depending on some input. I don't want to have global variables, but whenever I put them all in main(), g++ tells me that they are undeclared when they show in the rest of the functions.
This works:
double x,y,z;
int a();
int b();
int main () {
<code>
a();
b();
}
int a(){
x=1
}
int b(){
y=2
}
###########################
but this doesn't:
int main(){
double x,y,z;
int a();
int b();
a();
b();
}
int a(){
x=1
}
int b(){
y=2
}
What's going on here? Does it have to do with the prototypes? I've tried all kinds of different placements, but can only get the top style to work.
Can somebody help me out? I can post the entire code if you want.
|
|
|
02-10-2003, 01:07 AM
|
#2
|
Member
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637
Rep:
|
Here is one variant. You needed to place your prototypes outside of any code blocks but the local variables should be inside code blocks.
Code:
//prototypes
int MyFunction();
int main()
{
//variables defined in main
int num;
//function call
num = MyFunction();
return 0;
}
//function definition
int MyFunction()
{
return 10;
}
|
|
|
02-10-2003, 01:43 AM
|
#3
|
Member
Registered: Jan 2003
Posts: 167
Original Poster
Rep:
|
Ok, I tried putting the prototypes outside of the main function, but it still doesn't work. I see the method that you suggest, but I really can't use that at this point in the class.... my teacher will actually TAKE OFF points... he's really trying to get us to use his EXACT style for now (which sucks because I'm taking another c++ class at the same time)
So am I correct in assuming that you must actually declare the variables outside of the main function for them to be global? I know you end up getting around this with pointers, and pass by value or whatever, but we're not there yet, and I haven't had time to investigate too much on my own.
I just thought that if you declare a variable in a block, that all functions called from that block can access them, but I guess that's wrong
Thanks again for the suggestion.
|
|
|
02-10-2003, 01:47 AM
|
#4
|
Member
Registered: Jan 2003
Posts: 167
Original Poster
Rep:
|
PS:
Here's what DOESN'T work
Code:
#include<iostream>
#include<cmath>
using namespace std;
const double K = 1.4;
const double ROE = 0.002378;
const double PSI_2_PSF_CONV = 144; /* (12in/ft)*(12in/ft) */
int psi2psf(); /* Converts input to pounds/(square foot) units */
int evaluate(); /* Calculates the equasion */
int outputResults(); /* Output the results */
int main()
{
double feetPerSecond,
milesPerHour,
totalPressure_psi, /* total pressure input...pounds/(square inch) */
totalPressure_psf, /* total pressure in terms of square feet */
staticPressure_psi, /* static pressure input... pounds/(square inch) */
staticPressure_psf; /* static pressure in terms of square feet */
cout << "\nEnter the Total Pressure in psi: ";
cin >> totalPressure_psi;
cout << "\nEnter the Static Pressure in psi: ";
cin >> staticPressure_psi;
staticPressure_psi += .000000000001; /* quality control */
psi2psf();
evaluate();
outputResults();
return 0;
}
int psi2psf ()
{
totalPressure_psf = totalPressure_psi * PSI_2_PSF_CONV;
staticPressure_psf = staticPressure_psi * PSI_2_PSF_CONV;
return 0;
}
int evaluate ()
{ /* local variables... for computing only (see top) */
double val1,
val2,
val3,
velocitySquared; /* code-cleaner... gimme that memory! :) */
val1 = (2*K*staticPressure_psf)/((K-1)*ROE);
val2 = ((totalPressure_psf - staticPressure_psf)/staticPressure_psf) \
+ 1;
val3 = (K-1)/K;
velocitySquared = val1 * ( pow(val2,val3) - 1 );
feetPerSecond = sqrt(velocitySquared);
milesPerHour = (feetPerSecond * pow(60,2))/5280; /* ft/sec -> mi/hr */
return 0;
}
int outputResults() /* extremely watered down */
{
cout.setf(ios::fixed | ios::showpoint); /* Show decimal point */
cout.precision(1); /* Print correct accuracy */
<<"***************OUTPUT**************RESULTS******************\n"
return 0;
}
######################################################
and here's what I get from g++:
Code:
/home/milo/ENG_38/dev [244]> g++ -o lab_2 lab_2.cpp
lab_2.cpp: In function `int psi2psf()':
lab_2.cpp:59: `totalPressure_psf' undeclared (first use this function)
lab_2.cpp:59: (Each undeclared identifier is reported only once
lab_2.cpp:59: for each function it appears in.)
lab_2.cpp:59: `totalPressure_psi' undeclared (first use this function)
lab_2.cpp:60: `staticPressure_psf' undeclared (first use this function)
lab_2.cpp:60: `staticPressure_psi' undeclared (first use this function)
lab_2.cpp: In function `int evaluate()':
lab_2.cpp:79: `feetPerSecond' undeclared (first use this function)
lab_2.cpp:81: `milesPerHour' undeclared (first use this function)
Last edited by PTBmilo; 02-10-2003 at 01:53 AM.
|
|
|
02-10-2003, 02:25 AM
|
#5
|
Member
Registered: Jan 2003
Posts: 167
Original Poster
Rep:
|
OK, I got it. the {BLOCKS} send the variable to it's children, but not the functions. I ended up doing these:
Code:
int evaluate(double& totalPressure_psf, double& staticPressure_psf,
double& feetPerSecond, double& milesPerHour)
|
|
|
02-10-2003, 02:49 AM
|
#6
|
Member
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637
Rep:
|
Quote:
Originally posted by PTBmilo
I just thought that if you declare a variable in a block, that all functions called from that block can access them, but I guess that's wrong
|
You have to pass local varibles as arguments to function calls because they are allocated on different runtime stacks.
Code:
#include<iostream>
int DoubleVar ( int localVar )
{
localVar *= 2;
return localVar;
}
int main()
{
int localVar = 10;
//pass localVar by value, it needs to be returned to update main's copy
localVar = DoubleVar( localVar );
std::cout << localVar << std::endl;
return 0;
}
The argument is copied because it is passed by value, so you have to return it it in an assignment in order to double the size of the localVar in main. The main runtime stack is separate from DoubleVar's runtime stack.
Code:
#include<iostream>
void DoubleVar ( int * localVar )
{
(*localVar) *= 2;
}
int main()
{
int localVar = 10;
DoubleVar( &localVar );
std::cout << localVar << std::endl;
return 0;
}
On the other hand when you pass by reference, you pass a pointer to DobuleVar's runtime stack to the memory location on mains runtime stack. So updates to the value are occuring at memory locations on main's runtime stack.
Actually C++ has a nicer syntax for passing by reference
Code:
#include<iostream>
void DoubleVar ( int & localVar )
{
localVar *= 2;
}
int main()
{
int localVar = 10;
DoubleVar( localVar );
std::cout << localVar << std::endl;
return 0;
}
|
|
|
02-10-2003, 12:48 PM
|
#7
|
Member
Registered: Jul 2002
Location: san francisco
Distribution: freebsd
Posts: 102
Rep:
|
You might want to do a web search on the terms 'lexical scope' and 'dynamic scope' if you would lijke a slightly deeper understanding of this issue. C++ is lexically scoped.
|
|
|
02-10-2003, 05:59 PM
|
#8
|
Member
Registered: Jan 2003
Posts: 167
Original Poster
Rep:
|
Thanks to the both of you..... this is some interesting stuff.
|
|
|
All times are GMT -5. The time now is 05:37 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|