在类函数调用期间丢失私有变量信息
losing private variable information during the class function calls
下面的代码在运行时将显示私有成员变量(MaxRows,MaxCols)在调用函数输入时正在更改。你能帮忙吗?
如您所见,第一个构造函数生成私有变量的正确显示。 但是,该函数会将它们分开。
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <windows.h>
#include <cstring>
#include <cctype>
#include <iomanip>
#include <algorithm>
#include <sstream>
using namespace std;
class TwoD
{
private:
int MaxRows;
int MaxCols;
double** outerArray;
public:
TwoD(int MaxRows, int MaxCols)
{
outerArray = new double *[MaxRows];
for (int i = 0; i < MaxRows; i++)
outerArray[i] = new double[MaxCols];
cout << MaxRows << MaxCols << endl;
}
void input()
{
cout << MaxRows << MaxCols << endl;
for (int k = 0; k < MaxRows; k++)
for (int j = 0; j < MaxCols; j++)
cin >> outerArray[k][j];
}
void outPut()
{
for (int l = 0; l < MaxRows; l++)
{
for (int m = 0; m < MaxCols; m++)
cout << outerArray[l][m] << " ";
cout << endl;
}
}
~TwoD()
{
for (int i = 0; i < MaxRows; i++)
delete[] outerArray[i];
delete[] outerArray;
}
};
int main()
{
TwoD example(5, 2);
example.input();
example.outPut();
return 0;
}
您从未实际将类成员设置为作为参数传递的值。
另外:您可能需要查看命名约定 - 通常使用maxRows
而不是MaxRows
TwoD(int maxRows, int maxCols) : MaxRows(maxRows), MaxCols(maxCols)
{
outerArray = new ...
正确命名您的类成员将在这里对您有所帮助。重命名成员并在构造类时初始化它们。
令人困惑的是,您的参数与您的成员命名相同。
TwoD(int maxRows, int maxCols)
: m_maxRows(maxRows)
, m_maxCols(maxCol)
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 在C++中,我想通过使用来自变量(例如字符串)的typename信息,从模板中定义一个类对象
- 如何将我的 cli 信息(变量等)共享到子进程,这是一个 bash shell 脚本
- 如何通过使用void中的变量中传递信息
- 在TXT文档的一部分中查找信息并将其存储在变量中
- 获取在GDB便利性变量中持有的价值的符号信息
- 使用 fstream 对象将文件中的信息存储到变量中
- 编译器如何以及在内存中的何处存储有关变量类型的信息
- 我可以在不使用全局变量的情况下将信息传递给函数指针回调吗?
- 调用函数变量/名称信息
- 在类函数调用期间丢失私有变量信息
- 将switch语句中的信息存储到变量中
- 如何创建一个具有全局变量的 lib 文件,这些全局变量应该将其信息共享给另一个项目
- 如果用调试信息编译,则通过其名称获取全局变量地址
- Qt:在一个int变量,我想保持颜色信息
- 将版本信息放入环境变量中以包含第三方库的路径是否合适?
- 关于函数类变量的更多信息
- 需要有关在ROS主题上发布双变量的信息
- 是否可以获取字节(或文本()变量的信息在WiFi中