我正在通过 Visual Studio(Windows)为我的班级开发一个项目,因为我更喜欢编译器,但是当我将代码复制/粘贴到 ubuntu linux(linux mint cinnamon)并运行 g++ 编译器和 cmake 时在此基础上构建,字符串函数似乎没有正确定义,我收到以下错误。
导致问题的主要代码在这里:
int main() {
//start up
//load commands.csv into a singly linked list
srand(time(NULL));//starts up a randomized seed
fstream commandFile;
commandFile.open("commands.csv");
string line = "";
linkedList commandList;
Profile* profileList = NULL;
int profileSize = 0;
while (getline(commandFile,line)) {//while loop will run as long as there is a line to read
string name;
string description;
stringstream inputString(line);//stringstream is used for more string functionallity
getline(inputString, name, ',');
getline(inputString, description);
description.erase(remove(description.begin(),description.end(),'\"'),description.end());//removes all quotation marks from a string.
Command data;
data.setName(name);
data.setDescription(description);
commandList.insertNode(data);//commands are being stored in a singly linked list
line = "";
}
commandFile.close();
或更具体地说这一行:
description.erase(remove(description.begin(),description.end(),'\"'),description.end());
目标只是从本网站上显示的给定字符串中删除给定字符:https://www.tutorialspoint.com/how-to-remove-certain-characters-from-a-string-in- cplusplus
虽然我可以编写一个新函数来为我做到这一点,但我想知道这个问题是否可以通过包含我没有的额外库来解决,或者是否存在一些我在从windows 上的 Visual Studio 到 Linux 上的 g++ 编译器可以修复。
将鼠标悬停在 vs code 中的description.begin 和description.end 上会显示此错误:
不存在从“__gnu_cxx::__normal_iterator
>”到“const char *”的合适转换函数C/C++(413)
我已经解决这个问题几天了,因此我们将不胜感激!
编辑: 因为这可能很重要,所以我使用的库如下:
#include<iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cstdlib>//randomizer library
#include <time.h>
问题很简单。您需要包含标题
<algorithm>
#include <algorithm>
否则编译器会找到 C 函数
remove
int remove(const char *filename);
在标头中声明
<cstdio>
因为(C++ 14 标准、27.4 标准 iostream 对象)
1 标头声明了将对象与 由中声明的函数提供的标准 C 流
(27.9.2),并包含使用这些所需的所有标头 物体。<cstdio>
我也面临这个问题,但我只需在下面添加一行代码 include iostream 就可以了。
using namespace std;