Std :: strcpy is not declared in this scope DESPITE #include <cstring>

Simple bit of code. This is clearly incomplete from now on, but it should compile. I am converting this to Linux from Visual Studio. When I compile with g ++ test.cpp I get this error:

In the file included in test.cpp: 2: 0:
String.h: In the constructor String :: String (char *):
String.h: 24: 2: error: strcpy is not a member of 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

      

I seem to have been over it back and forth. Any ideas?

Compiling with:

  • libgcc-4.9.2-6.fc21.i686

  • GCC-4.9.2-6.fc21.x86_64

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

  • libgcc-4.9.2-6.fc21.x86_64

+3


source to share


3 answers


It looks like you hacked string.h

include guard.

#ifndef _STRING_H
#define _STRING_H

      

It is illegal for this and it is not clear why you did it. #include <cstring>

is all that is needed.

The likely result is that it is being <string.h>

ignored, which will result in no objects in the global namespace expected <cstring>

.



EDIT: Ah, now I see. The title also has a title "string.h"

.

Names beginning with an underscore followed by an uppercase letter are reserved for implementation: the compiler and the standard library. They can be internal use statements or internal variables (for example, include protectors for system headers). Try this instead:

#ifndef INCLUDED_SEANS_STRING_H
#define INCLUDED_SEANS_STRING_H

      

Since macros are all concentrated in only one namespace, you need to use macro names that don't clash with anything else. Some people go as far as putting the UUID in the header; I just mention the name of the library.

+5


source


In C ++, it is not allowed to run any identifier, including a macro with _ followed by an uppercase letter. Therefore:

#ifndef _STRING_H
#define _STRING_H

      

should be changed to something else that does not violate this rule.



Working with the standard:

7.1.3 Reserved identifiers

Each header declares or defines all the identifiers listed in the corresponding subclause, and possibly declares or identifies the identifiers listed in the corresponding subclause of its associated future libraries, and identifiers that are always reserved for either use or use as file scope identifiers ...

  • All identifiers beginning with an underscore and an uppercase letter or other underscore are always reserved for any use.
  • All identifiers beginning with an underscore are always reserved for use as file-scoped identifiers in both the regular and tag names.

To go into more detail, it is possible that your implementation <cstring>

uses _STRING_H

as its own include guard, and so when you include it, it is masked by re-guards #ifdef

. As I said, the best solution is to simply use a standards-compliant macro name that does not start with _

+4


source


It's not in namespace std

, just usestrcpy

-4


source







All Articles