简体   繁体   English

在参数化构造函数中将数组作为参数传递

[英]Passing array as argument in Parameterized Constructor

I am trying to overload the >> operator to rotate the elements in the array.I have removed the array declaration and initialization for shorten the code. 我试图重载>>运算符以旋转数组中的元素。我删除了数组声明和初始化以缩短代码。 But compiler is giving error as "invalid use of undefined type 'class ABC'." 但是编译器给出错误为“无效使用未定义类型'ABC类'”。 and many more. 还有很多。

#include<iostream>
using namespace std;
class ABC
{
      int iarr[10],n2,n3;
      char carr[10];

      Public:
             ABC();
      ABC(int arr[],char car[],int n,int n1)
      {
              n2=n; n3=n1;
              for(int i=0;i<n;i++)
              iarr[i]=arr[i];
              for(int i=0;i<n1;i++)
                      carr[i]=car[i];
      }

      ABC operator>>(int n)
      {
          while(n)
          {
                  int temp;
                  temp=iarr[n2-1];
                  for(int i=n2-1;i>=0;i--)
                  {
                      iarr[i]=iarr[i-1];
                  }
                  iarr[0]=temp;
          }
      }

      void display()
      {
          for(int i=0;i<n2;i++)
              printf("%d\t",iarr[i]);
          printf("\n");
          for(int i=0;i<n3;i++)
              printf("%d\t",carr[i]);
      }
};

main()
{
    ABC A;
    A=ABC(arr,car,n,n1);
    A.display();
}

Your error " invalid use of undefined type 'class ABC' " is because of your default constructor. 您的错误“ 无效使用未定义类型'class ABC'的类型 ”是由于您的默认构造函数。 You have declared a default constructor, but you have not defined it. 您已经声明了默认构造函数,但尚未定义它。

Instead of ABC(); 代替ABC(); you need to atleast do ABC() {} 您需要至少做ABC() {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM