为什么我的变量没有在作用域中声明?
我现在正在处理一项作业,当运行我的代码时返回此错误:
main.cpp:60:20: error: ‘dataArr’ was not declared in this scope
if(tolower(dataArr[i].last) == tolower(lastName))
我不太确定我在这里缺少什么。如果我至少能让它运行,我将不胜感激。谢谢。 我认为数组是全局声明的,所以我认为这不会是我的函数中的问题
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Database
{
string first;
string last;
string ID;
string phoneNum;
};
void lastSearch(string);
void idSearch(string);
int main()
{
Database dataArr[100];
ifstream myFile("library_database.txt");
int count = 0;
while(!myFile.eof() && count < 100)
{
myFile >> dataArr[count].first >> dataArr[count].last >> dataArr[count].ID >> dataArr[count].phoneNum;
cout << dataArr[count].first << " " << dataArr[count].last << " " << dataArr[count].ID << " " << dataArr[count].phoneNum << endl;
count++;
}
int input;
string search;
cout << "Would you like to search by last name or member ID?\n1. Last Name\n2. ID\n> ";
cin >> input;
while(input != 1 || input != 2)
{
cout << "Enter a valid answer.\n> ";
cin >> input;
}
if(input == 1)
{
cout << "Enter last name: ";
cin >> search;
lastSearch(search);
}
if(input == 2)
{
cout << "Enter ID: ";
cin >> search;
idSearch(search);
}
return 0;
}
void lastSearch(string lastName)
{
int num = 0;
for(int i = 0; i < 100; i++)
{
if(tolower(dataArr[i].last) == tolower(lastName))
{
cout << dataArr[i].first << " " << dataArr[i].last << " " << dataArr[i].ID << " " << dataArr[i].phoneNum << endl
num++;
}
}
if(num == 0)
{
cout << "No match was found in the file.";
}
}
voidSearch 被删除以允许发布
I'm working on an assignment right now and when run my code returns this error:
main.cpp:60:20: error: ‘dataArr’ was not declared in this scope
if(tolower(dataArr[i].last) == tolower(lastName))
I'm not quite sure what I'm missing here. If I could at least get it to run I'd appreciate it. Thanks.
I thought arrays were declared globally so i thought it wouldn't be an issue in my functions
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Database
{
string first;
string last;
string ID;
string phoneNum;
};
void lastSearch(string);
void idSearch(string);
int main()
{
Database dataArr[100];
ifstream myFile("library_database.txt");
int count = 0;
while(!myFile.eof() && count < 100)
{
myFile >> dataArr[count].first >> dataArr[count].last >> dataArr[count].ID >> dataArr[count].phoneNum;
cout << dataArr[count].first << " " << dataArr[count].last << " " << dataArr[count].ID << " " << dataArr[count].phoneNum << endl;
count++;
}
int input;
string search;
cout << "Would you like to search by last name or member ID?\n1. Last Name\n2. ID\n> ";
cin >> input;
while(input != 1 || input != 2)
{
cout << "Enter a valid answer.\n> ";
cin >> input;
}
if(input == 1)
{
cout << "Enter last name: ";
cin >> search;
lastSearch(search);
}
if(input == 2)
{
cout << "Enter ID: ";
cin >> search;
idSearch(search);
}
return 0;
}
void lastSearch(string lastName)
{
int num = 0;
for(int i = 0; i < 100; i++)
{
if(tolower(dataArr[i].last) == tolower(lastName))
{
cout << dataArr[i].first << " " << dataArr[i].last << " " << dataArr[i].ID << " " << dataArr[i].phoneNum << endl
num++;
}
}
if(num == 0)
{
cout << "No match was found in the file.";
}
}
voidSearch was removed to allow this to be posted
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
回答你的帖子标题:因为事实并非如此。
您在
main
中声明了dataArr
,但您尝试在lastSearch
中使用它,因此lastSearch
看不到它。但是您可以将其作为参数传递,这可能是最简单的解决方法:并像这样调用它:
注意 const 的使用(养成尽可能这样做的习惯)以及您的数组当您像这样将其作为参数传递时,它会“衰减”为指针,因此不要试图在
lastSearch
中使用sizeof
。如果您需要知道数组中元素的数量,也可以将其作为参数传递。或者,更好的是,使用
std::array
而不是 C 风格的数组,然后数组的大小可以在lastSearch
中使用,无需单独传入。如果这样做,您可能希望通过 const 引用传递它,以避免每次调用该函数时都复制它。最后,可能是时候了解
std::vector
。以增加一点复杂性(但不多)为代价,这将避免分配固定大小的数组的需要。同样,出于同样的原因,通过引用传递它。一些睡前读物:权威的 C++ 书籍指南和列表
To answer the title of your post: because it isn't.
You declare
dataArr
inmain
, but you are trying to use it inlastSearch
, solastSearch
can't see it. But you can pass it in as a parameter, that's probably the easiest fix:and call it like this:
Note the use of
const
(get into the habit of doing that whenever you can) and that your array 'decays' to a pointer when you pass it as a parameter like this, so don't be tempted to usesizeof
inlastSearch
. If you need to know the number of elements in the array, pass that as a parameter too.Or, better, use
std::array
instead of a C-style array and then the size of the array is available inlastSearch
without the need to pass it in separately. If you do that, you probably want to pass it byconst
reference to avoid copying it every time you call the function.Finally, it might be time to learn about
std::vector
. At the expense of a little more complexity (but not much), this would avoid the need to allocate a fixed size array. Again, for the same reason, pass it around by reference.Some bedtime reading: The Definitive C++ Book Guide and List
数组不是全局声明的,它们是在您声明它们的地方声明的:-)
在您的情况下,您在
main()
的顶部声明它,这就是它的范围,从声明点到结尾main()
。因此,尝试在lastSearch()
中使用它是无效的。最简单的修复可能只是将声明移动到
main()
之前,以便它是全局的。但最简单的事情往往不是正确的事情。您最好完全拥抱 C++(1) 并使用
std::vector
之类的东西,其大小不会任意限制为100
(例如)并且您可以很容易地传递它,例如:向量的主要优点是:
(1) 我喜欢将各种各样的开发人员称为
C+
开发人员。这些人虽然自称是 C++ 开发人员,但从未真正接受过 C++ 的做事方式,而是坚持 C 风格的编程实践,如非智能指针或普通数组:-)其中一些东西可能仍然有放在现代 C++ 代码中,但在使用它们时应该谨慎。
Arrays are not declared globally, they are declared where you declare them :-)
In your case, you declare it at the top of
main()
so that is its scope, from point of declaration to end ofmain()
. Trying to use it inlastSearch()
is therefore invalid.The easiest fix is probably just to move the declaration immediately before
main()
so that it is global. But the easiest things is often not the right thing.You would be better off embracing C++ fully(1) and using something like
std::vector
, whose size isn't arbitrarily limited to100
(for example) and which you could pass around quite easily, something like:The main advantages with vectors are that:
std::array
(you don't do that in your code but I assure you, that's a problem).(1) There's a variety of developers I like to call
C+
developers. These are the people that, though they claim to be C++ developers, have never really embraced the C++ way of doing things, sticking to C style programming practices like non-smart pointers or normal arrays :-)Some of those things may still have a place in modern C++ code but you should be circumspect in their use.