switch 语句报错
我怎么也想不通,为什么老是报 jump to case label?
switch (...) {
case ...:
...
...
case 8: // Error
...
case 9: // Error
...
}
实际上有511行的,我怕鱼C论坛炸掉
所以我就扔了个模型。 现在变这样子了:
C:\Program Files\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32\bin\ld.exe C:\Users\hzk20\AppData\Local\Temp\ccruxmxX.o:口算.cpp:(.text+0x10ae): undefined reference to `read(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
C:\Program Files\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32\bin\ld.exe C:\Users\hzk20\AppData\Local\Temp\ccruxmxX.o:口??cpp:(.text+0x1131): undefined reference to `write(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
C:\Users\hzk20\Desktop\collect2.exe ld returned 1 exit status
【Error】ld returned 1 exit status https://blog.csdn.net/e_t_e_r_n_i_t_y/article/details/109982282 你扔一个这模型说明不了问题
因为这个就是标准的switch case用法
这个模型是没问题的
对于这件事你自己是知道的吧
你扔一个switch case的用法上来做什么
没写break结束,还是已经有一个小黑窗在运行了,又运行同一个 https://blog.csdn.net/yang9325/article/details/120076472 步入进去,这样更容易找到是哪里出了问题
源码【不许侵权】:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cerrno>
using namespace std;
typedef unsigned int uint, *puint;
typedef const char cchar, *pcchar;
#define randint(a, b) ((rand() % (b-a+1)) + a)
#define MAX_LEVEL 15
struct Account {
string name = "Player";
uint max_score = 0;
uint score = 0;
uint level = 0;
uint exp = 0;
long long atid = randint(100000000000000, 999999999999999);
};
bool add(uint a, uint b);
bool sub(uint a, uint b);
bool mcl(uint a, uint b);
bool div(uint a, uint b);
bool writeInfo(string file, Account *account);
bool readInfo(string file, Account *account);
int main(void) {
short int code;
int number, score, operatorNum;
Account *account;
bool savable = false;
string rfile, wfile;
cout << "---欢迎来到口算小能手---" << endl;
while (code != 0) {
cout << "请选择模式:" << endl;
cout << "1 -- 纯加法[+1-3]" << endl;
cout << "2 -- 纯减法[+1-3]" << endl;
cout << "3 -- 纯乘法[+2-4]" << endl;
cout << "4 -- 纯除法[+2-4]" << endl;
cout << "5 -- 混合运算[作对一题就+1]" << endl;
cout << "6 -- 死亡算法[作对一题就+3]" << endl;
cout << "7 -- 导入数据[+0]" << endl;
cout << "8 -- 保存数据[+0]" << endl;
cout << "9 -- 查看账户信息[+0]" << endl;
cout << "0 -- Exit退出[+1]" << endl;
cout << "注:200积分升一级,共15级qwq!" << endl;
cout << "请输入指令代码:";
cin >> code;
if ((*account).exp >= 200 && (*account).level < 15) {
(*account).exp -= 200;
(*account).level += 1;
}
switch (code) {
case 0:
if ((*account).score > 0) {
if (savable) {
exit(EXIT_SUCCESS);
}
else {
char choose;
cout << "你还没有保存信息哦!" << endl;
cout << "确定要退出吗?【N/y】";
cin >> choose;
switch (choose) {
case 'Y':
case 'y':
exit(EXIT_SUCCESS);
break;
case 'N':
case 'n':
break;
}
}
}
else {
cout << "别想钻空子!加分不是白加的!" << endl;
}
code = 10;
break;
case 1:
cout << "请输入题目数:";
cin >> number;
for (int count = number;count > 0;count--) {
if (add(randint(100, 999), randint(100, 999))) {
score++;
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
if (score >= number/5) {
(*account).exp += 1;
}
else if (score >= number/3) {
(*account).exp += 2;
}
else if (score >= number/1) {
(*account).exp += 3;
}
score = 0;
break;
case 2:
cout << "请输入题目数:";
cin >> number;
uint a, b;
while (true) {
a = randint(100, 999);
b = randint(100, 999);
if (a >= b) {
break;
}
}
for (int count = number;count > 0;count--) {
if (sub(a, b)) {
score++;
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
if (score >= number/5) {
(*account).exp += 1;
}
else if (score >= number/3) {
(*account).exp += 2;
}
else if (score >= number/1) {
(*account).exp += 3;
}
score = 0;
break;
case 3:
cout << "请输入题目数:";
cin >> number;
for (int count = number;count > 0;count--) {
if (mcl(randint(100, 999), randint(100, 999))) {
score++;
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
if (score >= number/5) {
(*account).exp += 2;
}
else if (score >= number/3) {
(*account).exp += 3;
}
else if (score >= number/1) {
(*account).exp += 4;
}
score = 0;
break;
case 4:
cout << "请输入题目数:";
cin >> number;
uint c, d;
while (true) {
c = randint(100, 999);
d = randint(100, 999);
if (c % d == 0) {
break;
}
}
for (int count = number;count > 0;count--) {
if (div(randint(c, d), randint(c, d))) {
score++;
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
if (score >= number/5) {
(*account).exp += 2;
}
else if (score >= number/3) {
(*account).exp += 3;
}
else if (score >= number/1) {
(*account).exp += 4;
}
score = 0;
break;
case 5:
cout << "请输入题目数:";
cin >> number;
for (int count = number;count > 0;count--) {
operatorNum = randint(1, 4);
uint exp;
switch (operatorNum) {
case 1:
if (add(randint(100, 999), randint(100, 999))) {
score++;
exp++;
}
break;
case 2:
uint a, b;
while (true) {
a = randint(100, 999);
b = randint(100, 999);
if (a >= b) {
break;
}
}
if (sub(a, b)) {
score++;
exp++;
}
break;
case 3:
if (mcl(randint(100, 999), randint(100, 999))) {
score++;
exp++;
}
break;
case 4:
uint c, d;
while (true) {
c = randint(100, 999);
d = randint(100, 999);
if (c % d == 0) {
break;
}
}
if (div(c, d)) {
score++;
exp++;
}
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
score = 0;
break;
case 6:
cout << "请输入题目数:";
cin >> number;
for (int count = number;count > 0;count--) {
operatorNum = randint(1, 4);
uint exp;
switch (operatorNum) {
case 1:
if (add(randint(10000, 99999), randint(10000, 99999))) {
score++;
exp += 3;
}
break;
case 2:
uint a, b;
while (true) {
a = randint(10000, 99999);
b = randint(10000, 99999);
if (a >= b) {
break;
}
}
if (sub(a, b)) {
score++;
exp += 3;
}
break;
case 3:
if (mcl(randint(10000, 99999), randint(10000, 99999))) {
score++;
exp += 3;
}
break;
case 4:
uint e, f;
while (true) {
e = randint(10000, 99999);
f = randint(10000, 99999);
if (e % f == 0) {
break;
}
}
if (div(e, f)) {
score++;
exp += 3;
}
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
score = 0;
break;
case 7:
cout << "请输入文件路径:";
cin >> rfile;
if (readInfo(rfile.c_str(), account)) {
cout << "读取信息成功!" << endl;
}
else {
cout << "读取文件失败,请重试。" << endl;
}
break;
case 8:
cout << "请输入要保存的路径:";
cin >> wfile;
if (writeInfo(wfile.c_str(), account)) {
cout << "保存文件成功!" << endl;
savable = true;
}
else {
cout << "保存文件失败,请重试。" << endl;
}
break;
case 9:
cout << "Account Information:" << endl;
cout << "Name(姓名):" << (*account).name << endl;
cout << "Max-Score(最高分数):" << (*account).max_score << endl;
cout << "Score(累计分数):" << (*account).score << endl;
cout << "Level(等级):" << (*account).score << endl;
cout << "Exp(经验):" << (*account).score << endl;
cout << "ATID(账户ID):" << (*account).atid << endl;
break;
}
}
return 0;
}
bool add(uint a, uint b) {
uint answer = a + b;
uint userinput;
cout << "%d + %d = " << a, b;
cin >> userinput;
if (userinput == answer) {
cout << "答对啦!" << endl;
return true;
}
else {
cout << "答错啦!" << endl;
return false;
}
}
bool sub(uint a, uint b) {
uint answer = a - b;
uint userinput;
cout << "%d - %d = " << a, b;
cin >> userinput;
if (userinput == answer) {
cout << "答对啦!" << endl;
return true;
}
else {
cout << "答错啦!" << endl;
return false;
}
}
bool mcl(uint a, uint b) {
uint answer = a * b;
uint userinput;
cout << "%d × %d = " << a, b;
cin >> userinput;
if (userinput == answer) {
cout << "答对啦!" << endl;
return true;
}
else {
cout << "答错啦!" << endl;
return false;
}
}
bool div(uint a, uint b) {
uint answer = a / b;
uint userinput;
cout << "%d ÷ %d = " << a, b;
cin >> userinput;
if (userinput == answer) {
cout << "答对啦!" << endl;
return true;
}
else {
cout << "答错啦!" << endl;
return false;
}
}
bool writeInfo(pcchar file, Account *account) {
FILE *fp = NULL;
if ((fp = fopen(file, "w")) == NULL) {
cout << "打开文件失败:: %s" << errno, strerror(errno);
return false;
}
fwrite(account, sizeof(Account), 1, fp);
return true;
}
bool readInfo(pcchar file, Account *account) {
FILE *fp = NULL;
char choose;
if ((fp = fopen(file, "r")) == NULL) {
cout << "打开文件失败:: %s" << errno, strerror(errno);
return false;
}
cout << "确定覆盖现在的信息吗?【Y/n】";
cin >> choose;
switch (choose) {
case 'Y':
case 'y':
break;
case 'N':
case 'n':
cout << "用户取消了操作。" << endl;
return true;
default:
cout << "请认真输入!" << endl;
return true;
}
fread(account, sizeof(Account), 1, fp);
return true;
}
条款:
README.md(务必看完)
学编程最重要的一件事就是,千万不要看编译器的提示
不管是什么提示,只要是提示就一定不要看
对吧?
不至于吧?怎么能把代码写成这样呢?
先把编译器给出的警告解决了再说吧
sh-5.1$ cat main.cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cerrno>
using namespace std;
typedef unsigned int uint, *puint;
typedef const char cchar, *pcchar;
#define randint(a, b) ((rand() % (b-a+1)) + a)
#define MAX_LEVEL 15
struct Account {
string name = "Player";
uint max_score = 0;
uint score = 0;
uint level = 0;
uint exp = 0;
long long atid = randint(100000000000000, 999999999999999);
};
bool add(uint a, uint b);
bool sub(uint a, uint b);
bool mcl(uint a, uint b);
bool div(uint a, uint b);
bool writeInfo(string file, Account *account);
bool readInfo(string file, Account *account);
int main(void) {
//short int code; // variable 'code' is used uninitialized whenever function 'main' is called (FixIt)
short int code = 0;
int number, score, operatorNum;
Account *account;
bool savable = false;
string rfile, wfile;
cout << "---欢迎来到口算小能手---" << endl;
account = NULL; // line: 61
while (code != 0) {
cout << "请选择模式:" << endl;
cout << "1 -- 纯加法[+1-3]" << endl;
cout << "2 -- 纯减法[+1-3]" << endl;
cout << "3 -- 纯乘法[+2-4]" << endl;
cout << "4 -- 纯除法[+2-4]" << endl;
cout << "5 -- 混合运算[作对一题就+1]" << endl;
cout << "6 -- 死亡算法[作对一题就+3]" << endl;
cout << "7 -- 导入数据[+0]" << endl;
cout << "8 -- 保存数据[+0]" << endl;
cout << "9 -- 查看账户信息[+0]" << endl;
cout << "0 -- Exit退出[+1]" << endl;
cout << "注:200积分升一级,共15级qwq!" << endl;
cout << "请输入指令代码:";
cin >> code;
if ((*account).exp >= 200 && (*account).level < 15) { // variable 'account' is uninitialized when used here (FixIt)
(*account).exp -= 200;
(*account).level += 1;
}
switch (code) {
case 0:
if ((*account).score > 0) {
if (savable) {
exit(EXIT_SUCCESS);
}
else {
char choose;
cout << "你还没有保存信息哦!" << endl;
cout << "确定要退出吗?【N/y】";
cin >> choose;
switch (choose) {
case 'Y':
case 'y':
exit(EXIT_SUCCESS);
break;
case 'N':
case 'n':
break;
}
}
}
else {
cout << "别想钻空子!加分不是白加的!" << endl;
}
code = 10;
break;
case 1:
cout << "请输入题目数:";
cin >> number;
for (int count = number;count > 0;count--) {
if (add(randint(100, 999), randint(100, 999))) {
score++;
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
if (score >= number/5) {
(*account).exp += 1;
}
else if (score >= number/3) {
(*account).exp += 2;
}
else if (score >= number/1) {
(*account).exp += 3;
}
score = 0;
break;
case 2:
cout << "请输入题目数:";
cin >> number;
uint a, b;
while (true) {
a = randint(100, 999);
b = randint(100, 999);
if (a >= b) {
break;
}
}
for (int count = number;count > 0;count--) {
if (sub(a, b)) {
score++;
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
if (score >= number/5) {
(*account).exp += 1;
}
else if (score >= number/3) {
(*account).exp += 2;
}
else if (score >= number/1) {
(*account).exp += 3;
}
score = 0;
break;
case 3:
cout << "请输入题目数:";
cin >> number;
for (int count = number;count > 0;count--) {
if (mcl(randint(100, 999), randint(100, 999))) {
score++;
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
if (score >= number/5) {
(*account).exp += 2;
}
else if (score >= number/3) {
(*account).exp += 3;
}
else if (score >= number/1) {
(*account).exp += 4;
}
score = 0;
break;
case 4:
cout << "请输入题目数:";
cin >> number;
uint c, d;
while (true) {
c = randint(100, 999);
d = randint(100, 999);
if (c % d == 0) {
break;
}
}
for (int count = number;count > 0;count--) {
if (div(randint(c, d), randint(c, d))) {
score++;
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
if (score >= number/5) {
(*account).exp += 2;
}
else if (score >= number/3) {
(*account).exp += 3;
}
else if (score >= number/1) {
(*account).exp += 4;
}
score = 0;
break;
case 5:
cout << "请输入题目数:";
cin >> number;
for (int count = number;count > 0;count--) {
operatorNum = randint(1, 4);
uint exp;
switch (operatorNum) {
case 1:
if (add(randint(100, 999), randint(100, 999))) {
score++;
// 至于吗?这么多?怎么写代码的?
exp++; // variable 'exp' is uninitialized when used here (FixIt)
}
break;
case 2:
uint a, b;
while (true) {
a = randint(100, 999);
b = randint(100, 999);
if (a >= b) {
break;
}
}
if (sub(a, b)) {
score++;
exp++;
}
break;
case 3:
if (mcl(randint(100, 999), randint(100, 999))) {
score++;
exp++;
}
break;
case 4:
uint c, d;
while (true) {
c = randint(100, 999);
d = randint(100, 999);
if (c % d == 0) {
break;
}
}
if (div(c, d)) {
score++;
exp++;
}
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
score = 0;
break;
case 6:
cout << "请输入题目数:";
cin >> number;
for (int count = number;count > 0;count--) {
operatorNum = randint(1, 4);
// 说真的,这代码可以重写了
uint exp; // variable 'exp' set but not used
switch (operatorNum) {
case 1:
if (add(randint(10000, 99999), randint(10000, 99999))) {
score++;
exp += 3; // 我都懒得给你注释了
}
break;
case 2:
uint a, b;
while (true) {
a = randint(10000, 99999);
b = randint(10000, 99999);
if (a >= b) {
break;
}
}
if (sub(a, b)) {
score++;
exp += 3;
}
break;
case 3:
if (mcl(randint(10000, 99999), randint(10000, 99999))) {
score++;
exp += 3;
}
break;
case 4:
uint e, f;
while (true) {
e = randint(10000, 99999);
f = randint(10000, 99999);
if (e % f == 0) {
break;
}
}
if (div(e, f)) {
score++;
exp += 3;
}
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
score = 0;
break;
case 7:
cout << "请输入文件路径:";
cin >> rfile;
if (readInfo(rfile.c_str(), account)) {
cout << "读取信息成功!" << endl;
}
else {
cout << "读取文件失败,请重试。" << endl;
}
break;
case 8:
cout << "请输入要保存的路径:";
cin >> wfile;
if (writeInfo(wfile.c_str(), account)) {
cout << "保存文件成功!" << endl;
savable = true;
}
else {
cout << "保存文件失败,请重试。" << endl;
}
break;
case 9:
cout << "Account Information:" << endl;
cout << "Name(姓名):" << (*account).name << endl;
cout << "Max-Score(最高分数):" << (*account).max_score << endl;
cout << "Score(累计分数):" << (*account).score << endl;
cout << "Level(等级):" << (*account).score << endl;
cout << "Exp(经验):" << (*account).score << endl;
cout << "ATID(账户ID):" << (*account).atid << endl;
break;
}
}
return 0;
}
bool add(uint a, uint b) {
uint answer = a + b;
uint userinput;
cout << "%d + %d = " << a, b;
cin >> userinput;
if (userinput == answer) {
cout << "答对啦!" << endl;
return true;
}
else {
cout << "答错啦!" << endl;
return false;
}
}
bool sub(uint a, uint b) {
uint answer = a - b;
uint userinput;
cout << "%d - %d = " << a, b;
cin >> userinput;
if (userinput == answer) {
cout << "答对啦!" << endl;
return true;
}
else {
cout << "答错啦!" << endl;
return false;
}
}
bool mcl(uint a, uint b) {
uint answer = a * b;
uint userinput;
cout << "%d × %d = " << a, b;
cin >> userinput;
if (userinput == answer) {
cout << "答对啦!" << endl;
return true;
}
else {
cout << "答错啦!" << endl;
return false;
}
}
bool div(uint a, uint b) {
uint answer = a / b;
uint userinput;
cout << "%d ÷ %d = " << a, b;
cin >> userinput;
if (userinput == answer) {
cout << "答对啦!" << endl;
return true;
}
else {
cout << "答错啦!" << endl;
return false;
}
}
bool writeInfo(pcchar file, Account *account) {
FILE *fp = NULL;
if ((fp = fopen(file, "w")) == NULL) {
cout << "打开文件失败:: %s" << errno, strerror(errno);
return false;
}
fwrite(account, sizeof(Account), 1, fp);
return true;
}
bool readInfo(pcchar file, Account *account) {
FILE *fp = NULL;
char choose;
if ((fp = fopen(file, "r")) == NULL) {
cout << "打开文件失败:: %s" << errno, strerror(errno);
return false;
}
cout << "确定覆盖现在的信息吗?【Y/n】";
cin >> choose;
switch (choose) {
case 'Y':
case 'y':
break;
case 'N':
case 'n':
cout << "用户取消了操作。" << endl;
return true;
default:
cout << "请认真输入!" << endl;
return true;
}
fread(account, sizeof(Account), 1, fp);
return true;
}
sh-5.1$ g++ -g -Wall -o main main.cpp
main.cpp: In function ‘int main()’:
main.cpp:109:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
109 | if (score >= (*account).max_score) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:149:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
149 | if (score >= (*account).max_score) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:178:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
178 | if (score >= (*account).max_score) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:218:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
218 | if (score >= (*account).max_score) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:301:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
301 | if (score >= (*account).max_score) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:374:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
374 | if (score >= (*account).max_score) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function ‘bool add(uint, uint)’:
main.cpp:429:37: warning: right operand of comma operator has no effect [-Wunused-value]
429 | cout << "%d + %d = " << a, b;
| ^
main.cpp: In function ‘bool sub(uint, uint)’:
main.cpp:446:37: warning: right operand of comma operator has no effect [-Wunused-value]
446 | cout << "%d - %d = " << a, b;
| ^
main.cpp: In function ‘bool mcl(uint, uint)’:
main.cpp:463:37: warning: right operand of comma operator has no effect [-Wunused-value]
463 | cout << "%d × %d = " << a, b;
| ^
main.cpp: In function ‘bool div(uint, uint)’:
main.cpp:480:37: warning: right operand of comma operator has no effect [-Wunused-value]
480 | cout << "%d ÷ %d = " << a, b;
| ^
/usr/bin/ld: /tmp/ccd3nG4I.o: in function `main':
/tmp/main.cpp:385: undefined reference to `readInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
/usr/bin/ld: /tmp/main.cpp:398: undefined reference to `writeInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
collect2: error: ld returned 1 exit status
sh-5.1$ clang++ -g -Wall -o main main.cpp
main.cpp:315:46: warning: variable 'exp' set but not used [-Wunused-but-set-variable]
uint exp; // variable 'exp' set but not used
^
main.cpp:321:65: warning: variable 'exp' is uninitialized when used here [-Wuninitialized]
exp += 3; // 我都懒得给你注释了
^~~
main.cpp:315:49: note: initialize the variable 'exp' to silence this warning
uint exp; // variable 'exp' set but not used
^
= 0
main.cpp:248:65: warning: variable 'exp' is uninitialized when used here [-Wuninitialized]
exp++; // variable 'exp' is uninitialized when used here (FixIt)
^~~
main.cpp:241:49: note: initialize the variable 'exp' to silence this warning
uint exp;
^
= 0
main.cpp:429:36: warning: expression result unused [-Wunused-value]
cout << "%d + %d = " << a, b;
^
main.cpp:446:36: warning: expression result unused [-Wunused-value]
cout << "%d - %d = " << a, b;
^
main.cpp:463:37: warning: expression result unused [-Wunused-value]
cout << "%d × %d = " << a, b;
^
main.cpp:480:37: warning: expression result unused [-Wunused-value]
cout << "%d ÷ %d = " << a, b;
^
7 warnings generated.
/usr/bin/ld: /tmp/main-52f47d.o: in function `main':
<unknown>:385: undefined reference to `readInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
/usr/bin/ld: <unknown>:398: undefined reference to `writeInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
sh-5.1$
本帖最后由 小凯2013 于 2022-12-26 13:22 编辑
用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.34.31937 版
版权所有(C) Microsoft Corporation。保留所有权利。
口算.cpp
Microsoft (R) Incremental Linker Version 14.34.31937.0
Copyright (C) Microsoft Corporation.All rights reserved.
/out:口算.exe
口算.obj
口算.obj : error LNK2019: 无法解析的外部符号 "bool __cdecl writeInfo(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct Account *)" (?writeInfo@@YA_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEAUAccount@@@Z),函数 main 中引用了该符号
已定义且可能匹配的符号上的提示:
"bool __cdecl writeInfo(char const *,struct Account *)" (?writeInfo@@YA_NPEBDPEAUAccount@@@Z)
口算.obj : error LNK2019: 无法解析的外部符号 "bool __cdecl readInfo(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct Account *)" (?readInfo@@YA_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEAUAccount@@@Z),函数 main 中引用了该符号
已定义且可能匹配的符号上的提示:
"bool __cdecl readInfo(char const *,struct Account *)" (?readInfo@@YA_NPEBDPEAUAccount@@@Z)
口算.exe : fatal error LNK1120: 2 个无法解析的外部命令
怎么办???{:10_282:}
小凯2013 发表于 2022-12-26 13:18
用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.34.31937 版
版权所有(C) Microsoft Corporation。保留 ...
先把上面的编译器警告全改了再说
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cerrno>
// Informations: In README.md
using namespace std;
typedef unsigned int uint, *puint;
typedef const char cchar, *pcchar;
#define randint(a, b) ((rand() % (b-a+1)) + a)
#define MAX_LEVEL 15
struct Account {
string name = "Player";
uint max_score = 0;
uint score = 0;
uint level = 0;
uint exp = 0;
long long int atid = randint(100000000000000, 999999999999999);
};
bool add(uint a, uint b);
bool sub(uint a, uint b);
bool mcl(uint a, uint b);
bool div(uint a, uint b);
bool writeInfo(string file, Account *account);
bool readInfo(string file, Account *account);
int main(void) {
short int code = 9;
int number, score, operatorNum;
Account *account = (Account *)malloc(1024);
bool savable = false;
string rfile, wfile;
(*account).name = "Player";
(*account).max_score = 0;
(*account).score = 0;
(*account).level = 0;
(*account).exp = 0;
(*account).atid = randint(100000000000000, 999999999999999);
cout << "---欢迎来到口算小能手---" << endl;
while (code != 0) {
cout << "请选择模式:" << endl;
cout << "1 -- 纯加法[+1-3]" << endl;
cout << "2 -- 纯减法[+1-3]" << endl;
cout << "3 -- 纯乘法[+2-4]" << endl;
cout << "4 -- 纯除法[+2-4]" << endl;
cout << "5 -- 混合运算[作对一题就+1]" << endl;
cout << "6 -- 死亡算法[作对一题就+3]" << endl;
cout << "7 -- 导入数据[+0]" << endl;
cout << "8 -- 保存数据[+0]" << endl;
cout << "9 -- 查看账户信息[+0]" << endl;
cout << "0 -- Exit退出[+1]" << endl;
cout << "注:200积分升一级,共15级qwq!" << endl;
cout << "请输入指令代码:";
cin >> code;
if ((*account).exp >= 200 && (*account).level < 15) {
(*account).exp -= 200;
(*account).level += 1;
}
switch (code) {
case 0:
if ((*account).score > 0) {
if (savable) {
exit(EXIT_SUCCESS);
}
else {
char choose;
cout << "你还没有保存信息哦!" << endl;
cout << "确定要退出吗?【N/y】";
cin >> choose;
switch (choose) {
case 'Y':
case 'y':
exit(EXIT_SUCCESS);
break;
case 'N':
case 'n':
break;
}
}
}
else {
cout << "别想钻空子!加分不是白加的!" << endl;
}
code = 10;
break;
case 1:
cout << "请输入题目数:";
cin >> number;
for (int count = number;count > 0;count--) {
if (add(randint(100, 999), randint(100, 999))) {
score++;
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
if (score >= number/5) {
(*account).exp += 1;
}
else if (score >= number/3) {
(*account).exp += 2;
}
else if (score >= number/1) {
(*account).exp += 3;
}
score = 0;
break;
case 2:
cout << "请输入题目数:";
cin >> number;
uint a, b;
while (true) {
a = randint(100, 999);
b = randint(100, 999);
if (a >= b) {
break;
}
}
for (int count = number;count > 0;count--) {
if (sub(a, b)) {
score++;
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
if (score >= number/5) {
(*account).exp += 1;
}
else if (score >= number/3) {
(*account).exp += 2;
}
else if (score >= number/1) {
(*account).exp += 3;
}
score = 0;
break;
case 3:
cout << "请输入题目数:";
cin >> number;
for (int count = number;count > 0;count--) {
if (mcl(randint(100, 999), randint(100, 999))) {
score++;
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
if (score >= number/5) {
(*account).exp += 2;
}
else if (score >= number/3) {
(*account).exp += 3;
}
else if (score >= number/1) {
(*account).exp += 4;
}
score = 0;
break;
case 4:
cout << "请输入题目数:";
cin >> number;
uint c, d;
while (true) {
c = randint(100, 999);
d = randint(100, 999);
if (c % d == 0) {
break;
}
}
for (int count = number;count > 0;count--) {
if (div(randint(c, d), randint(c, d))) {
score++;
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
if (score >= number/5) {
(*account).exp += 2;
}
else if (score >= number/3) {
(*account).exp += 3;
}
else if (score >= number/1) {
(*account).exp += 4;
}
score = 0;
break;
case 5:
cout << "请输入题目数:";
cin >> number;
for (int count = number;count > 0;count--) {
operatorNum = randint(1, 4);
uint exp;
switch (operatorNum) {
case 1:
if (add(randint(100, 999), randint(100, 999))) {
score++;
exp++;
}
break;
case 2:
uint a, b;
while (true) {
a = randint(100, 999);
b = randint(100, 999);
if (a >= b) {
break;
}
}
if (sub(a, b)) {
score++;
exp++;
}
break;
case 3:
if (mcl(randint(100, 999), randint(100, 999))) {
score++;
exp++;
}
break;
case 4:
uint c, d;
while (true) {
c = randint(100, 999);
d = randint(100, 999);
if (c % d == 0) {
break;
}
}
if (div(c, d)) {
score++;
exp++;
}
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
score = 0;
break;
case 6:
cout << "请输入题目数:";
cin >> number;
for (int count = number;count > 0;count--) {
operatorNum = randint(1, 4);
uint exp;
switch (operatorNum) {
case 1:
if (add(randint(10000, 99999), randint(10000, 99999))) {
score++;
exp += 3;
}
break;
case 2:
uint a, b;
while (true) {
a = randint(10000, 99999);
b = randint(10000, 99999);
if (a >= b) {
break;
}
}
if (sub(a, b)) {
score++;
exp += 3;
}
break;
case 3:
if (mcl(randint(10000, 99999), randint(10000, 99999))) {
score++;
exp += 3;
}
break;
case 4:
uint e, f;
while (true) {
e = randint(10000, 99999);
f = randint(10000, 99999);
if (e % f == 0) {
break;
}
}
if (div(e, f)) {
score++;
exp += 3;
}
}
}
(*account).score += score;
if (score >= (*account).max_score) {
(*account).max_score = score;
}
score = 0;
break;
case 7:
cout << "请输入文件路径:";
cin >> rfile;
if (readInfo(rfile.c_str(), account)) {
cout << "读取信息成功!" << endl;
}
else {
cout << "读取文件失败,请重试。" << endl;
}
break;
case 8:
cout << "请输入要保存的路径:";
cin >> wfile;
if (writeInfo(wfile.c_str(), account)) {
cout << "保存文件成功!" << endl;
savable = true;
}
else {
cout << "保存文件失败,请重试。" << endl;
}
break;
case 9:
cout << "Account Information:" << endl;
cout << "Name(姓名):" << (*account).name << endl;
cout << "Max-Score(最高分数):" << (*account).max_score << endl;
cout << "Score(累计分数):" << (*account).score << endl;
cout << "Level(等级):" << (*account).score << endl;
cout << "Exp(经验):" << (*account).score << endl;
cout << "ATID(账户ID):" << (*account).atid << endl;
break;
}
}
return 0;
}
bool add(uint a, uint b) {
uint answer = a + b;
uint userinput;
cout << "%d + %d = " << a, b;
cin >> userinput;
if (userinput == answer) {
cout << "答对啦!" << endl;
return true;
}
else {
cout << "答错啦!" << endl;
return false;
}
}
bool sub(uint a, uint b) {
uint answer = a - b;
uint userinput;
cout << "%d - %d = " << a, b;
cin >> userinput;
if (userinput == answer) {
cout << "答对啦!" << endl;
return true;
}
else {
cout << "答错啦!" << endl;
return false;
}
}
bool mcl(uint a, uint b) {
uint answer = a * b;
uint userinput;
cout << "%d × %d = " << a, b;
cin >> userinput;
if (userinput == answer) {
cout << "答对啦!" << endl;
return true;
}
else {
cout << "答错啦!" << endl;
return false;
}
}
bool div(uint a, uint b) {
uint answer = a / b;
uint userinput;
cout << "%d ÷ %d = " << a, b;
cin >> userinput;
if (userinput == answer) {
cout << "答对啦!" << endl;
return true;
}
else {
cout << "答错啦!" << endl;
return false;
}
}
bool writeInfo(pcchar file, Account *account) {
FILE *fp = NULL;
if ((fp = fopen(file, "w")) == NULL) {
cout << "打开文件失败:: %s" << errno, strerror(errno);
return false;
}
fwrite(account, sizeof(Account), 1, fp);
return true;
}
bool readInfo(pcchar file, Account *account) {
FILE *fp = NULL;
char choose;
if ((fp = fopen(file, "r")) == NULL) {
cout << "打开文件失败:: %s" << errno, strerror(errno);
return false;
}
cout << "确定覆盖现在的信息吗?【Y/n】";
cin >> choose;
switch (choose) {
case 'Y':
case 'y':
break;
case 'N':
case 'n':
cout << "用户取消了操作。" << endl;
return true;
default:
cout << "请认真输入!" << endl;
return true;
}
fread(account, sizeof(Account), 1, fp);
return true;
}
编译结果:
C:\Program Files\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32\bin\ld.exe C:\Users\hzk20\AppData\Local\Temp\cc05c8yf.o:口算.cpp:(.text+0x11b9): undefined reference to `readInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
C:\Program Files\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32\bin\ld.exe C:\Users\hzk20\AppData\Local\Temp\cc05c8yf.o:口算.cpp:(.text+0x128b): undefined reference to `writeInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
C:\Users\hzk20\Desktop\collect2.exe ld returned 1 exit status
. 什么玩意还不能私自编译
不能私自编译还发出来
直接去问某一个人得了 我怎么看都没发现问题,给这个代码做了个语法检查(没编译),没有问题 zhangjinxuan 发表于 2022-12-28 08:51
我怎么看都没发现问题,给这个代码做了个语法检查(没编译),没有问题
cout << "%d ÷ %d = " << a, b; // 这是什么意思呢?我表示看不懂
他代码里面随处可见的都是类似的低级错误,而且就这破玩意,还不让别人编译
他把这代码当宝贝,在别人看来,这破玩意有什么用,谁稀罕要呢
他这哪是在提问题呀
你的语法检查没有检查出问题吗?
把你的语法检查的警告等级开到最大,然后再看看
zhangjinxuan 发表于 2022-12-28 08:51
我怎么看都没发现问题,给这个代码做了个语法检查(没编译),没有问题
我又稍微看了一下,我就不知道他这代码怎么写出来的
你看他的 readInfo 函数
31 行声明的 bool readInfo(string file, Account *account);
507 行定义的 bool readInfo(pcchar file, Account *account)
还有36行的这个 Account *account = (Account *)malloc(1024); 人造人 发表于 2022-12-28 09:37
我又稍微看了一下,我就不知道他这代码怎么写出来的
你看他的 readInfo 函数
我也觉得很奇怪{:10_277:}
页:
[1]
2