Home > Blockchain >  String functions from Windows Visual Studio seem to not work when compiled through g on Ubuntu(Lin
String functions from Windows Visual Studio seem to not work when compiled through g on Ubuntu(Lin

Time:10-03

I was working on a project for my class through visual studio(windows) because I like the compiler a bit more, but when I copy/pasted the code to ubuntu linux(linux mint cinnamon) and ran both a g compiler and a cmake build on it, the string functions did not seem to be defined correctly and I received the following errors. enter image description here

enter image description here

The main code which has been causing the issue is here:

 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();

or more specifically this line:

description.erase(remove(description.begin(),description.end(),'\"'),description.end());

The goal is simply to remove a given character from a given string which is shown on this website: https://www.tutorialspoint.com/how-to-remove-certain-characters-from-a-string-in-cplusplus

While I could just write a new function to do this for me, I want to know if this problem can be resolved by including an extra library I don't have, or if there is some difference I'm not seeing when going from visual studio on windows to g compiler on linux that can be fixed.

hovering over description.begin and description.end in vs code shows this error:

no suitable conversion function from "__gnu_cxx::__normal_iterator<char *, std::__cxx11::basic_string<char, std::char_traits, std::allocator>>" to "const char *" existsC/C (413)

I have been tackling this problem for a few days so any help would be greatly appreciated!

Edit: Since this may be important the libraries I used are the following:

#include<iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cstdlib>//randomizer library
#include <time.h>

CodePudding user response:

The problem is simple. You need to include the header <algorithm>

#include <algorithm>

Otherwise the compiler finds the C function remove

int remove(const char *filename);

declared in the header <cstdio> because (the C 14 Standard, 27.4 Standard iostream objects)

1 The header declares objects that associate objects with the standard C streams provided for by the functions declared in <cstdio> (27.9.2), and includes all the headers necessary to use these objects.

  • Related