Hello,
I'm having an error in my code plz someone solve my problem. I just want to multiply the element # 1 of an object array from the element # 2 of another object of same class and so on. Plz help me. Thx

Error: int MyCLass::getValue():
invalid conversion from 'int*' to 'int'

my code is:

//---------------------------------------------------------------------------

#include <iostream>//header file for input/output


using namespace std;


//---------------------------------------------------------------------------




//---------------------------------------------------------------------------
//class declaration and definition

class MyClass
{

      //declare friend function of the class
      friend MyClass operator*(const MyClass& x, const MyClass& y);



      private:

              int value[10];//data member of the class


      public:

             //declare and define default constructor
             MyClass()
             { 
               for(int i = 0; i < 10; i++)
               {
                     value[i] = 0;//initialize array to 0 using for loop
               }
             }

             //destructor of the class
             ~MyClass() {};


             //define set function which initialize the array 
             void setValue(int v[]) 
             { 
                     value[9] = v[9];        
                     for (int i = 1; i <= 10; i++)
                     {
                         v[i] = i;

                     }
             }

             int getValue() { return value; }//define get function 




           //define function which shows the results after multiplication of both arrays  
           MyClass show()
           {

                for(int i = 1; i <= 10; i++)
                        cout << value[i] << endl;

           }

};//end of class definition

//---------------------------------------------------------------------------
//define the friend function of the class which multiplies two arrays

MyClass operator*(const MyClass& x, const MyClass& y)
{
                    MyClass tmp;


                    for(int i = 0; i < 10; i++)

                    tmp.value[i] = x.value[i] * y.value[i];

                    return tmp;

}



//---------------------------------------------------------------------------

//main function of the program
int main()

{

    MyClass a, b, c;


    cout << "Object 1 :" << endl;
    b.show();

    cout << "\nObject 2 :" << endl;
    c.show();

    a = b * c;//calling overloaded operator '*'

    cout << "\nMultiplication of both objects:" << endl;

    a.show();//show the results




    cin.get();

    return 0;//terminate the program successfully

}

//---------------------------------------------------------------------------

First of all, use code tags to paste code...

So let's look at your error, it is complaining that it cannot convert an int* to an int. And the error occurs in getValue.

getValue is declared to return an 'int', and in the function you return 'value' which is declared as int value[10];

What you should read up about are pointers and arrays. The value variable will store 10 int values.. so it may look like;

value[0] = 10;
value[1] = 3;
...
value[9] = 2;

So when you want to access _one_ of these elements, you use 'value[ i ]' where i is the index of the value you want to access. Now if you use just 'value' without any array-index it means 'pointer to the first element of the array' so of type 'int*'.

In the getValue function you do return value; so it is trying to return an int* but getValue should return int... that's an impossible implicit conversion.

Instead, your getValue should return something like value[0] or value[2] depending on which element of the array you want to return.

Are you trying to get the whole array or just a single element of the array from getValue(). Either way, you'll have to at least change the declaration and potentially the implementation.

If you want the array, you have to return the pointer:

int[B]*[/B] gimmeTheArray() {
  return arrayName;
}

If you want a specific element, you must index it:

int gimmeAnElement ([B]int index[/B]) {
  return arrayName[B][index][/B];
}

EDIT:
As long as I have your attention, I want to share a friendly warning. Wherever you are working with your value[] array, you are hard coding the element count as 10. This works, but really isn't a good idea, it's a maintenance nightmare. What happens if you need to change the size of the array from 10 to 20, or even worse, from 10 to 5? You have to search through your program looking for every place the array is used and change your 10 to the new value. What happens if you miss one, especially if the array gets smaller? Either A, you start getting alot of garbage, or B, you start crashing all the time.

To correct this, you will want to define a constant of some sort then use the constant every time you access the array.

This is what you did:

#include <iostream>
using std::cout;
using std::endl;
int main() {
  int myArray[10] = {0};

  for (int i = 0; i < 10; i++) {
    myArray[i] = i * 2;
    cout << myArray[i] << endl;
  }
  return 0;
}

This is what I suggest:

#include <iostream>
using std::cout;
using std::endl;
int main() {
  const int ARRAY_SIZE = 10;

  int myArray[ARRAY_SIZE] = {0};

  for (int i = 0; i < ARRAY_SIZE; i++) {
    myArray[i] = i * 2;
    cout << myArray[i] << endl;
  }
  return 0;
}

Do you see the difference? In yours you have to change the 10 on lines 1 and 3. In mine, it only needs to be changed on Line 1.

What happens if you miss an instance of the 10?

//WARNING: THIS CODE WILL CAUSE CRASHES!!!!
#include <iostream>
using std::cout;
using std::endl;
int main() {
  int myArray[5] = {0};

  for (int i = 0; i < 10; i++) { //<---this 10 got missed
    myArray[i] = i * 2;
    cout << myArray[i] << endl;
  }
  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.