简体   繁体   English

std :: strcpy未在此范围内声明DESPITE #include <cstring>

[英]std::strcpy not declared in this scope DESPITE #include <cstring>

Simple bit of code. 简单的代码。 It's obviously incomplete as of this point, but it should compile. 到目前为止它显然是不完整的,但它应该编译。 I'm converting this to Linux from Visual Studio. 我正在从Visual Studio将其转换为Linux。 When I compile with g++ test.cpp I get this error: 当我用g ++ test.cpp编译时,我收到此错误:

In file included from test.cpp:2:0: 在test.cpp中包含的文件中:2:0:
String.h: In constructor String::String(char*): String.h:在构造函数String :: String(char *)中:
String.h:24:2: error: strcpy is not a member of std std::strcpy(pointer_Object, s); String.h:24:2:错误:strcpy不是std std :: strcpy(pointer_Object,s)的成员;

#ifndef _STRING_H
#define _STRING_H

#include <cstring>
#include "ArrayClass.h"
#include "Exception.h"

class String : virtual public ArrayClass<char>
{

public:
    String();
    String(char* s);

};

String::String() : ArrayClass<char>(1,'\0') {}

String::String(char* s) : ArrayClass<char>(std::strlen(s)+1)
{
    std::strcpy(pointer_Object, s);
}

#endif

It seems like I've been over it backwards and forwards. 好像我已经向前和向前过去了。 Any Ideas? 有任何想法吗?

Compiling with these: 用这些编译:

  • libgcc-4.9.2-6.fc21.i686 libgcc中,4.9.2-6.fc21.i686

  • gcc-4.9.2-6.fc21.x86_64 GCC-4.9.2-6.fc21.x86_64

  • gcc-c++-4.9.2-6.fc21.x86_64 GCC-C ++ - 4.9.2-6.fc21.x86_64

  • libgcc-4.9.2-6.fc21.x86_64 libgcc中,4.9.2-6.fc21.x86_64

It appears that you've hacked string.h 's include guard. 看来你已经攻击了string.h的包含守卫。

#ifndef _STRING_H
#define _STRING_H

It's illegal to do this, and unclear why you did. 这样做是违法的,也不清楚你为什么这么做。 #include <cstring> is all that is necessary. #include <cstring>就是必要的。

The likely result is that <string.h> gets ignored, which will cause things to be missing from the global namespace which <cstring> expects. 可能的结果是<string.h>被忽略,这将导致<cstring>期望的全局命名空间中缺少事物。

EDIT: Ah, now I see. 编辑:啊,现在我明白了。 Your header is also named "string.h" . 您的标题也被命名为"string.h"

Names starting with an underscore followed by a capital letter are reserved to the implementation: the compiler and the standard library. 以下划线后跟大写字母开头的名称保留给实现:编译器和标准库。 They can be internal-use operators, or internal variables (such as include guards for system headers). 它们可以是内部使用的运算符,也可以是内部变量(例如包含系统头的保护)。 Try this instead: 试试这个:

#ifndef INCLUDED_SEANS_STRING_H
#define INCLUDED_SEANS_STRING_H

Since macros are all lumped into only one namespace, it's up to you to use macro names that don't collide with anything else. 由于宏都集中在一个名称空间中,因此您可以使用不会与其他任何内容发生冲突的宏名称。 Some folks go so far as to put UUIDs in header guards; 有些人甚至把UUID放在头部防护装置中; I just mention the name of the library. 我只提到了图书馆的名称。

It isn't legal in C++ to start any identifier including a macro with an _ followed by a capital letter. 在C ++中启动任何标识符都是不合法的,包括带有_后跟大写字母的宏。 Therefore: 因此:

#ifndef _STRING_H
#define _STRING_H

should be changed to something else that doesn't break this rule. 应该更改为不违反此规则的其他内容。

The working from the standard: 从标准工作:

7.1.3 Reserved identifiers 7.1.3保留标识符

Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers. 每个标头声明或定义其关联子条款中列出的所有标识符,并可选地声明或定义其关联的未来库方向子条款和标识符中列出的标识符,这些标识符始终保留用于任何用途或用作文件范围标识符。

  • All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use. 所有以下划线开头的标识符以及大写字母或另一个下划线始终保留用于任何用途。
  • All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces. 所有以下划线开头的标识符始终保留用作普通和标记名称空间中具有文件范围的标识符。

To go into a little more detail, what is probably happening is that your implementation of <cstring> is using _STRING_H as its own include guard, and therefore when you include it it is being masked out by the repeated guard #ifdef . 更详细一点,可能发生的事情是你的<cstring>是使用_STRING_H作为它自己的包含保护,因此当你包含它时,它被重复的守卫#ifdef掩盖了。 So as I said, the best solution is just to use a standards compliant macro name that doesn't begin with an _ 正如我所说,最好的解决方案就是使用一个不以_开头的符合标准的宏名称

它不在std命名空间中,只需使用strcpy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM