【发布时间】:2017-02-25 23:18:43
【问题描述】:
#include <iostream>
using namespace std;
int profileClearanceLevel = 0;
void clearanceLevel (int **a) {
*a = &profileClearanceLevel;
}
int main() {
cout << "Greetings and welcome to the Aperture Science Enrichment Center Employee Database." << endl;
string enterToContinue = "Press enter to continue.";
cout << enterToContinue << endl;
cin.get();
cout << "Please input ID: " << flush;
int identificationNumber;
cin >> identificationNumber;
while (identificationNumber < 1) {
cout << "Specified profile not found. Please try again." << endl;
cout << enterToContinue << endl;
cin.get();
cin.get();
cout << "Please input ID: " << flush;
cin >> identificationNumber;
}
while (identificationNumber > 1) {
cout << "Specified profile not found. Please try again." << endl;
cout << enterToContinue << endl;
cin.get();
cin.get();
cout << "Please input ID: " << flush;
cin >> identificationNumber;
}
clearanceLevel (&profileClearanceLevel);
int *profileClearanceLevel = 0;
if (identificationNumber == 1) {
cout << "Please input your password: " << flush;
int profilePassword;
cin >> profilePassword;
while (profilePassword != 2356) {
cout << "Incorrect password. Please try again." << endl;
cout << enterToContinue << endl;
cin.get();
cin.get();
cout << "Please input your password: " << flush;
cin >> profilePassword;
}
if (profilePassword == 2356) {
int employeeNumber = 1;
int *profileClearanceLevel = 1;
cout << "Welcome, Employee #" << employeeNumber << endl;
cout << "Clearance level: " << profileClearanceLevel << endl;
cout << enterToContinue << endl;
cin.get();
cin.get();
}
}
if (*profileClearanceLevel == 1) {
cout << "it worked" << endl;
return 0;
}
}
这是我得到的错误:
00:12:58 **** Incremental Build of configuration Debug for project Menu ****
make all
Building file: ../src/Menu.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Menu.d" -MT"src/Menu.o" -o "src/Menu.o" "../src/Menu.cpp"
../src/Menu.cpp: In function 'int main()':
../src/Menu.cpp:35:44: error: cannot convert 'int*' to 'int**' for argument '1' to 'void clearanceLevel(int**)'
clearanceLevel (&profileClearanceLevel);
^
../src/Menu.cpp:51:37: error: invalid conversion from 'int' to 'int*' [-fpermissive]
int *profileClearanceLevel = 1;
^
make: *** [src/Menu.o] Error 1
00:12:59 Build Finished (took 723ms)
我对 C++ 完全陌生,我只学习了 If 和 If - else。但我认为我知道的足够多,可以创建一种数据库菜单程序。我在谷歌上搜索了如何更改 int 值,我遇到了一些帖子,我试图在这里处理指针,但我显然没有这方面的经验,我不知道该怎么做。请帮我解决这个问题,我已经这样做了好几个小时了。
【问题讨论】:
-
编译器错误是不言自明的:将
int *传递给需要int **的函数;并尝试将int分配给int *。具体有什么不明白的?尽管 C++ 以晦涩难懂的编译错误着称,但这里并非如此。你的编译器的错误相当简单,而且很温和。 -
我想我应该更清楚 - 我明白。但是我能想到的一切都行不通。所以我不知道如何解决它。
-
@Easify 为什么所有这些不必要的指针混乱?
-
我已经说过我正在尝试将 int profileClearanceLevel 从 0 更改为 1。
-
去掉所有变量的 * 和 **。只是永远不要使用它并编写代码以这种方式工作。