Reach problem in C ++

1

I'm a beginner in C ++ programming and I came across a problem when I tried to run my code in atom , using the gpp-compiler package and MinGW: 'function' was not declared in this scope l.8). I did not understand the reason for the error, can anyone help me?

Here is the main function:

#include <iostream>
#include "header.hpp"

using namespace std;

int main(){
    int* a;
    a = function(50);
    for(int d = 0; d < a.length; d++){
       .
       .
       .
    }
    system("pause");
    return 0;
}

and the header file:

#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
#include <vector>

int* function(int num){
    std::vector<int> c;
       .
       .
       .     
    int z[(int)c.size()];
    for(int b = 0; b < (int)c.size(); b++){
        z[b] = c.at(b);
    }
    return z;
}

#endif

To be more specific, the error message was as follows:

  

... \ main.cpp: In function 'int main ()':

     

... \ main.cpp: 9: 18: error: 'function' was not declared in this scope
  a = function (50);

     

... \ main.cpp: 10: 23: error: request for member 'length' in 'a', which   is of non-class type 'int *' for (int d = 0; d

    
asked by anonymous 11.02.2018 / 01:22

1 answer

0

To be able to return z, being a pointer have to allocate memorize first

int * z = new int[(int)c.size()];

Then be careful to delete when the array is no longer necessary in the main function.

delete a;

The arrays in C ++ are like in C, do not have the attribute "length" unlike the java, to know the size of the array is necessary another way to save the size, to avoid these problems I advise to use the vector. / p>     

14.02.2018 / 22:14