微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C++ 复制构造函数需要帮助且错误不断发生错误是在 'ex' 中请求 'ex',它是非类类型 'int*'

如何解决C++ 复制构造函数需要帮助且错误不断发生错误是在 'ex' 中请求 'ex',它是非类类型 'int*'

我是一名大学生,老实说,在数据结构方面苦苦挣扎,我可以在我的复制构造函数方面获得一些帮助。在这个实验中,我应该反转一个数组,但我目前真的被复制构造函数难住了,它应该构造一个新的 IntArray 对象,而 x 接收另一个 IntArray。

这就是呈现给我的内容,到目前为止,我设法使构造函数和析构函数关闭,但是每当我执行复制构造函数时,我都会收到错误消息:请求成员“长度”在“其他”中,这是非类类型“int*”或其他随机错误

#ifndef __INT_ARRAY_H__
#define __INT_ARRAY_H__

namespace ds {

/**
 * @brief Array of integers
 */
class IntArray {
private:
  /**
   * @brief The internal array of integers
   */
  int *storedArray; 

public:
  /**
   * @brief Length of array
   */
  int length;

  /**
   * @brief Construct a new IntArray object
   *
   * @param len length of the IntArray
   */
  IntArray(int len) {
    // Todo: initialize data members

    storedArray = new int[len];
    length = len;

  }

这是复制构造函数。到目前为止,我一直在研究它,但我无法理解它。如果有人能帮我解决这个问题,我将不胜感激。

  /**
   * @brief Construct a new IntArray object
   *
   * @param x another IntArray object
   */
  IntArray(const IntArray &x) {
    // Todo: complete the copy constructor

    int *other;
    int arr[length];
    length = other.length;
    

    for(int i = 0; i < length; i++)
    {
      arr[i] = x.storedArray[length];
    }
   
  }

  /**
   * @brief Destroy the IntArray object
   */
  ~IntArray() {
    // Todo: delete the internal array
    
    delete [] storedArray;

  }

如果有帮助,这是 main.cpp

#define CATCH_CONfig_MAIN
#include "IntArray.h"
#include "catch.hpp"
#include <cstdlib>
#include <sstream>

TEST_CASE("All") {
  // define an array of 10 ints
  ds::IntArray nums(10);

  // randomly add 10 ints to the array
  srand(time(0)); // setting the seed for rand()
  for (int i = 0; i < 10; i++) {
    nums[i] = rand() % 10 + 1; // generating random numbers by rand()
  }

  // print the array to a string stream
  std::stringstream originalSS;
  for (int i = 0; i < nums.length; i++) {
    originalSS << nums[i] << " ";
  }

  // test the copy constructor
  SECTION("copy") {
    // make a copy
    ds::IntArray copyArr(nums);
    // print the copy to a string stream
    std::stringstream copySS;
    for (int i = 0; i < copyArr.length; i++) {
      copySS << copyArr[i] << " ";
    }
    // compare the copy and the original
    CHECK(originalSS.str() == copySS.str());
  }

  // test the reverse function
  SECTION("Reverse") {
    // call reverse
    nums.reverse();
    // print the reverse to a string stream from back to front
    std::stringstream reverseSS;
    for (int i = nums.length - 1; i >= 0; i--) {
      reverseSS << nums[i] << " ";
    }
    // compare the original with the reversed
    CHECK(originalSS.str() == reverseSS.str());
  }
}


错误提示

error: request for member 'length' in 'other',which is of non-class type 'int*'
   44 |     length = other.length;

解决方法

在这行代码中,

int *other;
int arr[length];
length = other.length;

other 的类型为 int*。这正是错误告诉你的。也许您要找的是x.length

我在这里发现的另一件事是新创建的数组大小可能不是您需要的,因为 length 未初始化。所以我猜你的意思是,

length = x.length;
sortedArray = new int[length];    // Since its the copy constructor,you can directly allocate.

for(int i = 0; i < length; i++)
{
    sortedArray[i] = x.storedArray[i];    // Here x.storedArray should be indexed using 'i'. If not you might be asking for undefined behavior and will not copy data properly.
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。