0

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;
        }
    
15
  • 3
    struct and class are just two different keywords used to declare a class. C++ has no structs and classes, but just classes. The only difference between struct and class is their default acces (private for class and public for struct) Commented Sep 7, 2018 at 9:14
  • 1
    please format your code properly. I dont want to do it myself, because there are typos in it and I dont want to fix them or make it worse... Commented Sep 7, 2018 at 9:16
  • 1
    This line seams strange : ` cout<< strC.setValue<<endl;` => setValue is a function Commented Sep 7, 2018 at 9:18
  • your code violates the Rule of 3/5/0 probably resulting in double delete Commented Sep 7, 2018 at 9:19
  • your implementation of setValue looks odd (in both versions of the code) you create a temporary set its values and then return it, instead setValue should set the values of this object Commented Sep 7, 2018 at 9:19

2 Answers 2

2

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.

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

Comments

1

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.

1 Comment

Thank all of you. I am in progress learning C++. Just read online on "this" pointer topic after seeing your codes. With your helps, I feel better as the support is there for me to progress and improve.

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.