[英]Fail to compile: strlen is not a member of std
I'm trying to compile this very simple program where I'm implementing a simplified version of C++ strings. 我正在尝试编译这个非常简单的程序,在其中实现了C ++字符串的简化版本。 However the compiler cannot find the std::strlen function even though I included
但是,即使我包含了编译器,编译器也找不到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
I'm compiling using g++ (homebrew) 我正在使用g ++(自制)进行编译
g++ main.cpp -o main
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
Am I doing something wrong here ? 我在这里做错什么了吗? I don't think I missed any headers or namespace identifier so it should compile.
我认为我没有错过任何标头或名称空间标识符,因此应该进行编译。
The macro name _STRING_H_
is reserved for the C standard to use. 宏名称
_STRING_H_
保留供C标准使用。 Defining such macro is undefined behavior. 定义此类宏是未定义的行为。
You'r include guard around str.h
is _STRING_H_
and you define it. 找你身边有后卫
str.h
是_STRING_H_
,你定义它。 But, the same include guard is most probably used in string.h
standard C header. 但是,相同的include保护很可能用在
string.h
标准C标头中。 So all symbols from string.h
will be not visible in your program. 因此,
string.h
所有符号在您的程序中都不可见。 As you could get lucky with glibc , as it uses _STRING_H
, but I could find many implementations that use just the string _STRING_H_
as include guards around their files. 正如你可以得到幸运glibc的 ,因为它使用
_STRING_H
,但我能找到很多 的实现 是 使用只是字符串_STRING_H_
作为包括在他们的文件警卫。
To fix the issue, change your include guard from _STRING_H_
to example STR_H_
or STRING_H_
without the leading underscore. 要解决此问题,请将您的包含防护从
_STRING_H_
为示例STR_H_
或STRING_H_
但不要使用下划线。 Remember, about reserved names by the C standard when writing programs. 请记住,关于编写程序时使用C标准保留的名称 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.