1

I'm working on a project that is basically controlling a parking lot. And in some point of my code I use the function strcpy(), but I am getting an error saying that this function may be unsafe.
Here's the part of the code I'm using strcpy():

Automovel::Automovel(char * matr, Data ent, double comp) {
    //CONSTRUTOR POR ENUMERAÇAO:
    //RECEBE A MATRICULA, A DATA DE ENTRADA E O COMPRIMENTO DO CARRO
    //POSIÇAO E VALOR PAGO FICA INDEFINIDO
    matricula = new char[11];
    entry = new Data(ent);

    strcpy(matricula, matr);
    comprimento = comp;
    pos[0] = -1; pos[1] = -1;
    pago = -1;
}

I need to use the function, so, how can I solve this error?

Cache Staheli
  • 3,510
  • 7
  • 32
  • 51
César Pereira
  • 249
  • 4
  • 17

2 Answers2

1

You didn't say, but I'll assume you're using Visual Studio; that's where I've encountered the error.

Either bite the bullet and use strcpy_s etc., or do this:

#pragma warning(disable : 4996) 

This disables the "feature" that makes strcpy an error.

Topological Sort
  • 2,733
  • 2
  • 27
  • 54
  • And i should do this in every file i get the error? I have many .cpp files in my project – César Pereira Jul 03 '16 at 01:43
  • I would. But you can also do this through the IDE. Go to Project Properties, C/C++, Advanced, then Disable Specific Warnings. https://msdn.microsoft.com/en-us/library/jj715718.aspx – Topological Sort Jul 03 '16 at 01:47
0

Your issue is that strcpy is an 'unsafe' function so you need to disable the unsafe warning in Visual Studio so follow these steps.

Community
  • 1
  • 1
Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61