因此,我在课堂上的任务是制作一个C ++程序,该程序实质上使数据库具有许多选项(添加到数据库,删除条目,修改,搜索和列出)。它必须专门用数组来完成,而不是向量或类等。
我决定制作许多函数来处理每个选项,并使它们彼此调用。经过广泛的搜索,我还决定让一个结构处理声明,以便可以在所有函数中使用它们,而无需使用::标记。我之所以特别使所有内容相互依赖,是因为老师暗示我们将不得不对其进行进一步的工作,因此,如果我修改了某些内容,则其他所有内容都将更改以适应。
#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();
}
}
(我还没有为option编写实际的函数。)但是,当我尝试运行它时,被告知main中没有声明'achi',尽管我公开了所有内容,所以我不会遇到这个错误。我该如何使main主要“查看”该结构及其变量?
答案 0 :(得分:0)
您仅定义了一种类型,没有该类型的值。您还声明了但未定义许多成员函数,然后声明了(并可能定义了许多未显示的)具有相同名称的自由函数。
在提供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();
}
}