错误:“strstr”未在此范围内声明

MrN*_*ice 2 c++ strstr raspberry-pi2

我正在树莓派上编写一个具有共享内存的应用程序。我在我自己编写的共享内存库中使用函数 strstr() 。当我在 OS X 上使用 clang++ 编译库时,没有出现错误。如果我在树莓派上编译它,我会收到错误:\'strstr\' 未在此范围内声明。

\n\n

我尝试更新我的树莓派,但没有成功,你能给我任何提示或解决方案吗?

\n\n

标题-Datei

\n\n
#ifndef SHAREDMEMORY_H\n#define SHAREDMEMORY_H\n\n#include <string>\n#include <cstdlib>\n#include <stdio.h>\n#include <sys/types.h>\n#include <sys/stat.h>\n#include <unistd.h>\n#include <fcntl.h>\n#include <sys/mman.h>\n#include <iostream>\n\n#define MAX_SERVICES 99\n\n/**\n * Datei mit der Datenbank.\n */\n#define FILEPATH "database.dat"\n/**\n * Anzahl der Zeichen in der\n * Datenbank.\n */\n#define CHARACTERS 2500\n/**\n * Gr\xc3\xb6\xc3\x9fe der Datenbank.\n */\n#define FILESIZE (CHARACTERS*sizeof(char))\n\nclass SharedMemory {\npublic:\n    /**\n     * Constructor \n     */\n    SharedMemory();\n    /**\n     * Desctructor\n     */\n    ~SharedMemory();\n    /**\n     * Method to open file\n     * @param string: Path to file, has to exist\n     * @param int: for reading 0 \n     *             for writing 1\n     * @return bool: true on success\n     *               false on error\n     */\n    bool openFile( std::string, int );\n    /**\n     * Method to map file to memory\n     * @param string: Path to file, has to exist\n     * @param int: for reading 0 \n     *             for writing 1\n     * @return bool: true on success\n     *               false on error\n     */\n    bool mappingFile( int );\n    /**\n     * Method to remove file from memory\n     * @return bool: true on success\n     *               false on error\n     */\n    bool unmapFile();\n    /**\n     * Method to write information to file\n     * @param string: data to write\n     * e.g. string="#1:127.0.0.1:8000", #number range 0-99.\n     * @return bool: true on success\n     *               false on error\n     */\n    bool set( std::string );\n    /**\n     * Method to read information from file\n     * @param string: need to cointains id, if success\n     *                then contains info from id.\n     *  e.g. string="1", number range "0"-"99".\n     * @return bool: true on success\n     *               false on error\n     */\n    bool get( std::string& );\nprivate:\n    /**\n     * Datei-Deskriptor.\n     */\n    int fd;\n    /**\n     * Zeiger auf Dateiinhalt.\n     */\n    char *mapPointer;\n    /**\n     * Path to file\n     */\n    std::string filePath;\n};\n\n\n#endif /* SHAREDMEMORY_H */\n
Run Code Online (Sandbox Code Playgroud)\n\n

Cpp-达泰

\n\n
#include "SharedMemory.h"\n\nSharedMemory::SharedMemory() { }\n\nSharedMemory::~SharedMemory() { }\n\nbool SharedMemory::openFile( std::string _path, int mode ) {\n    if ( mode ) {\n        fd = open( _path.c_str(), O_RDWR, (mode_t)0600 );\n    } else {\n        fd = open( _path.c_str(), O_RDONLY, (mode_t)0600 );\n    }\n    if ( fd == -1 ) {\n        return false;\n    }\n\n    return true;\n}\n\nbool SharedMemory::mappingFile( int mode ){\n    void* tmpPointer;\n    if ( mode ) {\n        tmpPointer = mmap( 0, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );\n    } else {\n        tmpPointer = mmap( 0, FILESIZE, PROT_READ, MAP_SHARED, fd, 0 );\n    }\n\n    if( tmpPointer == MAP_FAILED ) {\n        close( fd );\n        return false;\n    } \n    mapPointer = (char*) tmpPointer;\n\n    return true;\n}\n\nbool SharedMemory::unmapFile() {\n    int ret = munmap(mapPointer, FILESIZE);\n    close( fd );\n    if ( ret == -1 ) {\n        return false;\n    }\n\n    return true;\n}\n\nbool SharedMemory::set( std::string s ) {\n    /**\n     * Filter id, find the id in the file.\n     * If Values exist and id is valid, insert value\n     * -> if value not exists, insert "No_Service".\n     * if given id is invalid, return false\n     */\n    int mid = s.find( ";" );\n    int begin = s.find( "#" );\n    std::string id = s.substr( begin + 1, mid - begin);\n    std::string info = s.substr( mid + 1, s.length() );\n    if ( info == "" ) {\n        info = "No_Service";\n    }\n    char* i = strstr( mapPointer, id.c_str() );\n    while ( *i++ != \';\' );\n    for ( auto x: info ) {\n        *i++ = x;\n    }\n    for ( int j = 0; j < ( 20 - info.length() ); ++j ) {\n        *i++ = \' \';\n    }\n\n    return true;\n}\n\nbool SharedMemory::get( std::string& id ){\n    /**\n     * Filter id, find the id in the file.\n     * save data in string s.\n     */\n    int tmp;\n    try {\n        tmp = stoi( id );\n    } catch ( ... ) {\n        id = "No_Service";\n        return false;\n    }\n\n    if ( tmp > 100 || tmp < 1 ){\n        id = "No_Service";\n        return false;\n    } \n\n    id += ";";\n    char* i = strstr( mapPointer, id.c_str() );\n    while ( *i++ != \';\' );\n    id = "";\n    do {\n        id += *i++;\n    } while( *i != \' \' && *i != \';\' );\n\n    if ( id == "No_Service" ){\n        return false;\n    }\n\n    return true;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Ala*_*kes 6

尝试包含cstring(并调用std::strstr)或包含string.h.

这是在文档中指定的:http://en.cppreference.com/w/cpp/string/byte/strstr