.h and .cpp relationships.

If I have a private variable in a header file, then it can only be accessed directly by the corresponding .cpp file, else it needs public methods to access them, yes? Why would I get an error from a .cpp file saying that these variables are unidentified?
.h file is as follows:
#pragma once
#include<iostream>
using namespace::std;
using std::cin;
using std::cout;
using std::cin;
class Quest5
{
public:
Quest5(void);
~Quest5(void);
void Question5();
double potable;
double nonPotable;
};
and the .cpp file is:
#include "Quest5.h"

Quest5::Quest5(void)
{
}

Quest5::~Quest5(void)
{
}
void Question5()
/*5. Write a program to compute a water and sewage bill. The input is the number of gallons consumed.
The bill is computed as follows: water costs .21 dollars per gallon of water consumed sewage service .01 dollars per gallon of water consumed.*/
{

cout<<"Welcome to question 5"<<endl;
cout<<"Please enter the number of gallons of consumed and the number of sewage used."<<endl;
cin>> potable; //here is where it says the error is
cin>> nonPotable; //and here
potable=potable *.21;
nonPotable= nonPotable *.01; //and here
cout<< "The cost of water consumed is $"<< potable */and here*/<<". The cost of the sewage used is $"<< nonPotable <<"." << endl<< "The total cost of this months services is $"<<potable+nonPotable<<"."<<endl;
cin.get();
cin.get();
}
If I have a private variable in a header file, then it can only be accessed directly by the corresponding .cpp file, else it needs public methods to access them, yes?

No. When you #include a *.h file, the preprocessor copies that whole file exactly into place where the #include is. That's it. There is no magic link between a cpp and an h file with corresponding names.
Last edited on
The error is obvious because variables potable and nonPotable are not defined in function Question5.

You are defining a standalone function which knows nothing about the class Quest5. You forgot to specify nested name for the class. Instead of

void Question5()

shall be

void Quest5::Question5()
Last edited on
Topic archived. No new replies allowed.