user10293779
user10293779

Reputation: 103

Can return two arrays in struct but not in class C++

I can return two arrays by 'struct' with below codes; but can't translate the code to "class". the "class" code and error areattached also.

please shed lights on it. I have to use "class" and mutiple arrays in my project.

1) with "struct"

   struct strA{
   int *p;
   int *p1;
   };

   strA setValue(int n)
   {
     strA strB;
     strB.p=new int[n];
     strB.p1=new int[n];

      for (int i=0; i<n;i++)
      {
            strB.p[i]=i;
            strB.p1[i]=i*2;
      }
      return strB;
   }

   int main(){
      const int N=3;
      strA strC;
      strC=setValue (5);
      for (int i=0; i<N;i++)
      {
            cout<< strC.p[i]<<endl;
            cout<< strC.p1[i]<<endl;
      }
      return 0;
   }
  1. with "class". it turned out "error C3867: 'strA::setValue': function call missing argument list; use '&strA::setValue' to create a pointer to member"

    class strA{
    public:
      int *p;
      int *p1;
    public:
      strA();
      ~strA(){delete p, delete p1;}
      strA setValue(int n);
    };
    
    
     strA strA::setValue(int n)
     {
       strA strB;
       strB.p=new int[n];
       strB.p1=new int[n];  
       for(int i=0; i<n;i++)
       {
            strB.p[i]=i;
            strB.p1[i]=i*2;
       }
       return strB;
      }
    
     int main(){
        const int N=3;
        strA strC;
        strC.setValue (N);
        for (int i=0; i<N;i++)
        {
          cout<< strC.setValue<<endl;
          cout<< strC.p1[i]<<endl;
        }
        return 0;
        }
    

Upvotes: 0

Views: 96

Answers (2)

P.W
P.W

Reputation: 26800

I will first address the error you have mentioned. There are other issues with this code as well.

The error is because of this line in main:

cout<< strC.setValue<<endl;

setValue is a function and it has to be called with arguments like this:

strC.setValue(N);

Other issues:

  1. You cannot use cout to print the object returned from setValue unless you have overloaded the << operator for the class strA.
  2. In the setValue function you have defined an object strB and you assign memory to its members. This memory is not freed. What you are freeing are the members of the object strC defined in main. Look at the destructor of strA and you will understand.

The main in the first ("struct") version of the code can be used in the second ("class") version because p and p1 are public.

Upvotes: 2

TaQuangTu
TaQuangTu

Reputation: 2343

First, As the answer of P.W. You will meet a compile error in this line cout<< strC.setValue<<endl; because you forgot to pass argument for the function setValue(int n).

Second, it is not suitable idea to write setValue(int n) function as what you have written. I recommend to you to write the function as following:

void ::setValue(int n)
{
  this->p=new int[n];
  this->p1=new int[n];  
  for (int i=0; i<n;i++)
  {
    this->p[i]=i;
    this->p1[i]=i*2;
  }
}

I think you are newbie and you should read more about Object Oriented Programming.

Upvotes: 1

Related Questions