0

I am using the up to date compiler for gcc. Therefore my code can execute to_String() and stoi() but unfortunately the platform that I am trying to compile my code in has a much previous one and I want to add these two functions into the string class therefore I can use them in my code without any problem.This is one of each example of the errors I get.

Cabinet.cpp:93:25: error: 'to_string' was not declared in this scope
 cout << to_string(id) << " not found"<<endl;
LabOrganizer.cpp: In member function 'void LabOrganizer::findClosest(int, int, int)':
LabOrganizer.cpp:294:38: error: 'stoi' was not declared in this scope
        cc = stoi(arr1[i].substr(1,1))-1;
13
  • 1
    Have you tried including the namespace, such as std::to_string ? Commented Nov 15, 2021 at 18:47
  • 1
    @AlpE don't do using namespace std; and do include the correct headers for the functions you wish to call. Commented Nov 15, 2021 at 18:50
  • 4
    with 1998 gcc are you an archeologist? Commented Nov 15, 2021 at 18:52
  • 1
    The only practical way of "adding these two functions into the string class" is to update your 1998 standard library and compiler with recent ones. If it cannot be done for whatever reason, you'll need to write in 1998's subset of C++. Commented Nov 15, 2021 at 18:54
  • 1
    Neither of these is a member of std::string (or any other class). Nothing prevents you from implementing them, but put them in your own (or the global) namespace. Commented Nov 15, 2021 at 19:11

1 Answer 1

1

These two functions aren't part of the "string class". All you need is to use alternatives that existed in 1998:

  • The to_string function isn't even needed in this context. You can simply change it to:

    cout << id << " not found" << endl;
    
  • You can replace stoi with atoi. It doesn't throw exceptions if the conversion fails, but since it's a school assignment -- you most probably don't care about that:

    cc = atoi(arr1[i].substr(1,1).c_str())-1;
    

If you have many instances whose replacement is too cumbersome, you can, of course, define those functions in some common header file:

template<class T>
std::string to_string(const T &x) {
    std::ostringstream s;
    s << x;
    return s.str();
}

inline int stoi(const std::string &s) {
    // ... ignores error handling ...
    return atoi(s.c_str());
}

Hopefully this compiles with that 1998 GCC.

Sign up to request clarification or add additional context in comments.

1 Comment

you are simply the best thank you .)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.