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;
}
|