学习如何使用c ++中的指针运行到未在范围中声明的Error'变量'

时间:2017-10-08 21:38:59

标签: c++ class c++11 pointers

正如标题所说,我正在学习如何在C ++中使用指针。我的任务是编写一个程序,从文本文件中获取有关国家/地区的数据,将它们加载到类对象数组中,然后让用户查看数据,删除条目或退出。

我遇到了类访问器功能的问题。我收到的错误'变量未在范围内声明。'我得到了这个错误试图告诉我的内容,但我无法弄清楚如何使访问器正常运行。

赋值的要求是使用char指针存储字符串,所以我认为这会增加额外的复杂性。我希望访问器函数只返回一个变量,例如国名,资本和表面积。

我正在使用CodeBlocks并在Ubunutu上运行。我将编译器设置为c ++ 11。

无论如何,这是代码:

//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;
    }
}

这里的函数printCountryData是我想传递类对象和数组大小变量的数组,然后调用存取函数。

//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_;
}

底部的这3个访问者函数产生错误&#39; name_未在此范围内声明等等。&#39;

1 个答案:

答案 0 :(得分:1)

您的功能签名不正确。你应该让它们属于班级Country

char* Country::GetCountryName()
{
    return name_;
}

char* Country::GetCapital()
{
    return capital_;
}

long Country::GetSurfaceArea()
{
    return surfaceArea_;
}