首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我怎样才能使我的struct的变量在所有函数中都可见?

我怎样才能使我的struct的变量在所有函数中都可见?
EN

Stack Overflow用户
提问于 2018-11-07 22:38:19
回答 1查看 64关注 0票数 1

因此,我在课堂上的任务是编写一个C++程序,该程序实质上是创建一个具有许多选项(添加、删除条目、修改、搜索和列出)的数据库。它必须专门使用数组来完成,而不是向量或类或其他什么。

我决定创建许多函数来处理每个选项,并让它们彼此调用。在广泛的googling搜索之后,我还决定让struct处理声明,这样我就可以在所有函数中使用它们,而不需要使用::标记。我特别让每一件事都相互依赖,因为老师暗示我们将不得不做进一步的工作,所以如果我修改了一些东西,其他所有的东西都会改变以适应。

代码语言:javascript
运行
复制
#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“看到”结构及其变量?

EN

Stack Overflow用户

发布于 2018-11-07 23:19:14

您只定义了一个类型,没有该类型的值。您还声明了许多成员函数,但没有定义,然后使用相同的名称声明了(可能是定义了许多)自由函数。

在提供struct va的成员函数的类外定义时,您需要使用va::来限定成员的名称,以便将它们与任何同名的其他成员区分开来。如果不是这样,那么所有的好名字都会被标准库中的类成员用完。

在尽可能窄的位置声明变量也是一种好的做法。不要在va的数据成员中放入它的成员函数的本地内容。

代码语言:javascript
运行
复制
#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();
    }
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53191638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档