为什么我的变量没有在作用域中声明?

发布于 2025-01-11 20:39:28 字数 2066 浏览 0 评论 0原文

我现在正在处理一项作业,当运行我的代码时返回此错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懷念過去 2025-01-18 20:39:28

回答你的帖子标题:因为事实并非如此。

您在 main 中声明了 dataArr,但您尝试在 lastSearch 中使用它,因此 lastSearch 看不到它。但是您可以将其作为参数传递,这可能是最简单的解决方法:

void lastSearch(const string lastName, const Database *dataArr) { ... }

并像这样调用它:

lastSearch (search, dataArr);

注意 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 in main, but you are trying to use it in lastSearch, so lastSearch can't see it. But you can pass it in as a parameter, that's probably the easiest fix:

void lastSearch(const string lastName, const Database *dataArr) { ... }

and call it like this:

lastSearch (search, dataArr);

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 use sizeof in lastSearch. 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 in lastSearch without the need to pass it in separately. If you do that, you probably want to pass it by const 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

叫嚣ゝ 2025-01-18 20:39:28

数组不是全局声明的,它们是在您声明它们的地方声明的:-)

在您的情况下,您在 main() 的顶部声明它,这就是它的范围,从声明点到结尾main()。因此,尝试在 lastSearch() 中使用它是无效的。

最简单的修复可能只是将声明移动到 main() 之前,以便它是全局的。但最简单的事情往往不是正确的事情。

您最好完全拥抱 C++(1) 并使用 std::vector 之类的东西,其大小不会任意限制为 100 (例如)并且您可以很容易地传递它,例如:

#include <iostream>
#include <vector>

void function(const std::vector<int> &vec) {
    std::cout << vec.size() << ' ' << vec[0] << '\n'; // Output: 2 42
}

int main() {
    std::vector<int> x;
    x.push_back(42);
    x.push_back(99);
    function(x);
}

向量的主要优点是:

  • 您不限于最多 100 个项目;
  • 您不必像原始数组甚至 std::array 那样传递单独读取的项目的实际计数(您不必在代码,但我向你保证,这是一个问题)。
  • 向量的大小是向量的一个完整属性,在向量范围内的任何地方都可用。

(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 of main(). Trying to use it in lastSearch() 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 to 100 (for example) and which you could pass around quite easily, something like:

#include <iostream>
#include <vector>

void function(const std::vector<int> &vec) {
    std::cout << vec.size() << ' ' << vec[0] << '\n'; // Output: 2 42
}

int main() {
    std::vector<int> x;
    x.push_back(42);
    x.push_back(99);
    function(x);
}

The main advantages with vectors are that:

  • you're not limited to a maximum of 100 items;
  • you don't have to pass around the actual count of items read separately as with a raw array or even a std::array (you don't do that in your code but I assure you, that's a problem).
  • the size of the vector is an integral property of the vector, available anywhere the vector is in scope.

(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文