dimanche 8 octobre 2017

Learning how to use pointers in c++ running into Error 'variable not declared in scope'

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.

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.

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.

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

Anyways, here is the code:

//This is the main.cpp file

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

#include "Country.h"


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

using namespace std;

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

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


int main() {

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


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

        switch (menuChoice)
        {
            case 'a':
            {

                cout << "Please enter the full path name of the file you wish to open: " << endl;
                cin >> pathName;

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

            }

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

    return 0;
}

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

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

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

void getArrSize(ifstream& inFile, string pathName, string& 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& inFile, string pathName, string& countryName, string& capitalName, string& 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 < 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 < g_arrsz; ++i)
    {
        cout << g_countryArray[i]->GetCountryName() << ", ";
        cout << g_countryArray[i]->GetCapital() << ", ";
        cout << g_countryArray[i]->GetSurfaceArea() << endl;
    }
}

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.

//here is the Country.h file
#include <iostream>
#include <string>
#include <cstring>

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 < 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 < countryLen; i++)
    {
        capital_[i] = capital[i];
    }

    surfaceArea_ = surfaceArea;

}

char* GetCountryName()
{
    return name_;
}

char* GetCapital()
{
    return capital_;
}

long GetSurfaceArea()
{
    return surfaceArea_;
}

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

Aucun commentaire:

Enregistrer un commentaire