tags:

views:

1252

answers:

2

I get this problem in a c++ problem compiling in Ubuntu g++ version 4.4.3. I dont know the headers to include to solve this problem.. Thanks

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);
A: 

When you say:

 #include <cstring>

the g++ compiler should put the <string.h> declarations it itself includes into the std:: AND the global namespaces. It looks for some reason as if it is not doing that. Try replacing one instance of strcpy with std::strcpy and see if that fixes the problem.

anon
Not correct. `<cstring>` *does* put the declarations into `std::`, but whether or not it also puts them in the global namespace depends on the implementation. I am not sure if the standard even allows putting them into global, but I am positive it does not *require* it.
DevSolar
It does allow it, currently doesn't require it, and the behaviour I was talking about was that of g++. On my installation (version 4.4.1) it does what I said.
anon
Sorry, I'm a bit anal-retentive when it comes to the language standards. ;-)
DevSolar
+1  A: 

Observations:

  • #include <cstring> should introduce std::strcpy().
  • using namespace std; (as written in medico.h) should introduce std::strcpy() into the global namespace.

Aside from using namespace std; being somewhat clumsy once the application grows larger (as it introduces one hell of a lot of identifiers into the global namespace), and that you should never use using in a header file (see below!), I am not at all sure if using namespace affects identifiers introduced after the statement.

(using namespace std is written in the header, which is included in medico.cpp, but #include <cstring> comes after that.)

My advice: Put the using namespace std; into medico.cpp, after any includes, and use explicit std:: in medico.h.


strcmpi() is not a standard function at all; while being defined on Windows, you have to solve case-insensitive compares differently on Linux.


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

A "string constant" is when you write a string literal (e.g. "Hello") in your code. It's type is const char *, i.e. pointer to constant characters (as you cannot change the characters). Using them as char *, i.e. removing the const qualifier, generates the warning you are seeing.


OT clarification: using in a header file changes visibility of identifiers for anyone including that header, which is usually not what the user of your header file wants. For example, I could use std::string and a self-written ::string just perfectly in my code, unless I include your medico.h, because then the two classes will clash.

Don't use using in header files.

DevSolar
Thank u, i think o solve the problem of error: ‘strcmpi’ was not declared in this scopeI get another warning: warning: deprecated conversion from string constant to ‘char*’
Chak
Sorry, didn't see the other two warning types. I usually fix only the first error before recompiling, as quite often many of the subsequent errors / warnings are follow-ups that vanish once the topmost problem is solved. I extended my answer to cover the other two cases.
DevSolar
THank you so much!!! very helpfull!
Chak