0

我是 C++ 新手,请帮助我。非常感谢。

我想将一个(新类型数组)传递给一个类。但我收到消息“抛出 bad_alloc 实例后调用 c++ 终止”这是什么意思?再次感谢!!!!

#include <iostream>

using namespace std;

class test {
  public:
      test (int*, int);
       void check ();
  private :
    int k;
     int *a= new int [k];
//int a;

};


int main()
{
  //int a1=5,n=4;
  int n=4;
  int *a1= new int[n];
   //int a1[4]={1,2,3,4};
  test haha(a1,n);
  haha.check();

    return 0;
}


test::test(int *aa, int kk){
a=aa;
k=kk;
}

void test::check()
{
 for(int i=0; i<k; i++){
    cout<<a<<" ";

 }

}
4

5 回答 5

1
class test {
  public:
      test (int*, int);
       void check ();
  private :
    int k;
     int *a= new int [k]; <--- problem, k is undefined + you don't allocate in class declerations
//int a;

};

您不能在类声明中分配..尤其是未定义的成员:)

除此之外..你已经在c'tor中分配了指针(不是你给他们任何值......)

于 2013-03-28T16:27:05.723 回答
1

这些都是您的代码存在的所有问题,按出现顺序列出。我还为您提供了一些提示:

  1. 在 C++11 之前的代码中不允许对非静态数据成员进行类内初始化。因此这一行是无效的:

    private:
    // ...
        int *a = new int [k];
    //         ^^^^^^^^^^^^^^
    

    不仅如此,这条线也是多余的。您已经有一个带有指针的构造函数,并将该指针分配给a. 所以不需要在课堂上进行作业。

  2. 你的构造函数test应该在上面定义main。另外,提示:您应该使用成员 initializer-list初始化您的成员。在您的代码中看起来像这样:

    test::test(int *aa, int kk) : k(kk), a(aa) {}
    //                          ^^^^^^^^^^^^^^
    

    冒号后面是成员的初始化,每个成员用冒号分隔。

  3. 以下行打印时间地址a k

    cout << a << " ";
    

    你不想这样做。您打印一个十六进制数字,表示您的指针指向的内存地址。要打印指针指向的值,您必须取消引用指针:

    cout << *a << " ";
    

    注意:您尚未使用任何值初始化数组,因此将打印的所有内容都是堆栈中的垃圾。要为数组分配一些值,您可以这样做(在声明数组的 main 内)

    int *a1 = new int[n];
    
    a1[0] = 1;
    a1[1] = 2;
    a1[2] = 3;
    a1[3] = 4
    

    或者,您可以使用 for 循环:

    for (int i = 1; i <= 4; ++i)
    {
        a1[i - 1] = i;
    }
    

    或者,如果您使用 C++11 进行编译(您可能不是):

    int *a1 = new int[n] {1, 2, 3, 4};
    

    delete[]但最重要的是,当你完成使用它时,不要忘记你的数组。new您使用/创建的任何内容new[]都必须删除:

    delete[] a1;
    

    std::vector无论如何,您可以通过使用(恕我直言更好)来避免内存分配和混乱:

    #include <vector>
    
    std::vector<int> a1 = {1, 2, 3, 4};
    

    std::vector包含有关其大小的信息,这使得传递数组的长度变得不必要。使用向量将大大简化您的程序。

这是你的程序:

#include <iostream>
#include <vector>

class test {
  public:
      test(std::vector<int>);
      void check();

   private:
      std::vector<int> v;
};

test::test(std::vector<int> temp) : v(temp) {}

void test::check()
{
   std::vector<int>::const_iterator it;

   for (it = v.begin(); it != v.end(); ++it) {
      std::cout << *it << std::endl;
   }
}

int main()
{
  std::vector<int> a1 = {1, 2, 3, 4};

  test haha(a1);
  haha.check();
}

现场演示

于 2013-03-28T16:47:46.543 回答
0

您可能应该使用 std::vector 而不是动态内存分配;特别是考虑到您没有删除内存。

std::vector 非常易于使用,并且有大量可用的文档。请参阅http://en.cppreference.com/w/cpp/container/vector

例子

std::vector<int> qux;

qux.push_back(6);

void foo(std::vector<int>& bah)
{

}

如果你必须传递一个数组,你至少要使用 std::array,见 http://en.cppreference.com/w/cpp/container/array

错误

std::bad_alloc 是新操作符失败时抛出的异常

如果您不想让新的抛出该异常,您可以使用 std::nothrow,请参阅http://en.cppreference.com/w/cpp/memory/new/nothrow

您收到此消息的原因是因为您正在传递一个未初始化的变量

于 2013-03-28T16:35:49.147 回答
0
int k;
 int *a= new int [k];

这就是问题所在。创建类实例时, k 未初始化,您将其用作数组大小。

于 2013-03-28T16:28:15.670 回答
0

在你的课堂上:

 int *a= new int [k];

这将导致错误:因为 k 尚未定义。

error: `test::k' cannot appear in a constant-expression
error: `new' cannot appear in a constant-expression
error: ISO C++ forbids initialization of member `a'
error: making `a' static
error: invalid in-class initialization of static data member of non-integral type `int*' In constructor `test::test(int*, int)':

您应该= new int[k]从构造函数的声明中删除*a 并初始化*a

于 2013-03-28T16:29:43.770 回答