Thread: Undeclared identifier major problem

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    11

    Undeclared identifier major problem

    First of all id like to say Hi! im new to this forum and stumbled upon it via google really realllly need some help with this assignment im doing at University, i'm missing something pretty fundamental i think.

    I have two classes, I first run the userdisplay and get values...these values are then calculated in the other class...but i cant get the classes to see the private variables such as Resistance. I have created 2 header files and 2 Cpp files

    Code:
    //Main Function - Responsible for Running program
    //All C++ Projects need a main function
    
    #include <iostream>
    using namespace std;
    
    #include "RLCSeriesCircuit.h"
    #include "Simulation.h"
    
    int main (void)
    {
    	char Restart; //declare variable restart as type char
    
    	do 
    	{
    	system("CLS"); //Clears the Screen (Stage 2 on Activity)
    	Simulation mySimulation; //object
    	mySimulation.Userdisplay(); //executes first function
    	
    
    	cout << "\nDo you want to Restart the Program? (y/n): " ; //loop to restart whole program
    	cin >> Restart;  //stores into char variable restart
    
    	}
    while (Restart== 'y'); //Restarts if user enters in character y
    
    cout << "\nEnd of program\n\n"; //outputs simple statement stating program has ended
    system ("Pause"); //Pauses Program and displays "press any key to continue"
    
    return 0;
    }
    Code:
    //Header File for CLass Simulation
    //Created by Daniel Jackson
    
    #include <iostream>
    using namespace std;
    
    
    class Simulation
    {
    public:
    	void Userdisplay(void);
    	
    
    private:
    	double Resistance;
    	double Inductance;
    	double Capacitance;
    	double VS;
    	double MinFreq;
    	double MaxFreq;
    	double Freqsteps;
    };
    Code:
    //Simulation C++ File
    //Created by Daniel Jackson
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    #include <cmath>
    using std::pow;
    using std::log;
    using std::setprecision;
    #include <string>
    using std::string;
    using std::getline;
    
    #include "Simulation.h" //include Simulation Class Definition
    #include "RLCSeriesCircuit.h" //include RLCSeriesCircuit class defniition
    
    
    
    
    void Simulation::Userdisplay () //1st Function (User inputs)
    {
    	
        cout << "**********************************************************\n"; //user display (start)
    	cout << "* 205SE Assignment                                       *\n";
    	cout << "* Created by Daniel Jackson (SID: 1641552)               *\n";
    	cout << "* This program simulates the behaviour of an             *\n";
    	cout << "* RLC circuit based on user inputs                       *\n";
    	cout << "**********************************************************\n";
    
    	cout << "\nRLC Series circuit Simulation"; //Program Title (Stage 3 of Activity)
    	cout << "\nEnter the values for the circuit components:\n";
    
    
    	cout << "\nPlease enter Resistance value[Ohms] -> "; //Prompt for R (Stage 4 of Activity)
    	cin >> Resistance; //Read in R (Stage 4 of Activity)
    
        cout << "\nPlease enter Inductance value[H] -> "; //Pompt for L (Stage 4 of Activity)
    	cin >> Inductance; //Read in L (Stage 4 of Activity)
     
    	cout << "\nPlease enter Capacitance value[F] -> "; //Prompt for C (Stage 4 of Activity)
    	cin >> Capacitance; //Read in C (Stage 4 of Activity)
    
    	cout << "\nEnter the operating conditions:\n";
    	
    	cout << "Enter supply Voltage [V] -> "; //prompt for supply voltage
    	cin >> VS; //Read in supply Voltage
    
    	cout << "Enter Min Supply frequency [Hz] -> "; //prompt for minimum supply frequency
    	cin >> MinFreq; //Read in Minimum Frequency
    
    	cout << "Enter Max supply frequency [Hz] -> "; //prompt for maximum frequency
    	cin >> MaxFreq; //Read in Maximum Frequency
    
    	cout << "Enter No. of frequency steps required -> "; //prompt for number of freq steps
    	cin >> Freqsteps;
    
    	RLC_Series_Cct Circuit;
        Circuit.getResistance();
    	Circuit.getCapacitance();
    	Circuit.getInductance();
    	Circuit.Incrementloop();
    
    }
    Code:
    //Header file for RLC Series CIrcuit
    //Created by Daniel Jackson
    #include <iostream>
    using namespace std;
    
    
    class RLC_Series_Cct
    {
    public:
    	void Incrementloop();
    	double getResistance();
    	double getInductance();
    	double getCapacitance();
    private:
    	double actualFreq;
    	double Z;
    	double I;
    	double VR;
    	double NF;
    	double XL;
    	double XC;
    	double VOVR;
    	int iteration;
    	int Maxstep;
    		
    };
    Code:
    //RLC Series Circuit C++ File
    //Created by Daniel Jackson
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    #include <cmath>
    using std::pow;
    using std::log;
    using std::setprecision;
    #include <string>
    using std::string;
    using std::getline;
    
    #include "Simulation.h"
    #include "RLCSeriesCircuit.h"
    
    const double pi = 3.1415926535; //Global constant
    
    double RLC_Series_Cct::getResistance()
    {
    	return Resistance;
    }
    
    double RLC_Series_Cct::getCapacitance()
    {
    	return Capacitance;
    }
    
    double RLC_Series_Cct::getInductance()
    {
    	return Inductance;
    }
    
    void RLC_Series_Cct::Incrementloop() //2nd function (calculation with for loop)
    	{
    		
    		cout << right; // one time exectution
    		cout.fill ('*');
    		
    		NF = (((2*pi*sqrt(Inductance*Capacitance()))/1)); //Calculate Natural Frequency 	
    		Maxstep = ((log10(MaxFreq) - log10(MinFreq))/(0.1)); //incerement calculation
    		cout << "Voltage ratio plot at each frequency step:\n\n"; //introduces chart
    			
    				for (iteration= 1; iteration<= Maxstep; iteration++) //for loop begins 
    				{
    				actualFreq = (MinFreq * pow(10.0, (0.1 * iteration))); // plot Voltage ratio
    				XL = (2 * pi * actualFreq * Inductance); //Calculation 
    				XC = (1/ (2 * pi * actualFreq * Capacitance)); //Calculation 
    				Z = (sqrt(Resistance * Resistance + (XL - XC) * (XL - XC))); //Calculation 
    				I = (VS / Z); //Calculation 
    				VR = (I * Resistance); //Calculation 
    				VOVR = (VR / VS); //Calculation 
    				 	
    				cout << setw(VOVR * 100) << actualFreq << setprecision (2) << endl;
    				}
    				
    			
    			cout << "\nNatural frequency of circuit = " << NF << setprecision( 2 ); //outputs Natural Frequency to user
    
    }
    1>c:\data\jacks1d\my documents\visual studio 2008\projects\rlc project 2\rlc project 2\rlcseriescircuit.cpp(42) : error C2065: 'MaxFreq' : undeclared
    identifier
    1>c:\data\jacks1d\my documents\visual studio 2008\projects\rlc project 2\rlc project 2\rlcseriescircuit.cpp(32) : error C2065: 'Inductance' : undeclared identifier
    1>c:\data\jacks1d\my documents\visual studio 2008\projects\rlc project 2\rlc project 2\rlcseriescircuit.cpp(27) : error C2065: 'Capacitance' : undeclared identifier


    Examples of the errors im getting are above...seems no matter what i try i cannot call them variables....
    Last edited by Danieljax88; 03-27-2010 at 12:03 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    In your .cpp file for class "RLC_Series_Cct" your using a variable named "MaxFreq", which was never declared in that file or the header file for this same class. Note that this variable is declared in class "Simulation", and maybe your confusing it with that variable. I haven't read your description or actual code, just telling you why you're getting this error (you then decide which is the best/appropriate solution).

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And even then, you don't actually have "a" variable MaxFreq -- it is a member of the simulation class, and so (1) you have to have an instance of that class hanging around to use it, and (2) since it's private you can't use it outside the class anyway.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    Hi Nadroj thank you for your reply, you are correct and i understand what you are saying but it kind of doesn't answer my main question, ive posted this in a bit of a rush, i'll elaborate a bit more. "MaxFreq" is an example of a variable that the user must enter for me to carry out the calculation....all of the user facing part of the simulation is part of the "simulation" class and all of the interworkings and actual calculations are done in the "RLC_Series_Cct" class. So how is best to retrieve the information stored in the variables of the simulation class and take it into the "RLC_Series_Cct" class so it knows about the values that the user has given? Any further advice would be great as i have got serious brain ache with getting this to work.

    Thanks
    Daniel

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    Quote Originally Posted by nadroj View Post
    In your .cpp file for class "RLC_Series_Cct" your using a variable named "MaxFreq", which was never declared in that file or the header file for this same class. Note that this variable is declared in class "Simulation", and maybe your confusing it with that variable. I haven't read your description or actual code, just telling you why you're getting this error (you then decide which is the best/appropriate solution).
    MaxFreq is useed in the Simulation Cpp file "cin << maxFreq" from within Userdisplay function.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    Have now edited to add a few more of the errors from the "missing" variables" that the "RLC_Series_Cct" class cannot see

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Those are still private workings of the simulation class, which your circuit class doesn't even know exists. If you want those values there, you will have to pass them into the function, in much the same way you pass 10.0 and 0.1 * Iteration into the pow function.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    Yeh i understand that it doesn't know about them as they are private which means exclusive to the simulation class...im not quite qure what you mean about the way they should be passed though? is there a way of using set and get methods to carry it out for me?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I mean change
    Code:
    Circuit.getResistance()
    to
    Code:
    Circuit.getResistance(PUT THE NUMBERS HERE ALREADY)

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    i understand what you mean but i wont have the values as its something the user can enter?

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    If you have a private variable p in one class, A, the only way that any other class, say class B, can access it is either:
    - class A has a public "get" function, which returns this variable, class B then calls this function to get the value.
    - class A calls a function in class B, and passes p as an argument to this function.

    The latter case seems to best fit with what your doing right now (class Simulator calls a function in class Circuit (whatever its called)). So, because of this, it might be a good idea to, as tabstop suggested, pass these values to the function in class Circuit that you're calling.

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    Quote Originally Posted by nadroj View Post
    If you have a private variable p in one class, A, the only way that any other class, say class B, can access it is either:
    - class A has a public "get" function, which returns this variable, class B then calls this function to get the value.
    - class A calls a function in class B, and passes p as an argument to this function.

    The latter case seems to best fit with what your doing right now (class Simulator calls a function in class Circuit (whatever its called)). So, because of this, it might be a good idea to, as tabstop suggested, pass these values to the function in class Circuit that you're calling.
    The latter option is what my lecturer also suggest...that it needs to be passed as an argument, can you maybe write a line as an example of a passed argument?

    Thanks alot guys for your help i feel im close to getting this now!

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Danieljax88 View Post
    i understand what you mean but i wont have the values as its something the user can enter?
    Of course you can. That just means that the numbers are variables.

  14. #14
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by Danieljax88 View Post
    i understand what you mean but i wont have the values as its something the user can enter?
    You're either overcomplicating things, thus confusing yourself, or you don't fully understand some programming foundations and how programs are executed.

    If you are doing something like:
    - get input, store in variable x
    - pass variable x to function f
    - function f does something with x

    then this is fine. If you're doing something like
    - get input, store in variable x
    - pass variable x to function f
    - variable x in the calling function is modified
    - function f does something with x

    then this is of course incorrect.

  15. #15
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    Quote Originally Posted by nadroj View Post
    You're either overcomplicating things, thus confusing yourself, or you don't fully understand some programming foundations and how programs are executed.

    If you are doing something like:
    - get input, store in variable x
    - pass variable x to function f
    - function f does something with x

    then this is fine. If you're doing something like
    - get input, store in variable x
    - pass variable x to function f
    - variable x in the calling function is modified
    - function f does something with x

    then this is of course incorrect.
    Ok so to put this into the context of my program:
    Get input //eg stored as Resistance
    pass variable Resistance to Increment loop by writing
    Code:
    RLC_Series_Cct Circuit;
    circuit.Incrementloop(Resistance)
    will that now mean that when Incrementloop runs it knows the value assigned to the variable?
    Btw i do not wish to modify the value and only wish to use it so i think i am attempting the top (correct version)
    Last edited by Danieljax88; 03-27-2010 at 12:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM