编译失败:strlen 不是 std 的成员
Fail to compile: strlen is not a member of std
我正在尝试编译这个非常简单的程序,我在其中实现了 C++ 字符串的简化版本。
但是编译器找不到 std::strlen 函数,即使我包含了
//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
我正在使用 g++(自制软件)进行编译
g++ main.cpp -o main
这是结果日志
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
我是不是做错了什么?我认为我没有遗漏任何 headers 或名称空间标识符,因此它应该可以编译。
宏名_STRING_H_
保留给C标准使用。定义这样的宏是未定义的行为。
你在 str.h
周围包含了 guard 是 _STRING_H_
并且你定义了它。但是,同样的 include guard 很可能用在 string.h
标准 C 头文件中。因此,来自 string.h
的所有符号在您的程序中都将不可见。因为你可能会幸运地使用 glibc, as it uses _STRING_H
, but I could find many implementations that use 字符串 _STRING_H_
作为包括他们文件周围的守卫。
要解决此问题,请将您的包含防护从 _STRING_H_
更改为示例 STR_H_
或 STRING_H_
,而无需前导下划线。记住,写程序的时候约reserved names by the C standard
我正在尝试编译这个非常简单的程序,我在其中实现了 C++ 字符串的简化版本。 但是编译器找不到 std::strlen 函数,即使我包含了
//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
我正在使用 g++(自制软件)进行编译
g++ main.cpp -o main
这是结果日志
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
我是不是做错了什么?我认为我没有遗漏任何 headers 或名称空间标识符,因此它应该可以编译。
宏名_STRING_H_
保留给C标准使用。定义这样的宏是未定义的行为。
你在 str.h
周围包含了 guard 是 _STRING_H_
并且你定义了它。但是,同样的 include guard 很可能用在 string.h
标准 C 头文件中。因此,来自 string.h
的所有符号在您的程序中都将不可见。因为你可能会幸运地使用 glibc, as it uses _STRING_H
, but I could find many implementations that use 字符串 _STRING_H_
作为包括他们文件周围的守卫。
要解决此问题,请将您的包含防护从 _STRING_H_
更改为示例 STR_H_
或 STRING_H_
,而无需前导下划线。记住,写程序的时候约reserved names by the C standard