Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在类函数调用期间丢失私有变量信息_C++ - Fatal编程技术网

C++ 在类函数调用期间丢失私有变量信息

C++ 在类函数调用期间丢失私有变量信息,c++,C++,在运行时,下面的代码将显示私有成员变量(MaxRows、MaxCols)随着调用函数输入的时间而变化。你能帮我做点什么吗 如您所见,第一个构造函数生成私有变量的正确显示。但是,该函数会将它们分开 #include <iostream> #include <string> #include <fstream> #include <cmath> #include <cstdlib> #include <ctime> #inclu

在运行时,下面的代码将显示私有成员变量(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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
二班
{
私人:
int最大行;
int-MaxCols;
双**外射线;
公众:
TwoD(int-MaxRows,int-MaxCols)
{
outerArray=新的双*[MaxRows];
对于(int i=0;icout您实际上从未将类成员设置为作为参数传递的值

另外:您可能希望查看您的命名约定-通常使用
maxRows
而不是
maxRows

TwoD(int maxRows, int maxCols) : MaxRows(maxRows), MaxCols(maxCols)
    {
        outerArray = new ...

实际上,您从未将类成员设置为作为参数传递的值

另外:您可能希望查看您的命名约定-通常使用
maxRows
而不是
maxRows

TwoD(int maxRows, int maxCols) : MaxRows(maxRows), MaxCols(maxCols)
    {
        outerArray = new ...

适当地命名类成员将有助于您。请重命名您的成员,并在构建类时对其进行初始化

您的参数与您的成员的名称相同,这令人困惑

TwoD(int maxRows, int maxCols)
    : m_maxRows(maxRows)
    , m_maxCols(maxCol)

适当地命名类成员将有助于您。请重命名您的成员,并在构建类时对其进行初始化

您的参数与您的成员的名称相同,这令人困惑

TwoD(int maxRows, int maxCols)
    : m_maxRows(maxRows)
    , m_maxCols(maxCol)

除非您感到严重受虐狂,否则请围绕
向量
构建您的作为包装器。我还没有学习这一部分。我想解决这个问题并了解发生了什么。MaxRows和MaxCols从未初始化。除非您感到严重受虐狂,否则请围绕
向量
构建您的作为包装器。我没有我想解决这个问题并了解发生了什么。MaxRows和MaxCols从未初始化。谢谢你的帮助..我添加了…TwoD(inta,intb){outerArray=new double*[MaxRows];for(inti=0;IMaxRows
MaxCols
从未设置。但是“TwoD(inta,intb)”不会流入“MaxRows=a;MaxCols=b;”为了更新类成员MaxRows和MaxCols?我想我可以关闭这个案例,因为我已经让它工作了。你能告诉我如何在Stackoverflow中关闭一个案例吗?我很感谢你的指导。我还尝试添加了“MaxRows=MaxRows;MaxCols=MaxCols;对于构造函数,它似乎也在工作。太好了!感谢您的帮助..我添加了…TwoD(inta,intb){outerArray=new double*[MaxRows];for(inti=0;IMaxRows
MaxCols
从未设置。但是“TwoD(inta,intb)”不会流入“MaxRows=a;MaxCols=b;”为了更新类成员MaxRows和MaxCols?我想我可以关闭这个案例,因为我让它工作了。你能告诉我如何在Stackoverflow中关闭一个案例吗?我很感激这些说明。我还尝试为构造函数添加了“MaxRows=MaxRows;MaxCols=MaxCols;,它似乎也在工作。太棒了!