Fail to compile: strlen is not a member of std

NBW*_*BWL 1 c++ std strlen

I'm trying to compile this very simple program where I'm implementing a simplified version of C++ strings. However the compiler cannot find the std::strlen function even though I included

//main.cpp
#include "str.h"

int main()
{
    return 0;
}

// str.h
#ifndef _STRING_H_
#define _STRING_H_

#include <vector>
#include <cstring>
#include <algorithm>
#include <iterator>

class Str {
public:
    typedef std::vector<char>::size_type size_type;

    Str() { }
    Str(size_type n, char c): data(n, c) { }
    Str(const char* cp) {
        std::copy(cp, cp+std::strlen(cp), std::back_inserter(data));
    }
    template <class In> Str(In b, In e) {
        std::copy(b, e, std::back_inserter(data));
    }

private:
    std::vector<char> data;
};

#endif
Run Code Online (Sandbox Code Playgroud)

I'm compiling using g++ (homebrew)

g++ main.cpp -o main
Run Code Online (Sandbox Code Playgroud)

and here's the result log

In file included from str.h:5,
                 from main.cpp:1:
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:75:11: error: '::memchr' has not been declared
   using ::memchr;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:76:11: error: '::memcmp' has not been declared
   using ::memcmp;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:77:11: error: '::memcpy' has not been declared
   using ::memcpy;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:78:11: error: '::memmove' has not been declared
   using ::memmove;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:79:11: error: '::memset' has not been declared
   using ::memset;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:80:11: error: '::strcat' has not been declared
   using ::strcat;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:81:11: error: '::strcmp' has not been declared
   using ::strcmp;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:82:11: error: '::strcoll' has not been declared
   using ::strcoll;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:83:11: error: '::strcpy' has not been declared
   using ::strcpy;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:84:11: error: '::strcspn' has not been declared
   using ::strcspn;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:85:11: error: '::strerror' has not been declared
   using ::strerror;
           ^~~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:86:11: error: '::strlen' has not been declared
   using ::strlen;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:87:11: error: '::strncat' has not been declared
   using ::strncat;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:88:11: error: '::strncmp' has not been declared
   using ::strncmp;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:89:11: error: '::strncpy' has not been declared
   using ::strncpy;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:90:11: error: '::strspn' has not been declared
   using ::strspn;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:91:11: error: '::strtok' has not been declared
   using ::strtok;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:92:11: error: '::strxfrm' has not been declared
   using ::strxfrm;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:93:11: error: '::strchr' has not been declared
   using ::strchr;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:94:11: error: '::strpbrk' has not been declared
   using ::strpbrk;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:95:11: error: '::strrchr' has not been declared
   using ::strrchr;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:96:11: error: '::strstr' has not been declared
   using ::strstr;
           ^~~~~~
In file included from main.cpp:1:
str.h: In constructor 'Str::Str(const char*)':
str.h:19:31: error: 'strlen' is not a member of 'std'
         std::copy(cp, cp+std::strlen(cp), std::back_inserter(data));
                               ^~~~~~
str.h:19:31: note: suggested alternative: 'strstr'
         std::copy(cp, cp+std::strlen(cp), std::back_inserter(data));
                               ^~~~~~
                               strstr
Run Code Online (Sandbox Code Playgroud)

Am I doing something wrong here ? I don't think I missed any headers or namespace identifier so it should compile.

Kam*_*Cuk 5

宏名称_STRING_H_保留供C标准使用。定义此类宏是未定义的行为。

找你包括后卫身边str.h_STRING_H_你定义它。但是,相同的包含保护很可能在string.h标准C标头中使用。因此,所有符号string.h都将在您的程序中不可见。正如你可以得到幸运glibc的,因为它使用_STRING_H的,但我可以找到很多 的实现 使用只是字符串_STRING_H_作为包括在他们的文件警卫。

要解决此问题,请将您的include防护从更改_STRING_H_为example STR_H_STRING_H_不带下划线。请记住,关于编写程序时使用C标准保留的名称