错误:strcpy 未在此范围内声明

发布于 2024-08-20 19:29:04 字数 4698 浏览 5 评论 0原文

我在 Ubuntu g++ 版本 4.4.3 中编译的 c++ 问题中遇到了这个问题。 我不知道要包含哪些标头来解决这个问题..谢谢

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红衣飘飘貌似仙 2024-08-27 19:29:04

观察:

  • #include 应该引入 std::strcpy()。
  • using namespace std; (如在medico.h 中所写)将std:: 中的任何标识符引入到全局命名空间中。

除了 using namespace std; 一旦应用程序变得更大(因为它在全局命名空间中引入了大量标识符)会有点笨拙,并且您不应该永远在头文件中使用 using(见下文!),using namespace 不会影响语句之后引入的标识符。

using namespace std 写在标头中,该标头包含在 medico.cpp 中,但 #include 出现在之后。 )

我的建议:using namespace std;(如果您坚持使用它)放入medico.cpp中,在任何包含之后,并在medico.h中使用显式的std::


strcmpi() 根本不是一个标准函数;虽然在 Windows 上定义,但在 Linux 上必须以不同的方式解决不区分大小写的比较。

(一般而言,我想指出这个答案< /a> 关于 C 和 C++ 中考虑 Unicode 的“正确”字符串处理,就像每个应用程序都应该考虑的那样。 摘要:标准不能正确处理这些事情。 > 使用 ICU。)


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

“字符串常量”是指您编写字符串文字(例如 “Hello”) 在您的代码中。它的类型是const char[],即常量字符数组(因为你无法更改字符)。您可以将数组分配给指针,但分配给 char *(即删除 const 限定符)会生成您所看到的警告。


OT 说明:头文件中的 using 会更改包括该标头在内的任何人的标识符的可见性,这通常不是头文件的用户想要的。例如,我可以在我的代码中完美地使用 std::string 和自写的 ::string除非我包含您的 medico.h,因为这样两个类就会发生冲突。

不要在头文件中使用using

即使在实现文件中,它也可能会带来很多歧义。也有一种情况需要在实现文件中使用显式命名空间。

Observations:

  • #include <cstring> should introduce std::strcpy().
  • using namespace std; (as written in medico.h) introduces any identifiers from std:: 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!), using namespace does not affect 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; (if you insist on using it at all) 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.

(On general terms, I would like to point to this answer with regards to "proper" string handling in C and C++ that takes Unicode into account, as every application should. Summary: The standard cannot handle these things correctly; do use ICU.)


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

A "string constant" is when you write a string literal (e.g. "Hello") in your code. Its type is const char[], i.e. array of constant characters (as you cannot change the characters). You can assign an array to a pointer, but assigning to 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.

And even in implementation files, it can introduce lots of ambiguity. There is a case to be made to use explicit namespacing in implementation files as well.

烟沫凡尘 2024-08-27 19:29:04

当您说:

 #include <cstring>

g++ 编译器应该将其本身包含的 声明放入 std:: 和全局命名空间中。由于某种原因,它看起来好像没有这样做。尝试将 strcpy 的一个实例替换为 std::strcpy 并查看是否可以解决问题。

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.

眼藏柔 2024-08-27 19:29:04

这个错误有时会在这样的情况下发生:

#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
}

如果以下代码行没有在运行时运行:

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

您将在代码中遇到如下错误;因为initstate被放置在stdlib.h文件中并且它没有被包含

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

This error sometimes occurs in a situation like this:

#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
}

If the following code lines not run in the run-time:

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

you will face with an error in your code like something as follows; because initstate is placed in the stdlib.h file and it's not included:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文