无法解析C++; 所以我不是新手,但是我对C++是新的。br> 我在Next()方法中遇到了这个错误,names[]数组高亮显示。有人能帮忙吗 typedef string text; text Next(text t); int main() { text rock; text names[10] = {"basalt", "dolomite", "granite", "gypsum", "limestone", "marble", "obsidian", "quartzite", "sandstone", "shale"}; cout<<"Enter a rock: \n"; cin>>rock; cout<<Next(rock); return 0; } text Next(text t) { for (int i = 0; i < 10; i++) { if (names[i].compare(t) == 0) { return names[i + 1]; } } return "Off boundary or not found"; } typedef字符串文本; 下一个文本(文本t); int main(){ 文字摇滚; 文本名称[10]={“玄武岩”、“白云石”、“花岗岩”、“石膏”、“石灰石”、“大理石”, “黑曜岩”、“石英岩”、“砂岩”、“页岩”}; 库特罗克; cout

无法解析C++; 所以我不是新手,但是我对C++是新的。br> 我在Next()方法中遇到了这个错误,names[]数组高亮显示。有人能帮忙吗 typedef string text; text Next(text t); int main() { text rock; text names[10] = {"basalt", "dolomite", "granite", "gypsum", "limestone", "marble", "obsidian", "quartzite", "sandstone", "shale"}; cout<<"Enter a rock: \n"; cin>>rock; cout<<Next(rock); return 0; } text Next(text t) { for (int i = 0; i < 10; i++) { if (names[i].compare(t) == 0) { return names[i + 1]; } } return "Off boundary or not found"; } typedef字符串文本; 下一个文本(文本t); int main(){ 文字摇滚; 文本名称[10]={“玄武岩”、“白云石”、“花岗岩”、“石膏”、“石灰石”、“大理石”, “黑曜岩”、“石英岩”、“砂岩”、“页岩”}; 库特罗克; cout,c++,netbeans,C++,Netbeans,试试这个,希望它能起作用。我在最后一行代码中添加了一些解释 typedef string text; text Next(text t, text namesArr[], int size); // modified int main() { text rock; text names[10] = {"basalt", "dolomite", "granite", "gypsum", "limestone", "marble", "obsidian", "quartzi

试试这个,希望它能起作用。我在最后一行代码中添加了一些解释

typedef string text;
text Next(text t, text namesArr[], int size);    // modified

int main() {

    text rock;
    text names[10] = {"basalt", "dolomite", "granite", "gypsum", "limestone", "marble", "obsidian", "quartzite", "sandstone", "shale"};
    cout<<"Enter a rock: \n";
    cin>>rock;
    cout<<Next(rock, names, 10);    // modified

    return 0;
}

text Next(text t, text namesArr[], int size) {    // modified
    for (int i = 0; i < size; i++) {
        if (namesArr[i].compare(t) == 0) {
            if (i==9) {
                return namesArr[0]; 
            }
            else {
                return namesArr[i + 1]; 
            }
        }
    }

    return "Not found"; // modified , it should never be out of bound since you specify the fixed array size, and also because you do the checking i==9
}
2.将
names[]
数组按数组大小传递给函数
这是在我的答案的开头完成的

重新阅读关于变量scopes的书籍或注释正如@KenWhite所建议的,names[]仅在main()函数中可用。您可以在int main()之前的最顶部声明names[]变量@ Drunke:我没有直接回答的原因是因为海报会通过分析来了解更多。我不相信你。变量范围不是C++独有的。你显然是新的编程。如果它是数组中的最后一个字符串,那么,如果代码>名称(9)。==0,然后您将尝试返回不是您的数组的
名称[10]
。然后您打算做什么?谢谢,伙计,第二个成功了,将名称[]声明为globaloh抱歉,请参阅我的更新答案,我在
下一个()中仍然使用
名称[]
时输入了
名称[]
功能。这两种功能现在都可以使用了。
typedef string text;
text Next(text t);

// defined globally here, any function can now access this array.
text names[10] = {"basalt", "dolomite", "granite", "gypsum", "limestone", "marble", "obsidian", "quartzite", "sandstone", "shale"};

int main() {
    // your original code here, without the "names[]" declaration.
}

text Next(text t) {
    // your code here, but with the "i==9" case checking to prevent out of bound
}