【发布时间】:2015-12-06 17:31:33
【问题描述】:
我有问题。我想使用在成员函数中生成的数组的单个元素进行操作,但它不起作用。这是我的代码:
using namespace std;
class Example
{
public:
int *pole;
void generate_pole();
};
void Example::generate_pole()
{
int *pole = new int [10];
for (int i = 0; i < 10; i++)
{
pole[i] = i;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Example reference;
reference.generate_pole();
cout << reference.pole[1] << endl; //there is the problem
system("pause");
return 0;
}
如何获得对元素的访问权限?真正的问题在哪里?谢谢!
【问题讨论】:
-
您在这里隐藏了成员变量:
int *pole = new int [10];将该部分更改为pole = new int [10]; -
然后打开你的编译器警告!
gcc -Wall -Wextra...
标签: c++ arrays function member