从同一类的另一个方法调用数组2D-C++
Call Array 2D From Another Method Same Class - C++
我有代码:
#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 ‘<unresolved overloaded function type>[int]’ for array subscript
coba.cpp:34:18: error: invalid types ‘<unresolved overloaded function type>[int]’ for array subscript
有什么错误?请给我点击以解决代码
thx
string notebook[][8]是方法的本地变量,您需要传递一个引用,或者只为类提供一个私有的notebook[][]变量。
notebook[1][3]
notebook[2][3]
以上内容不在printNotebook的范围内定义为
string notebook[][8]
在notebook()方法结束后超出范围。
编辑:确保您重命名它,因为您不能拥有具有相同名称的方法和变量成员
再次编辑:这里有一些示例代码可以让您的示例站稳脚跟,这可能是NOT最简单或最好的方法,但确实编译并工作。
#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
};
相关文章:
- C++从另一个类访问公共静态向量的正确方法是什么
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 在另一个类视图中添加最多2个图表的正确方法是什么
- 检查哪个对象调用了另一个对象的对象方法
- 不能将方法返回的值用于另一个方法
- Qt将信号与另一个类方法连接
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 将一个类的方法指针存储到另一个类中
- 从另一个标头中的标头调用静态方法
- 无法"this"指针传递到另一个类并在 CPP 中调用该类的任何方法
- C++ |DLL / EXE - 如何从导出的类调用另一个类方法?
- 在销毁期间从另一个线程调用对象上调用方法是否未定义行为?
- 如果类"A"具有指向另一个类的指针,则"B" A 可以访问 B 类公共方法
- 如何在另一个类中使用类成员的正确方法?
- 重写另一个方法 [C++] 使用的超类回调函数
- 如何构造一个类型特征,可以判断一个类型的私有方法是否可以在另一个类型的构造函数中调用?
- 如何启用使用另一个类的静态方法的模板函数的自动推导,从而消除冗长的调用方代码
- 覆盖作为另一个类的好友的虚拟受保护方法
- 将vector<vector<double>>从x86平台中创建的一个进程发送到x64中构建的另一个进程的最快方法是什么
- 返回一个引用C++中另一个类对象的对象的有效方法