Function that returns a list

0

I am new programming in c ++; So far I have programmed in visual basic.e get in state of 4 analog inputs and save them in a list EA={"","","",""} . But when I want the function to return that list ( return EA; ) it gives me an error. I leave you in code in case you can help me.

I have modified the code and I almost have it but it still gives me a conversion error:

std::tr1::array<int, 4>FuncionEA(){

std::tr1::array<int, 4> EA[]={0,0,0,0};

return EA;}

the error is as follows:

Error   1   error C2440: 'return' : no se puede realizar la conversión de 'std::tr1::array<_Ty,_Size> [1]' a 'std::tr1::array<_Ty,_Size>'   c:\users\p\documents\visual studio 2008\projects\we\we\we.cpp   13  we
    
asked by Andermutu 03.07.2017 в 09:31
source

1 answer

3

Problem.

The error is crystal clear, maybe you are not familiar with the compiler's messages and that's why you do not understand it.

The compiler is telling you, " You can not convert an array of four double ( double [4] ) to double ) ".

Evident, right? Four cars are not a car, four toast with butter are not toast with butter, four gallifants are not a fowl ... you see the idea, right?

Solution.

Use std::array

std::array<double, 4> FuncionEA(){

    // ...

    std::array<double, 4> EA[]={0.,0.,0.,0.};

    while(exitChar != '5')
    {
        for (adcChannel = 0; adcChannel < 4; adcChannel++)
        {
            // ...
            EA[adcChannel]=(temp+0.03)/0.15;
        }
    }

    return EA;
}

The template std::array allows you to manage data collections (in your case a double ) with a fixed size (in your case four). If the syntax bothers you, you can declare the type with an alias:

using entradas_analogicas = std::array<double, 4>;

entradas_analogicas FuncionEA(){

    // ...

    entradas_analogicas EA[]={0.,0.,0.,0.};

    // ...

    return EA;
}

Note that you are initializing the array with integers ( 0 ) but it contains double , use a literal double .

    
answered by 03.07.2017 в 09:38