-2

我有代码:

    #include <iostream>

using namespace std;

class tokoKomputer
{
    public:
        void notebook();
        void printNotebook();
};

void tokoKomputer::notebook()
{
    string notebook[][8]=
    {
        {"MERK", "NO SERI", "HARGA", "STOK", "MEMORY", "HDD", "GPU", "DISPLAY"},
        {"Asus", "ASN0002", "2500000", "9", "1GB", "250GB", "128MB", "10"},
        {"Fujitsu", "FJN0001", "5500000", "12", "1GB", "320GB", "256MB", "14"},
        {"Fujitsu", "FJN0005", "6500000", "4", "4GB", "250GB", "1GB", "14"}
    };  
}

void tokoKomputer::printNotebook()
{
    cout<<notebook[1][3]<<endl;
    cout<<notebook[2][3]<<endl;
}

int main()
{
    tokoKomputer run;
    run.printNotebook;
}

但是,如果我编译代码 ubuntu 终端总是给我消息

coba.cpp:33:18: error: invalid types ‘&lt;unresolved overloaded function type>[int]’ for array subscript
coba.cpp:34:18: error: invalid types ‘&lt;unresolved overloaded function type>[int]’ for array subscript

有什么错误?请给我解决代码

谢谢

4

1 回答 1

2

字符串 notebook[][8] 对于您的方法来说是本地的,您要么需要传递一个引用,要么只为您的班级提供一个私有 notebook[][] 变量。

notebook[1][3]
notebook[2][3]

以上不属于 printNotebook 范围内的定义为

string notebook[][8]

notebook() 方法结束后超出范围。

编辑:确保重命名它,因为你不能有同名的方法和变量成员

再次编辑:这里有一些示例代码可以让您的示例站起来,这可能根本不是最简单或最好的方法,但它确实可以编译和工作。

#include <iostream>
#include <string>
using namespace std;

class tokoKomputer
{
    public:
        void notebook();
        void printNotebook();
        string myNotebook[4][8];  
};

void tokoKomputer::notebook()
{
    string myTempNotebook[4][8] = {
        {"MERK", "NO SERI", "HARGA", "STOK", "MEMORY", "HDD", "GPU", "DISPLAY"},
        {"Asus", "ASN0002", "2500000", "9", "1GB", "250GB", "128MB", "10"},
        {"Fujitsu", "FJN0001", "5500000", "12", "1GB", "320GB", "256MB", "14"},
        {"Fujitsu", "FJN0005", "6500000", "4", "4GB", "250GB", "1GB", "14"}
    };  // This syntax will only work for initializing an array, not setting it later

    for (int i = 0; i <= 3; i++)
    {
        for (int j = 0; j <= 7; j++)
        {
            myNotebook[i][j] = myTempNotebook[i][j];
        }
    }

};

void tokoKomputer::printNotebook()
{
    cout << myNotebook[1][3] << endl;
    cout << myNotebook[2][3] << endl;
};

int main()
{
    tokoKomputer run;
    run.notebook();
    run.printNotebook();
    string hello;
    cin >> hello;  // this was just here to keep console open
};
于 2013-07-19T15:48:57.427 回答