c++ - 如何使我的结构变量对所有函数都可见?

标签 c++ database variables struct

因此,我在类里面的作业是编写一个 C++ 程序,该程序实质上是创建一个具有多个选项(添加、删除条目、修改、搜索和列表)的数据库。它必须专门用数组来完成,而不是 vector 或类或其他任何东西。

我决定制作一些函数来处理每个选项,并让它们互相调用。经过广泛的谷歌搜索后,我还决定让结构处理声明,这样我就可以在所有函数中使用它们而无需使用::标记。我特意让一切都相互依赖,因为老师暗示我们将不得不进一步处理它,所以如果我修改某些东西,其他一切都会改变以适应。

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;

struct va{

public:
    int i, j, k, l; //for possible loops or variables I only need for a very short time
    int id = 0;
    int name = id+1;
    // like 6 other ints I also declared here, including year2
    int achi = year2+1;
    //The above is for easier identification of pro[whatever][needed data]. The +1 method is to allow for easier editing later.

    int size = 20, row = 0; //This is important for addition
    string searchterm = ""; //this is for searching

public:
    int main();
    void MainMenu();
    void Addition();
    void Deletion();
    void Search();
    void Modify();
    void List();
};

void MainMenu();
void Addition();
void Deletion();
void Search();
void Modify();
void List(); 
//I just find it neater to make side functions after the main one.

int main()
{
    setlocale(LC_ALL, "");


    const int column = achi;
    const int initsize = size; //These two are so I can edit the size of the array from the struct directly
    string pro[initsize][column]; //This creates the array that's the actual database

    cout << endl << "Welcome to the League of Legends Pro Players database!" << endl << endl;
    cout << endl << "Please, use the menu to access its functions.";

    MainMenu();

    cout << endl;
    return 0; 
}

void MainMenu()
{
    cout << endl << "Main Menu" << endl;
    cout << endl << "1: add an entry to the database.";
    cout << endl << "2: delete an existing entry from the database.";
    cout << endl << "3: search for an existing entry in the database.";
    cout << endl << "4: modify an existing entry.";
    cout << endl << "5: list all existing entries." << endl;

    cin  >> i;

    switch(i)
    {
        case 1: Addition();
        case 2: Deletion();
        case 3: Search();
        case 4: Modify();
        case 5: List();
    }
}

(我还没有为选项编写实际函数。)但是,当我尝试运行它时,我被告知“achi”没有在 main 中声明,尽管我将所有内容都公开了,所以我不会遇到这个错误。我怎样才能让 main “看到”结构及其变量?

最佳答案

您只定义了一个类型,您没有该类型的值。您还声明但未定义许多成员函数,然后声明(并且可能已定义,但未显示许多)具有相同名称的自由函数。

在提供struct va成员函数的类外定义时,需要使用va::限定名字的成员,以区别于任何其他同名的人。如果不是这种情况,那么所有好名字都会被标准库中的类成员用完。

在尽可能窄的位置声明变量也是一种很好的做法。不要将 va 的数据成员与它的成员函数本地的东西混在一起。

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;

struct va {
    static constexpr int size = 20;
    static constexpr int column = ???;
    std::string pro[size][column];

    void MainMenu();
    void Addition();
    void Deletion();
    void Search();
    void Modify();
    void List();
};

int main()
{
    setlocale(LC_ALL, "");

    va instance;
    cout << endl << "Welcome to the League of Legends Pro Players database!" << endl << endl;
    cout << endl << "Please, use the menu to access its functions.";

    instance.MainMenu();

    cout << endl;
    return 0; 
}

void va::MainMenu()
{
    cout << endl << "Main Menu" << endl;
    cout << endl << "1: add an entry to the database.";
    cout << endl << "2: delete an existing entry from the database.";
    cout << endl << "3: search for an existing entry in the database.";
    cout << endl << "4: modify an existing entry.";
    cout << endl << "5: list all existing entries." << endl;

    int i;
    cin >> i;

    switch(i)
    {
        case 1: Addition();
        case 2: Deletion();
        case 3: Search();
        case 4: Modify();
        case 5: List();
    }
}

关于c++ - 如何使我的结构变量对所有函数都可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53191638/

相关文章:

c++ - C++ 的 SFML 错误,未找到入口点

用于跟踪对象的数据库表

java - 无法从另一个类访问变量

c++ - 变量在 if 语句中不可访问

C++ 变量类型 - 值

c++ - 无法更改存储在二维数组中的结构成员

c++ - 为什么我会收到此 'redeclared as different kind of symbol' 错误?

C++ - 将 stdout/stderr 复制到文件,同时保留控制台输出

ruby-on-rails - 如何增量填充数据仓库?

java - 在 SQLite 数据库中搜索