doubt matrices c ++ [closed]

2

I have the following exercise in which they ask me to compare a value with the numbers of a matrix given a precision that can not be exceeded, the numbers that meet that condition will be printed in a new matrix and if they do not comply, it will be printed a 0.

I have tried to do it, this is the function that they give me in the header file: previously, I declared a global variable with precision

const double epsilon=1e-3;

void filtra(matrix_t& M, matrix_item_t it, double precision);

Well, in the hpp the development, and I've put this:

void matrix_t::filtra(const matrix_t& M, matrix_item_t it, double epsilon) {

redimensiona(m_,n_); //destruí la matriz A y la redimensioné asignándole un número de filas y columnas
 matrix_t E; //esta matriz la declará para que guarde los números de la matriz A que cumplan con la precisión,y los que no cumplan, que guarde un 0 en su posición en mi nueva matriz,al usar funciones booleanas

for (i=0; i<(E.get_m()); i++) {
    for (j=0; j<(E.get_n());j++)
        if ((E.get_matrix_item(i,j))-matrix_item_t it < epsilon){
            E.set_matrix_item(i,j);
       }

M is a dynamic array that contains the rows and columns of the matrix A , the original matrix matrix_item_t it is the element that I have to search in the matrix A .

Then in the main , I put this:

matrix_t A;

    matrix_t &B=matrix_t A;
    matrix_t &E=matrix_t B;

cout << endl;
A.read(cin);
cout << "---  MATRIZ ORIGINAL  ---" << endl;

B.write();

cout << "---  PRECISIÓN 0.001  ---" << endl;
A.filtra(B,5.254,1E-3);

cout << "---  PRECISIÓN 0.001  ---" << endl;

A.filtra(B,5.254,1E-3);
B.write();

B.write();

cout << "---  PRECISIÓN 0.01   ---" << endl;

B.write();

cout << "---  PRECISIÓN 0.1    ---" << endl;
A.filtra(B,5.254,1E-1);
B.write();
cout << "---  PRECISIÓN 1.0    ---" << endl;
A.filtra(B,5.254,1.0);
B.write();

This last part was given to me in the exercise as a code that I had to put in the main, A.filtra's ...

what I put is matrix_t &B=matrix_t A; matrix_t &E=matrix_t B; until it starts A.filtra , and obviously everything about cpp and hpp I did, the problem is that when I compile I get a lot of errors, and it costs me To understand the errors pointed out by the compiler:

main_m_3.cpp: In function ‘int main()’:
main_m_3.cpp:25:18: error: redeclaration of ‘matrix_t A’
         matrix_t A;
                  ^
main_m_3.cpp:5:11: note: ‘matrix_t A’ previously declared here
  matrix_t A;
       ^
main_m_3.cpp:26:18: error: conflicting declaration ‘matrix_t B’
         matrix_t B;
                  ^
main_m_3.cpp:12:18: note: previous declaration as ‘const matrix_t& B’
  const matrix_t& B=A;
              ^
main_m_3.cpp:27:21: error: conversion from ‘const matrix_t*’ to non-scalar type ‘matrix_t’ requested
         matrix_t E=&B;
                 ^
main_m_3.cpp:35:3: error: ‘class matrix_t’ has no member named ‘filtra’
 A.filtra(B,5.254,1E-4);
   ^
main_m_3.cpp:39:3: error: ‘class matrix_t’ has no member named ‘filtra’
 A.filtra(B,5.254,1E-3);
   ^
main_m_3.cpp:43:3: error: ‘class matrix_t’ has no member named ‘filtra’
 A.filtra(B,5.254,1E-2);
   ^
main_m_3.cpp:47:3: error: ‘class matrix_t’ has no member named ‘filtra’
 A.filtra(B,5.254,1E-1);
  ^
main_m_3.cpp:51:3: error: ‘class matrix_t’ has no member named ‘filtra’
 A.filtra(B,5.254,1.0);
   ^

no ‘void matrix_t::filtra(const matrix_t&, matrix_item_t, double)’ member function declared in class ‘matrix_t’
 void matrix_t::filtra(const matrix_t& M, matrix_item_t it, double epsilon) 

The truth is that I do not know where the code fails, a lot of help is appreciated

Thank you very much.

    
asked by Robert 21.02.2017 в 01:42
source

2 answers

1

To correct the errors you must start with the first error that shows you and seek to correct it. As it does not show all the main.cpp it is difficult to know where it is exactly but you can find it.

main_m_3.cpp: In function ‘int main()’:
main_m_3.cpp:25:18: error: redeclaration of ‘matrix_t A’
         matrix_t A;

This error tells you that inside "int main ()" on line 25, specifically in column 18 you re-declare the variable matrix_t A and I think it is in the following lines where the error is

matrix_t A;

    matrix_t &B=matrix_t A;
    matrix_t &E=matrix_t B;

because you declare it above and you re-declare it below, the correct thing would be

matrix_t A;

    matrix_t &B= A;
    matrix_t &E= B;

Greetings

    
answered by 21.02.2017 в 08:15
1
  

It's hard for me to understand the errors pointed out by the compiler

In that case, I think it might be a good idea to help you understand them so that you can correct them later.

The first error you show:

main_m_3.cpp: In function ‘int main()’:
main_m_3.cpp:25:18: error: redeclaration of ‘matrix_t A’
     matrix_t A;
              ^
main_m_3.cpp:5:11: note: ‘matrix_t A’ previously declared here
     matrix_t A;

Consists of a re-declaration of variables ( redeclaration of ‘matrix_t A’ ). This means that at one point in the code you declared a variable called A of type matrix_t and at another point of the same code you declared another variable called A .

In particular, the compiler tells you that in line 5, column 11 you declared A for the first time and in line 25 column 8 you declared it a second time. In C ++ a declaration has the following format:

tipo nombre;
tipo nombre = inicializador;
tipo nombre(parámetros de construcción);
tipo nombre{parámetros de construcción};

The C ++ language (and many others) explicitly prohibits using entity names (objects, functions) more than once because if several things (entities) have the same name, which one do you mean when you use that name?

If we review your code we see the following:

matrix_t A;

    matrix_t &B=matrix_t A;
    matrix_t &E=matrix_t B;

You declare the object A of type matrix_t , then declare a reference ( & ) to matrix_t call B and use as initializer another statement : matrix_t A . He considers it another statement because it follows the format we have seen before. When you declare the reference to matrix_t called E the same thing happens to you because you try to initialize E with a declaration (something that is not allowed), this declaration is of the object called B (that you had already tried to declare in the previous line). So, change the code to the following:

matrix_t A;
matrix_t &B = A;
matrix_t &E = B;

Omitting the type, the compiler will realize that you are not re-stating A or B .

Your next error does not seem to correspond to the code you've shown:

main_m_3.cpp:26:18: error: conflicting declaration ‘matrix_t B’
     matrix_t B;
              ^
main_m_3.cpp:12:18: note: previous declaration as ‘const matrix_t& B’
  const matrix_t& B=A;

Indicates that on line 26 column 18 you are declaring an entity named B when another entity with the same name was already declared on line 12 column 18. If you follow the recommendations of the previous lines you should be able to correct the error.

Here's the error:

main_m_3.cpp:27:21: error: conversion from ‘const matrix_t*’ to non-scalar type ‘matrix_t’ requested
     matrix_t E=&B;

In this error, the compiler is telling you that you are trying to save an object of type matrix_t in a constant pointer type object to ‘const matrix_t*’ ( matrix_t ) and this requires a conversion of types that you do not know how to perform .

This is because you have prefixed the et ( & ) to the name of a variable of type matrix_t , the et ( & ) gets the memory address of a variable and returns a pointer to said variable (you can read more details in this other question ). Whether this is correct or incorrect will depend on what you would like to do, which we can not know with the code you have shared.

Subsequent errors:

main_m_3.cpp:35:3: error: ‘class matrix_t’ has no member named ‘filtra’
 A.filtra(B,5.254,1E-4);
main_m_3.cpp:39:3: error: ‘class matrix_t’ has no member named ‘filtra’
 A.filtra(B,5.254,1E-3);
main_m_3.cpp:43:3: error: ‘class matrix_t’ has no member named ‘filtra’
 A.filtra(B,5.254,1E-2);
main_m_3.cpp:47:3: error: ‘class matrix_t’ has no member named ‘filtra’
 A.filtra(B,5.254,1E-1);
main_m_3.cpp:51:3: error: ‘class matrix_t’ has no member named ‘filtra’
 A.filtra(B,5.254,1.0);

no ‘void matrix_t::filtra(const matrix_t&, matrix_item_t, double)’ member function declared in class ‘matrix_t’
 void matrix_t::filtra(const matrix_t& M, matrix_item_t it, double epsilon) 

They are telling you that class matrix_t does not have a member named filtra . The error happens because you are asking an object of type matrix_t (called A ) to use a function called filtra that should belong to it and does not belong, you can see more details in this question .

As I can see, the function filtra is free (does not belong to any object) and expects to receive as a first parameter a value of type matrix_t , so I assume that the expected use of this function would be:

filtra(B, 5.254, 1E-4);
filtra(B, 5.254, 1E-3);
filtra(B, 5.254, 1E-2);
filtra(B, 5.254, 1E-1);
filtra(B, 5.254, 1.0);

But I can not be sure, since the second parameter is of type matrix_item_t and I do not see any declaration of that type.

    
answered by 21.02.2017 в 09:13