-4
#include <iostream>
using namespace std;

class VectorND
{
public:
    int N;
    VectorND(int n)
        :N(n) {}

    float *vec_value = new float[N];

    void assignValue(int i, float v)
        {
            vec_value[i] = v;
        }

};

This is my code and I'm trying to solve this problem, but I do not know what to do.

Problem 4. Implement operator overloading of VectorND class for the following operations.

my_vec.assignValue(3) = 10;

std::cout << my_vec(3);

Example

int main(void)
{
  VectorND my_vec(10);
    my_vec.assignValue(3) = 10;
    std::cout << my_vec(3);

    return 0;
}

Output : 10
user0042
  • 7,917
  • 3
  • 24
  • 39
DW7
  • 43
  • 1
  • 5

1 Answers1

-1

For the first operator overload you have to do something like this:

float& operator () (int i)
{
    return vec_value[i];
}

When you do myVec(3) = 4;, the myVec(3) returns a reference to the float at the index 3 which you then assign to 4.

For myVec.assign(3) = 10 you should overload the assign method to take an index and return a reference to a float for that index.

For the second one it is more complicated you have to do a declaration in your class friend ostream& operator << (ostream& n, VectorND& myVec);

Then outside the class:

ostream& operator<<(ostream& n, const VectorND& myVec)
{
   // This just pushes all the values to the stream
   for(int i = 0; i< N; i++) cout<<vec_value[i]<<endl;
}
Jake Freeman
  • 1,700
  • 1
  • 8
  • 15