fout: strcpy is niet gedeclareerd in dit bereik

Ik krijg dit probleem bij het compileren van een c++-probleem in Ubuntu g++ versie 4.4.3.
Ik weet niet welke headers ik moet opnemen om dit probleem op te lossen. Bedankt

centro_medico.cpp: In constructor ‘Centro_medico::Centro_medico(char*, char*, int, int, float)’:
centro_medico.cpp:5: error: ‘strcpy’ was not declared in this scope
centro_medico.cpp:13: warning: deprecated conversion from string constant to ‘char*’
centro_medico.cpp:13: warning: deprecated conversion from string constant to ‘char*’
centro_medico.cpp: In member function ‘Centro_medico& Centro_medico::operator=(const Centro_medico&)’:
centro_medico.cpp:26: error: ‘strcpy’ was not declared in this scope
centro_medico.cpp:39: warning: deprecated conversion from string constant to ‘char*’
centro_medico.cpp:39: warning: deprecated conversion from string constant to ‘char*’
centro_medico.cpp: In member function ‘bool Centro_medico::quitar_medico(int)’:
centro_medico.cpp:92: warning: deprecated conversion from string constant to ‘char*’
centro_medico.cpp:92: warning: deprecated conversion from string constant to ‘char*’
centro_medico.cpp: In member function ‘void Centro_medico::mostrar_especialidades(std::ostream&) const’:
centro_medico.cpp:123: error: ‘strcmpi’ was not declared in this scope
centro_medico.cpp: In member function ‘void Centro_medico::mostrar_horarios_consulta(char*) const’:
centro_medico.cpp:162: error: ‘strcmpi’ was not declared in this scope
centro_medico.cpp: In member function ‘void Centro_medico::crea_medicos()’:
centro_medico.cpp:321: warning: deprecated conversion from string constant to ‘char*’
centro_medico.cpp:321: warning: deprecated conversion from string constant to ‘char*’

medico.cpp

#include "medico.h"
#include <cstdlib>
#include <iostream>
#include <stdlib>  
#include<cstring>
#include<string>
long Medico::total_consultas=0; 
Medico::Medico(char *nom,char * espe,int colegiado,int trabajo)
{
int i;
strcpy(nombre,nom);
strcpy(especialidad,espe);
num_colegiado=colegiado;
num_horas_diarias=trabajo;
citas_medico= new Cita*[5]; // 5 Días de las semana, de Lunes a Viernes.
for (i=0;i<5;i++)
citas_medico[i]=new Cita[num_horas_diarias];
}
Medico::Medico(const Medico &m){
  int i;
  citas_medico=new Cita*[5];
  for (i=0;i<5;i++)
   citas_medico[i]=NULL;
 (*this) = m;
}
Medico &Medico::operator=(const Medico &m){
 int i,j;
 if (this != &m) { // Para evitar la asignación de un objeto a sí mismo
     strcpy(nombre,m.nombre);
     strcpy(especialidad,m.especialidad);     
     num_colegiado=m.num_colegiado;
     num_horas_diarias=m.num_horas_diarias;
     for (i=0;i<5;i++){
      delete citas_medico[i]; 
      citas_medico[i]=new Cita[num_horas_diarias];
      for(j=0;j<num_horas_diarias;j++){
       citas_medico[i][j] = m.citas_medico[i][j] ;
       }
     }     
  }
 return *this;
}

medico.h

#pragma once
#include <cstdlib>
#include <iostream>
using namespace std;
#include "cita.h"
class Medico
{
 private:
                char nombre[50];
                char especialidad[50];
                int num_colegiado;
                int num_horas_diarias;
                Cita **citas_medico;
                static long total_consultas;                
 public:
                void mostrar_calendario_citas(ostream &o=cout) const;
                bool asignar_cita(int d, int hor,Paciente *p=NULL);
                void anular_cita(int d, int hor);
                bool consultar_cita(char dni[10], int modificar=0);
                void modificar_cita(int d, int hor);                
                void vaciar_calendario_citas();
                void borrar_calendario_citas();                
                char* get_especialidad(char espec[50]) const;
                char* get_nombre(char n[50]) const;
                int get_num_colegiado() const;
                int get_num_horas() const;
                void set_num_horas(int horas);
                void mostrar_info(ostream &o=cout) const;
                static long get_total_consultas();
                Cita* operator[](int dia);
                void eliminar_calendario_citas();
                void crear_calendario_citas();    
                Medico(char *nom,char * espe,int colegiado,int trabajo);
                Medico(const Medico &m);
                Medico &operator=(const Medico &c);
                void operator delete(void*);
                ~Medico();
 };
 ostream& operator<<(ostream &o, Medico &c);
 ofstream& operator<<(ofstream &fichero, Medico &m);
 ifstream& operator>>(ifstream &fichero, Medico &m);

