学习如何在遇到错误'variable not declared in scope'的 c++ 中使用指针

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

本文关键字:scope c++ 指针 in variable 遇到 错误 not 学习 declared      更新时间:2023-10-16

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

我遇到了类访问器函数的问题。我正在获取错误"variable not declared in scope"我明白这个错误试图告诉我的是什么,但我不知道如何让访问器正常工作。

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

我正在使用CodeBlocks,并在Ubunstu上运行。我已将编译器设置为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个访问器函数生成了错误"name_未在此范围内声明,等等">

它们上的函数签名不正确。您应该使它们属于Country类。

char* Country::GetCountryName()
{
return name_;
}
char* Country::GetCapital()
{
return capital_;
}
long Country::GetSurfaceArea()
{
return surfaceArea_;
}