0

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'." 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();
}
2
  • The proper way to construct an element using a non-default ctor is ABC A(arr, carr, n, n1); If you write ABC A; A = ABC(...); you'll need a default ctor and an assignment operator. Commented Oct 8, 2013 at 10:54
  • You're not modifying n inside operator>>, so this loop while(n) will either not run at all or run infinitely. Commented Oct 8, 2013 at 11:04

1 Answer 1

2

Your error "invalid use of undefined type 'class ABC'" is because of your default constructor. You have declared a default constructor, but you have not defined it.

Instead of ABC(); you need to atleast do ABC() {}

Sign up to request clarification or add additional context in comments.

1 Comment

After wasting about 45 mins...phew!!.'P' should not be capital in 'Public'. But compiler wasn't giving any error for this. Remaining code is correct except 'n--' in while loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.