我正在尝试编译这个非常简单的程序,在其中实现了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
我在这里做错什么了吗?我不认为我会错过任何标头或名称空间标识符,因此应该进行编译。
答案 0 :(得分:5)
宏名称ERC721.sol
保留供C标准使用。定义此类宏是未定义的行为。
您要在_STRING_H_
周围加上str.h
并加以定义。但是,相同的包含保护很可能用在_STRING_H_
标准C标头中。因此,string.h
中的所有符号将在您的程序中不可见。就像使用string.h
一样,您可能很幸运glibc,但是我可以发现many implementations that use只是字符串{{ 1}}包括对文件的保护。
要解决此问题,请将包含保护从_STRING_H
更改为示例_STRING_H_
或_STRING_H_
,但不要使用下划线。请记住,有关编写程序时的reserved names by the C standard。