Learning how to use pointers in c++ running into Error 'variable not declared in scope' : <p>As the title says, I am learning how to use pointers in C++. I have been tasked with writing a program that gets data about countries from a text file, loads them into an array of class objects, then lets the user view the data, remove an entry or quit.</p>

<p>I'm running into a problem with class accessor functions. I am getting the error 'variable not declared in scope.' I get what this error is trying to tell me, but I can't figure out how to get the accessors to function properly. </p>

<p>The requirements for the assignment are to use char-pointers to store strings, so I think that's adding an extra level of complexity. I want the accessor function to just return a single variable, e.g. country name, capital and surface area.</p>

<p>I am using CodeBlocks and running on Ubunutu. I have the compiler set to c++11.</p>

<p>Anyways, here is the code:</p>

<pre><code>//This is the main.cpp file

#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;cstdlib&gt;

#include "Country.h"


const int FILE_PATH_SZ = 512;
Country** g_countryArray;
int g_arrsz = 0;

using namespace std;

void openFile(ifstream&amp; inFile, string pathName);
void getArrSize(ifstream&amp; inFile, string pathName, string&amp; countriesData);
void fillCountryArr(ifstream&amp; inFile, string pathName, string&amp; countryName, string&amp; capitalName, string&amp; tempSurfaceArea);

void printCountryData(Country** g_countryArray, int g_arrsz);


int main() {

    char menuChoice, startingLetter;
    string pathName, countriesData, countryName, capitalName, tempSurfaceArea;
    ifstream inFile;


    do {
        cout &lt;&lt; "Choose one of the following:" &lt;&lt; endl &lt;&lt; endl;
        cout &lt;&lt; "Menu options" &lt;&lt; endl &lt;&lt; endl;
        cout &lt;&lt; "a) Read a text file:" &lt;&lt; endl;
        cout &lt;&lt; "b) Remove countries starting with given letter" &lt;&lt; endl;
        cout &lt;&lt; "c) Print all data to console" &lt;&lt; endl;
        cout &lt;&lt; "d) Quit" &lt;&lt; endl;
        cin &gt;&gt; menuChoice;

        switch (menuChoice)
        {
            case 'a':
            {

                cout &lt;&lt; "Please enter the full path name of the file you wish to open: " &lt;&lt; endl;
                cin &gt;&gt; pathName;

                openFile(inFile, pathName);
                getArrSize(inFile, pathName, countriesData);
                fillCountryArr(inFile, pathName, countryName, capitalName, tempSurfaceArea);

            }

            case 'b':
            {
                cout &lt;&lt; "Enter the starting letter: " &lt;&lt; endl;
                cin &gt;&gt; startingLetter;
                toupper(startingLetter);
            }
            case 'c':
            {
                printCountryData(g_countryArray, g_arrsz);
            }
        }
    }while (menuChoice != 'd');

    return 0;
}

void openFile(ifstream&amp; inFile, string pathName)
{
    inFile.open(pathName.c_str());

    if (!inFile)
        {
            cout &lt;&lt; "Cannot open file." &lt;&lt; endl;
        }

    inFile.close();
    inFile.clear(std::ios_base::goodbit);
}

void getArrSize(ifstream&amp; inFile, string pathName, string&amp; countriesData)
{

    inFile.open(pathName.c_str());

    while (getline(inFile, countriesData))
        {
            ++g_arrsz;
        }

    g_countryArray = new Country* [g_arrsz]; //declares g_countryArray to be an array of pointers of size [g_arrsz]. The array holds pointers to Country class objects
}

void fillCountryArr(ifstream&amp; inFile, string pathName, string&amp; countryName, string&amp; capitalName, string&amp; tempSurfaceArea)
{
    long int surfaceArea;

    //closes and reopens the file cleanly
    inFile.close();
    inFile.clear(std::ios_base::goodbit);
    inFile.open(pathName.c_str());

    for (int i = 0; i &lt; g_arrsz; i++)
    {
        getline(inFile, countryName, ','); //gets the name of the country from the input file
        getline(inFile, capitalName, ','); //gets the name of the capital of the country from the input file
        getline(inFile, tempSurfaceArea); //gets the surface area of the country in the form of a string
        surfaceArea = stol(tempSurfaceArea); //converts the string version of surface area to an integer
        g_countryArray[i] = new Country(countryName.c_str(), capitalName.c_str(), surfaceArea); //creates new Country class and stores address in the i'th element of g_countryArray
    }                                                                                           //passes the name of the country and capital of the country in to the constructor as
                                                                                                //c-strings and passes surfaceArea as an int
}

void printCountryData(Country** g_countryArray, int g_arrsz)
{
    for (int i = 0; i &lt; g_arrsz; ++i)
    {
        cout &lt;&lt; g_countryArray[i]-&gt;GetCountryName() &lt;&lt; ", ";
        cout &lt;&lt; g_countryArray[i]-&gt;GetCapital() &lt;&lt; ", ";
        cout &lt;&lt; g_countryArray[i]-&gt;GetSurfaceArea() &lt;&lt; endl;
    }
}
</code></pre>

<p>The function just above here printCountryData is where I want to pass the array of class objects and array size variables, then call the accessor functions.</p>

<pre><code>//here is the Country.h file
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;cstring&gt;

using namespace std;



class Country
{
private:
    char* name_;
    char* capital_;
    long surfaceArea_;
public:
    Country (const char* country, const char* capital, long surfaceArea);
    ~Country ();
    char* GetCountryName();
    char* GetCapital();
    long GetSurfaceArea();
};

Country::Country(const char* country, const char* capital, long surfaceArea)
{
    int countryLen, capitalLen; //variables to store length of c-strings country and capital for dynamically allocating arrays of the right length
    countryLen = strlen(country); //gets length of country name
    capitalLen = strlen(capital); //gets length of capital name

    name_ = new char[countryLen + 1]; //dyanmically creates a new character array of size countryLen and stores base address of array in name_ pointer
    for (int i = 0; i &lt; countryLen; i++)//transfers storage of country name to the name_ array
    {
        name_[i] = country[i];
    }

    capital_ = new char[capitalLen + 1]; //creates a new character array of size capitalLen and stores base address of array in capital_ pointer
    for (int i = 0; i &lt; countryLen; i++)
    {
        capital_[i] = capital[i];
    }

    surfaceArea_ = surfaceArea;

}

char* GetCountryName()
{
    return name_;
}

char* GetCapital()
{
    return capital_;
}

long GetSurfaceArea()
{
    return surfaceArea_;
}
</code></pre>

<p>It's these 3 accessor functions at the bottom that are generating the error 'name_ is not declared in this scope, etc.'</p>
