classes and assignment operators


A newbie learning C++. After learning about classes and assuming had a good understanding of operators, I wrote a program. I'm stumped at this point. I would like some direction. Thank you.

The idea of the program.

- Ask user for 'n' number of data.
- After the user provides the data, arrange and sort the data into 10
groups(blocks)
- Each group(block) is a multiple of 10. Therefore numbers from 0-9 will be
in group 1, numbers 10-19 will be in group 2 and so on..

The error:

I get an error which says ' No viable overloaded "=" '. i understand that this is due to the lack of an assignment operator.

However , i do have two questions.
1. Why is an assignment operator an absolute necessity ? Can I not just not use an equal to (=) sign to assign a value to a variable?
2. I am not sure how to an assignment operator would be used at this stage. Could someone help me out please ?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <math.h>

using namespace std;

#define arraycount 10

class Testmath{
public:
    int *potr;
    void Calculate(int n);
    
    int operator=(Testmath& obj){
 
    }
    
} Blocks[arraycount];

void Testmath::Calculate(int n){
    int m ;
    for (int i = 0; i<n; i++) {
        m = potr[i] / 10;
        Blocks[i] = m;     // The error appears here. 
    }
}

int main(int argc, const char * argv[]) {
    
    int a;
    Testmath collect;
    
    cout << "Enter the total number of data that you would like to input:";
    cin >> a;
    
    collect.potr = new int[a];
    
    for (int j = 0; j < a; j++) {
        cout << "Enter number " << j+1 << " : " << endl;
        cin >> collect.potr[j];
    }
    
    collect.Calculate(a);
    
    return 0;
}
Last edited on
1) Line 23: Blocks is an array of Testmath objects. You can't assign an int to a Testmath object without Testmath having an operator = that accepts an int.

2) I can give you the syntax for an operator = that takes an int, but I don't understand what such an operator is supposed to do.
1
2
3
4
Testmath Tesxtmath::operator = (int val)
{ // ???
   return Testmath (???);
}

Last edited on
Ahh. I understand.

Now to answer point 2, I merely would like to store the int value to the object "Blocks". Therefore it seems like my operator should facilitate an operation where an int is stored in an object.

With reference to this program, I would like to store 'm' from line 22 to "Blocks" which is an array of objects. As you pointed out, I have to make an assignment operator which will facilitate this process. using your syntax for an operator, I changed the following section (adding an operator), hoping i can assign an int value to an object.

class Testmath{
public:
int *potr;
void Calculate(int n);

int getvalue() const
{return *potr;}

// assignment operator
int operator=( const Testmath& obj)
{ return obj.getvalue();}

} Blocks[arraycount];

void Testmath::Calculate(int n){
int m ;
for (int i = 0; i<n; i++) {
m = potr[i] / 10;
Blocks[i] = m; // The error still occurs here
}
}

But I still get the same error "No viable overloaded operator = " . What would be an alternative option to store the int value to an object array?

Why is an assignment operator an absolute necessity ?
It is not necessary and doesn't make sense for this exercise.

potr is the fixed size array
-> int potr[arraycount]

Blocks is variable and needs to be calculated from a
-> Testmath *Blocks = new Testmath[a / arraycount];

This is according to the requirements.


When the user enters a number the first step is to calculate the two indexes of the respective arrays. This is done by division (/) and modulo (%) of arraycount.

In Testmath you need a second member variable that tells how many variables are stored.

What are you trying to achieve in Calculate?


What I am trying to achieve in Calculate is this.

If the user enters the number 43.

Then 43/10 = 4.3. but as an integer I'll get 4. Therefore I'd like to store this in the block 4. Therefore I wrote,

m = potr[i] / 10;
Blocks[i] = m; // The error appears here.

But as I know now, I can't do that, because an int cannot be assigned to an object. However I believe if I use an assignment operator, I could make that happen. But not sure how.
I know I could do this same calculation in an easier way if I wanted to. But I wanted to implement classes and array blocks. This is an exercise I just cooked up to test my ability and I have come undone, so trying to find my way around while learning how to use classes and operators better.
Then 43/10 = 4.3. but as an integer I'll get 4. Therefore I'd like to store this in the block 4.
This is correct, but your conclusion is wrong. m is the index of the block.

The true expression is:

1
2
3
m = n / 10;
Blocks[m].potr[Blocks[m].number_of_stored_values] = n;
Blocks[m].number_of_stored_values += 1;


It does not appear within Calculate(...) but the loop.
Thank you.

Out of curiosity, IF I persist with the calculate function (while it may not be the ideal solution), would it be possible to design a calculate function like this?

void Testmath::Calculate(int n){
int m ;
for (int i = 0; i<n; i++) {
m = potr[i] / 10;
Blocks[m] = potr[i]; // THe error is "NO viable overloaded '='"
}
}

Obviously to overcome the error, i'll have to design an assignment operator function. But if it is possible, how could I write an operator function that will allow me to call the object from the main function and let me assign the value to blocks ..

Something along the lines of this ..

// assignment operator
int operator=( const Testmath& obj)

// Not sure how , but here i call either a reference to the object or pointer to // the object and then if I return the int value, could I overcome the above
// error?

{ return ???;}




In order to make a bit sense it would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Testmath{
public:
    int *potr;
    int number_of_stored_values;

    Testmath() : potr(0), number_of_stored_values(0)
    {
    }

    void Calculate(int n);
    
    Testmath &operator=(int n){

      if(potr == 0)
        potr = new int[...]; // either a big enough value or you need to copy and reallocate the buffer each time
      potr[number_of_stored_values] = n;
      number_of_stored_values += 1;

      return *this;
    }
    
} Blocks[arraycount];
Thank you very much. As flawed as my code is, I was just trying to see what and how I'd have to manipulate classes to access different variables. Appreciate your help.
Topic archived. No new replies allowed.