求助:C++程序报ISO C++禁止字符串常量转换为char*错误
错误原因与解决方案
核心错误原因
字符串常量(如"Jakarta")在C中是不可修改的,类型为const char*。你的inbus函数参数用了char*,直接传递字符串常量会违反ISO C标准——因为这会允许代码尝试修改只读的字符串常量,所以编译器抛出该错误。
修复步骤
1. 调整函数参数类型
将所有接收字符串常量的函数参数,从char*改为const char*,包括inbus、match、find、isbus、route等函数的输入参数。
2. 修正代码中的隐性bug
你的代码还有几个会导致编译失败或运行异常的问题,需要一并修复:
struct stack定义末尾多了一个冗余的};,导致语法错误inbus函数中bus[buspos].Jarak = Jrk;里的Jrk未初始化,应改为使用传入的Jarak参数push和pop函数的声明与定义参数不匹配:声明是int Jrk,定义写成了int* Jrk,需统一参数类型find函数中误用strcmp复制字符串,应改为strcpypop函数中错误地在弹出后执行Tos++;,会导致栈顶指针异常match函数的循环起始位置错误,应从buspos-1开始(buspos是下一个空位置的索引)
修复后的完整代码
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <iomanip> #include <ctype.h> #include <cstring> #define MAX 100 using namespace std; struct Transport { char KotaAwal[20]; // Kota asal char KotaAkhir[20]; // Kota tujuan int Jarak; // Jarak antar kota char Skip; }; struct stack { char KotaAwal[20]; // Kota asal char KotaAkhir[20]; // Kota tujuan int Jrk; // Jarak antar kota }; struct Transport bus[MAX]; // struct menggunakan jalur Bus struct stack btstack[MAX]; // struct menggunakan traking int buspos = 0; int Findpos = 0; int Tos = 0; // 初始化栈顶指针 void setup(); void route(const char *KotaAkhir); void inbus(const char *KotaAwal, const char *KotaAkhir, int Jarak); void push(const char *KotaAwal, const char *KotaAkhir, int Jrk); void pop(char *KotaAwal, char *KotaAkhir, int &Jrk); // 使用引用传递返回值 void isbus(const char *KotaAwal, const char *KotaAkhir); int find(const char *KotaAwal, char *AnyWhere); int match(const char *KotaAwal, const char *KotaAkhir); int main() { char KotaAwal[20], KotaAkhir[20]; setup(); cout << "Masukkan Perjalanannya" << endl; cout << "~~~~~~~~~~~~~~~~~~~~~~" << endl; cout << "Dari : "; cin >> KotaAwal; cout << "Ke : "; cin >> KotaAkhir; isbus(KotaAwal, KotaAkhir); route(KotaAkhir); cin.get(); cin.get(); // 多一次get避免程序直接退出 } void setup() { inbus("Jakarta", "Jogyakarta", 650); inbus("Jakarta", "Semarang", 450); inbus("Jakarta", "Malang", 850); inbus("Jogyakarta", "Malang", 300); inbus("Semarang", "Surabaya", 450); inbus("Semarang", "Jogyakarta", 75); inbus("Semarang", "Banyuwangi", 750); inbus("Malang", "Madiun", 250); inbus("Malang", "Nganjuk", 300); inbus("Malang", "Sidoarjo", 30); } void inbus(const char *KotaAwal, const char *KotaAkhir, int Jarak){ if(buspos < MAX) { strcpy(bus[buspos].KotaAwal, KotaAwal); strcpy(bus[buspos].KotaAkhir, KotaAkhir); bus[buspos].Jarak = Jarak; // 修正为传入的Jarak参数 bus[buspos].Skip = 0; buspos++; } else { cout << "Database Penuh" << endl; } } void isbus(const char *KotaAwal, const char *KotaAkhir) { int d, Jarak, Jrk; char AnyWhere[20]; if((d = match(KotaAwal, KotaAkhir)) != 0) { push(KotaAwal, KotaAkhir, d); return; } if((Jarak = find(KotaAwal, AnyWhere)) != 0) { push(KotaAwal, AnyWhere, Jarak); // 修正push的目标城市为中间站 isbus(AnyWhere, KotaAkhir); } else if(Tos > 0) { char tempAwal[20], tempAkhir[20]; pop(tempAwal, tempAkhir, Jrk); isbus(tempAwal, tempAkhir); } } // 检查两城市间是否有直达路线,返回距离 int match(const char *KotaAwal, const char *KotaAkhir) { int t; for(t = buspos - 1; t >= 0; t--) { // 修正循环起始位置 if((!strcmp(bus[t].KotaAwal, KotaAwal)) && (!strcmp(bus[t].KotaAkhir, KotaAkhir))) { return bus[t].Jarak; } } return 0; } // 查找从起点出发的未使用过的路线,返回距离并输出中间站 int find(const char *KotaAwal, char *AnyWhere) { Findpos = 0; while (Findpos < buspos) { if((!strcmp(bus[Findpos].KotaAwal, KotaAwal)) && (bus[Findpos].Skip == 0)) { strcpy(AnyWhere, bus[Findpos].KotaAkhir); // 修正为strcpy复制字符串 bus[Findpos].Skip = 1; return bus[Findpos].Jarak; } Findpos++; } return 0; } // 将路线信息压入栈 void push(const char *KotaAwal, const char *KotaAkhir, int Jrk) { if(Tos < MAX) { strcpy(btstack[Tos].KotaAwal, KotaAwal); strcpy(btstack[Tos].KotaAkhir, KotaAkhir); btstack[Tos].Jrk = Jrk; // 修正为操作栈结构体而非bus数组 Tos++; } else { cout << "Stack penuh !!!" << endl; } } // 从栈中弹出路线信息 void pop(char *KotaAwal, char *KotaAkhir, int &Jrk) { if(Tos > 0) { Tos--; strcpy(KotaAwal, btstack[Tos].KotaAwal); strcpy(KotaAkhir, btstack[Tos].KotaAkhir); Jrk = btstack[Tos].Jrk; } else { cout << "Stack kosong !!!" << endl; } } // 输出完整路线与总距离 void route(const char *KotaAkhir) { int Jrk = 0, t; t = 0; while(t < Tos) { cout << btstack[t].KotaAwal << " -> " << btstack[t].KotaAkhir << " (" << btstack[t].Jrk << " km)" << endl; Jrk += btstack[t].Jrk; t++; } cout << "Total jarak: " << Jrk << " km" << endl; }
关键修复点总结
- 所有接收字符串常量的参数改为
const char*,符合C++标准 - 修正结构体定义、变量初始化、函数参数匹配等语法错误
- 修复字符串复制、栈操作逻辑等运行时bug
- 优化路线输出格式,更清晰展示路径信息
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

