Hello, I am making a code with templates, and when I compile it I get the following error:
main.cpp: In function ‘int main()’:
main.cpp:41:28: error: ‘class number<char, 0, 10>’ has no member named ‘make_op’
case '+': number_T.make_op(operacion);break;
^~~~~~~
main.cpp:42:28: error: ‘class number<char, 0, 10>’ has no member named ‘make_op’
case '-': number_T.make_op(operacion);break;
^~~~~~~
main.cpp:43:28: error: ‘class number<char, 0, 10>’ has no member named ‘make_op’
case '*': number_T.make_op(operacion);break;
^~~~~~~
main.cpp:44:28: error: ‘class number<char, 0, 10>’ has no member named ‘make_op’
case '/': number_T.make_op(operacion);break;
^~~~~~~
main.cpp:45:28: error: ‘class number<char, 0, 10>’ has no member named ‘make_op’
case '=': number_T.make_op(operacion);break;
^~~~~~~
main.cpp:46:28: error: ‘class number<char, 0, 10>’ has no member named ‘make_op’
case '<': number_T.make_op(operacion);break;
^~~~~~~
main.cpp:47:28: error: ‘class number<char, 0, 10>’ has no member named ‘make_op’
case '>': number_T.make_op(operacion);break;
^~~~~~~
The code in the main that you mention is the following:
switch(operacion){ // ejecutamos las operaciones
case '+': number_T.make_op(operacion);break;
case '-': number_T.make_op(operacion);break;
case '*': number_T.make_op(operacion);break;
case '/': number_T.make_op(operacion);break;
case '=': number_T.make_op(operacion);break;
case '<': number_T.make_op(operacion);break;
case '>': number_T.make_op(operacion);break;
}
And the file that contains the object and the method is the following:
#include <iostream>
#include <cmath>
using namespace std;
template < class T, size_t N, size_t B >
class number{
private:
T* digitos;
int index;
int base;
int val1,val2;
int final=0;
int size;
public:
void s_base(int i){
base=i;
}
void set_vals(int a, int b){
val1=a;
val2=b;
}
number(T valor):digitos(NULL){
resize_v(N);
int sz = sizeof(valor);
resize_v(sz);
}
void resize_v(int sz){
digitos = new T [sz];
size=sz;
index = sz -1;
}
int longitud(int number) const{
return ceil(log(number)/log(base)); // usa la libreria math.h
}
void set_base(int n ){
if(n>=base){
int result = n / base; // obtenemos el valor resultado de la divicion, para la llamada recursiva
int rest = n % base; // obtenemos el resto, para guardarlo en el vector
if(rest > 9 ){ // en caso que el numero dado sea mayor que 9, hay que representarlo en caracteres alfabeticos
rest+=55; // si rest = 10, 10 + 55 = 65 === 'A'
digitos[index]=(char)rest;
}
else{
rest+=48;
digitos[index]=(char)rest;
}
index--; // disminuimos el indice del vector
set_base(result); // llamada recursiva
}
else{
if(n > 9 ){ // en caso que el numero dado sea mayor que 9, hay que representarlo en caracteres alfabeticos
n+=55; // si rest = 10, 10 + 55 = 65 === 'A'
digitos[index]=(char)n;
}
else{
n+=48;
digitos[index]=(char)n;
}
digitos[index]=n; // al llegar al ultimo valor en el que no se pueda dividir por la base, se inserta en la posicion mas
index=0; // significativa
}
}
void make_op(char op){
int result=0;
switch(op){
case '+': result=val1+val2;resize_v(longitud(result));set_base(result);break;
case '-': result=val1-val2;resize_v(longitud(result));set_base(result);break;
case '*': result=val1*val2;resize_v(longitud(result));set_base(result);break;
case '/': result=val1/val2;resize_v(longitud(result));set_base(result);break;
case '=': val1==val2 ? cout<<"Son iguales"<<endl : cout<<"No son iguales"<<endl;break;
case '<': val1<val2 ? cout<<"El primer valor es menor"<<endl : cout<<"El primer valor es mayor";break;
case '>': val1>val2 ? cout<<"El primer valor es mayor"<<endl : cout<<"El primer valor es menor"<<endl;break;
}
}
int get_result(){
if(index>0){
int result=int(digitos[index]);
result>=55 ? result-55 : result-48;
int a=pow(base,index);
index--;
final=get_result()+ result*a;
return final;
}
}
ostream& write(ostream& os)const{
for(int i = 0; i <= size; i++){
cout << " | " << digitos[i];
}
cout<< endl;
}
};
That template leaks everywhere:
Builder
The template's constructor is supposed to receive a value... but it doesn't do anything with that value. I would expect it to store it in
digitos
, but no.The only thing it does with
valor
is callsizeof
for what? that operation does not make sense since itsizeof
will not tell you how many digits the number has.sizeof
is evaluated at compile time and tells you how many bytes the type in question occupies.T == int
,sizeof(valor) == 4
and it doesn't matter the content ofvalor
T == char[],
sizeof(value) == strlen(value)+1`. You also don't get the number of digitsT == char
,sizeof(valor) == 1
Taking into account that
T
it is used as the base type of the array, we see that it would only make sense thatT == char
, but in that case we can only put a single digit in the object... it doesn't make sense.memory leaks
Every time you call you
resize_v
create a memory leak. you don't free the memory before callingnew
:you need return
If a function has a return type, it must necessarily have a return . In your case that is not fulfilled in the function
write
:unused types
The template has 3 parameters. What does each one work for?
T
is used as the base for the arraydigitos
. Does it make sense for it todigitos
be an array of double? it seems to me not. It is also used in the constructor, here it could make sense but only to be able to pass fromT
to an array of integers or characters. From my point of viewT
it should not be a parameter of the class but of the constructor:N
is used to determine the size of the arraydigitos
, but only temporarily because it is then resized to a different size. What is it forN
then?B
It has no use directly, so it is a parameter that seems to be redundant.In short, with the code you present there is no reason for it to
number
be a template.But of course, I don't quite understand what utility this class intends to cover. You may need to rephrase your question to better refine the answers.