Antwoord 1, autoriteit 100%

Waarnemingen:

  • #include <cstring>zou std::strcpy() moeten introduceren.
  • using namespace std;(zoals geschreven in medico.h) introduceert alle identifiers van std::in de globale naamruimte.

Behalve dat using namespace std;wat onhandig is als de applicatie groter wordt (omdat het een heleboel identifiers in de globale naamruimte introduceert), en dat je nooitgebruik usingin een headerbestand (zie hieronder!), using namespaceheeft geen invloed op identifiers die nade instructie zijn geïntroduceerd.

(using namespace stdwordt geschreven in de header, die is opgenomen in medico.cpp, maar #include <cstring>komt nadat.)

Mijn advies:plaats de using namespace std;(als je erop staat om het überhaupt te gebruiken) in medico.cpp, na eventuele include, en gebruik expliciete std::in medico.h.


strcmpi()is helemaal geen standaardfunctie; terwijl het wordt gedefinieerd op Windows, moet je hoofdletterongevoelige vergelijkingen op Linux anders oplossen.

(Over het algemeen wil ik verwijzen naar dit antwoordmet betrekking tot “juiste” string-afhandeling in C en C++ die rekening houdt met Unicode, zoals elke applicatie zou moeten. Samenvatting: De standaard kandeze dingen niet correct afhandelen; doe gebruik ICU.)


warning: deprecated conversion from string constant to ‘char*’

Een “tekenreeksconstante” is wanneer u een letterlijke tekenreeks (bijv. "Hello") in uw code schrijft. Het type is const char[], d.w.z. een array van constantetekens (omdat u de tekens niet kunt wijzigen). U kunt een array toewijzen aan een pointer, maar toewijzen aan char *, d.w.z. het verwijderen van de kwalificatie const, genereert de waarschuwing die u ziet.


OT verduidelijking: usingin een headerbestand verandert de zichtbaarheid van identifiers voor iedereen, inclusief die header, wat meestal niet is wat de gebruiker van uw headerbestand wil. Ik zou bijvoorbeeld std::stringen een zelfgeschreven ::stringperfect in mijn code kunnen gebruiken, tenzij ik uw medico.h, want dan zullen de twee klassen botsen.

Gebruik usingniet in header-bestanden.

En zelfs in implementatiebestanden kan het voor veel onduidelijkheid zorgen. Er is een argument om expliciete naamruimte te gebruiken in implementatiebestanden.


Antwoord 2, autoriteit 4%

Als je zegt:

#include <cstring>

de g++-compiler moet de <string.h>-declaraties die hij zelf bevat in de std::EN de globale naamruimten plaatsen. Het ziet er om de een of andere reden uit alsof het dat niet doet. Probeer een exemplaar van strcpyte vervangen door std::strcpyen kijk of dat het probleem oplost.


Antwoord 3

Deze fout treedt soms op in een situatie als deze:

#ifndef NAN
#include <stdlib.h>
#define NAN (strtod("NAN",NULL))
#endif
static void init_random(uint32_t initseed=0)
{
    if (initseed==0)
    {
        struct timeval tv;
        gettimeofday(&tv, NULL);
        seed=(uint32_t) (4223517*getpid()*tv.tv_sec*tv.tv_usec);
    }
    else
        seed=initseed;
#if !defined(CYGWIN) && !defined(__INTERIX)
    //seed=42
    //SG_SPRINT("initializing random number generator with %d (seed size %d)\n", seed, RNG_SEED_SIZE)
    initstate(seed, CMath::rand_state, RNG_SEED_SIZE);
#endif
}

Als de volgende coderegels niet worden uitgevoerd tijdens de runtime:

#ifndef NAN
#include <stdlib.h>
#define NAN (strtod("NAN",NULL))
#endif

u krijgt te maken met een fout in uw code, zoals het volgende; omdat initstatein het bestand stdlib.hwordt geplaatst en niet wordt opgenomen:

In file included from ../../shogun/features/SubsetStack.h:14:0, 
                 from ../../shogun/features/Features.h:21, 
                 from ../../shogun/ui/SGInterface.h:7, 
                 from MatlabInterface.h:15, 
                 from matlabInterface.cpp:7: 
../../shogun/mathematics/Math.h: In static member function 'static void shogun::CMath::init_random(uint32_t)': 
../../shogun/mathematics/Math.h:459:52: error: 'initstate' was not declared in this scope

Other episodes