Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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++中创建了一个处理0和1数组的类。在私有属性中,我有一个大小、一个类型和一个指定大小的整数数组_C++ - Fatal编程技术网

C++;再次调用构造函数时修改对象的私有成员 我在C++中创建了一个处理0和1数组的类。在私有属性中,我有一个大小、一个类型和一个指定大小的整数数组

C++;再次调用构造函数时修改对象的私有成员 我在C++中创建了一个处理0和1数组的类。在私有属性中,我有一个大小、一个类型和一个指定大小的整数数组,c++,C++,我的问题是,每次我再次调用构造函数时,数组中的值都会被修改。在更详细的介绍中,我使用构造函数创建了第一个对象,然后创建了第二个对象,这样做会修改第一个对象 我尝试过使用指针,新的操作符,指向const对象的const指针,但没有任何效果!不管我选择的数组大小如何,总是数组的第三个、第六个、第九个等值被修改为大小值。 如有任何建议,我们将不胜感激 我的代码的一些摘录: class SArray { private: int SArray_Size; int DType; int Tab

我的问题是,每次我再次调用构造函数时,数组中的值都会被修改。在更详细的介绍中,我使用构造函数创建了第一个对象,然后创建了第二个对象,这样做会修改第一个对象

我尝试过使用指针,新的操作符,指向const对象的const指针,但没有任何效果!不管我选择的数组大小如何,总是数组的第三个、第六个、第九个等值被修改为大小值。 如有任何建议,我们将不胜感激

我的代码的一些摘录:

class SArray
{
private:
  int SArray_Size;
  int DType;
  int Table[];
public:
  //complete constructor
  SArray::SArray(const int& tsize, const int& ttype)
  {
    SArray_Size = tsize;
    DType = ttype;
    if (ttype == 0) //random array with integer values between 0 and 1
    {
      for (int i = 0; i < getSize(); i++) 
      {
        Table[i] = rand() % 2;
      }
    }
    if (ttype == 1) //default array with only 1s
    {
      for (int i = 0; i < getSize(); i++)
      {
        Table[i] = 1;
      }
    }
  }

};

int main()
{
  const int NbRes = 15;
  //reset the random number generator
  srand(time(0));
  const SArray test3(NbRes,1);
  (test3).print();
  const SArray test1(NbRes,1);
  (test1).print();
  (test3).print();
  return 0;
}
class-SArray
{
私人:
int-SArray_大小;
int-DType;
int表[];
公众:
//完全构造器
SArray::SArray(常量int&tsize、常量int&ttype)
{
SArray_尺寸=t尺寸;
DType=ttype;
if(ttype==0)//整数值介于0和1之间的随机数组
{
对于(int i=0;i
您必须为“表”分配内存。例如:

SArray::SArray(const int& tsize, const int& ttype)
{
    SArray_Size = tsize;
    DType = ttype;
    Table= new int[tsize];
    ...

不要忘记在析构函数中释放它。

您必须为“表”分配内存。例如:

SArray::SArray(const int& tsize, const int& ttype)
{
    SArray_Size = tsize;
    DType = ttype;
    Table= new int[tsize];
    ...

不要忘记在析构函数中释放它。

罪魁祸首是
int Table[]
-您没有指定表的大小

您真的应该用
std::vector Table替换它
并使用
tsize
对其进行初始化

例如:

#include <vector>

class SArray
{
  private:
    int DType;
    std::vector<int> Table;
  public:
    const size_t getSize() const { return Table.size(); }
  public:
    SArray::SArray(const int tsize, const int ttype) :
      DType(ttype), Table(tsize)
    {
      int i, n = getSize();
      switch( ttype )
      {
        case 0:
          for (i = 0; i < n; ++i) 
            Table[i] = rand() % 2;
          break;
        case 1:
          for (i = 0; i < n; ++i)
            Table[i] = 1;
          break;
      }
    }
 };
#包括
沙雷类
{
私人:
int-DType;
std::向量表;
公众:
const size_t getSize()const{return Table.size();}
公众:
SArray::SArray(常量int tsize,常量int ttype):
数据类型(ttype)、表格(tsize)
{
int i,n=getSize();
开关(ttype)
{
案例0:
对于(i=0;i
罪魁祸首是
int Table[]
-您尚未指定表的大小

您真的应该用
std::vector Table替换它
并使用
tsize
对其进行初始化

例如:

#include <vector>

class SArray
{
  private:
    int DType;
    std::vector<int> Table;
  public:
    const size_t getSize() const { return Table.size(); }
  public:
    SArray::SArray(const int tsize, const int ttype) :
      DType(ttype), Table(tsize)
    {
      int i, n = getSize();
      switch( ttype )
      {
        case 0:
          for (i = 0; i < n; ++i) 
            Table[i] = rand() % 2;
          break;
        case 1:
          for (i = 0; i < n; ++i)
            Table[i] = 1;
          break;
      }
    }
 };
#包括
沙雷类
{
私人:
int-DType;
std::向量表;
公众:
const size_t getSize()const{return Table.size();}
公众:
SArray::SArray(常量int tsize,常量int ttype):
数据类型(ttype)、表格(tsize)
{
int i,n=getSize();
开关(ttype)
{
案例0:
对于(i=0;i
能否发布完整的测试用例?如果我们看不到相关的代码,很难猜测问题出在哪里。这不是一个真正的bug,但是为什么要编写
(test3).print()
而不是仅仅
test3.print()?额外的支架看起来很奇怪…你能发布一个完整的测试用例吗?如果我们看不到相关的代码,很难猜测问题出在哪里。这不是一个真正的bug,但是为什么要编写
(test3).print()
而不是仅仅
test3.print()?额外的大括号在我看来很奇怪……或者更好,你用的是
std::vector
@OliCharlesworth:当然,我错了。所以有些东西不见了…谢谢!我现在意识到了错误。我在某个地方读到数组是邪恶的,可能是真的!:-)或者更好的是,你用了一个
std::vector
@OliCharlesworth:绝对是我的错。所以有些东西不见了…谢谢!我现在意识到了错误。我在某个地方读到数组是邪恶的,可能是真的!:-)