stoi compile error

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

stoi compile error

dmoeller
I am programming in c++ for the first part of VM. I get an error "stoi" was not declared in this scope. Im not sure why. I am using namespace std, and I have included iostream, fstream, and string. here is the context:

int Parse::part2()
{
        Vcom command=commands(); //get the command type
        if(command==PUSH||command==POP||command==FUNC||command==CALL) //if push, pop, function, or call command...
        {
                int first=currcom.find(" ",0); //get the first part of the argument, seperated by a blank space
                int second=currcom.find(" ",first+1); //get the second part of the argument
                int third=currcom.find(" ", second+1); //get the third part of the argument
                string line=currcom.substr(second+1, third-second-1); //meld the argument together into a string
                int lineInt=stoi(line); //convert the string to an integer
                return lineInt; //return the argument
        }
}

the bold text is where I have my error
Reply | Threaded
Open this post in threaded view
|

Re: stoi compile error

cadet1620
Administrator
dmoeller wrote
I am programming in c++ for the first part of VM. I get an error "stoi" was not declared in this scope.
What compiler are you using?  stoi() is fairly new.  You might need to use atoi().

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: stoi compile error

dmoeller
im using bloodshed dev c++. I will try atoi and see if that works.
Reply | Threaded
Open this post in threaded view
|

Re: stoi compile error

dmoeller
atoi() also did not work. Is there a way to convert a string to an int if the components of the string are integers? as listed in my code above, first, second, and third are all integers. maybe the string line=currcom.substr() command is the culprit?
Reply | Threaded
Open this post in threaded view
|

Re: stoi compile error

cadet1620
Administrator
atoi() is a C function.  You will need to #include <stdlib.h> and use it without std::

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: stoi compile error

dmoeller
now i get "[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'int atoi(const char*)'"
Reply | Threaded
Open this post in threaded view
|

Re: stoi compile error

cadet1620
Administrator
Since atoi() is a C function, it needs a C (char*) string.  Try
    int lineInt= atoi(line.c_str()); //convert the string to an integer
Sorry I forgot to mention that. It's been too long since I've done C++ programming using std::string.

Googling for "c++ std::string to int" will turn up some more C++ish ways to do it using string I/O streams, etc., but I find those way over the top. Like using a canon to swat flies 8-)

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: stoi compile error

dmoeller
ok I got that part fixed, now for CodeOut.cpp and Parse.cpp i get the same error. Line 18, undefined reference to 'WinMain' but the code is COMPLETELY different...im not sure what this means
Reply | Threaded
Open this post in threaded view
|

Re: stoi compile error

cadet1620
Administrator
WinMain() is the entrypoint for Windows GUI apps.

Sounds like you have the wrong compile options and are building a Windows GUI app instead of a command line app.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: stoi compile error

dmoeller
I have no idea how that happened... I don't even know how to do that. How do I fix that?
Reply | Threaded
Open this post in threaded view
|

Re: stoi compile error

cadet1620
Administrator
STEP 1:  Be sure to save copies of your sources somewhere safe in case MVS mangles them for you...

It's been years since I've used Visual Studio.

Assuming that's what you are using, the easiest thing to do is use the new project wizard to create a new Win32 Console Application and then add your sources into it.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: stoi compile error

dmoeller
im actually using bloodshed dev c++ and I created a new project but I still get the same error.
Reply | Threaded
Open this post in threaded view
|

Re: stoi compile error

cadet1620
Administrator
Oops. Just rescanned the thread and see that you mentioned that.

I don't know anything about bloodshed.  The only idea that comes to mind is if you did your Assembler in bloodshed, clone that project and try substituting in your VM Translator code.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: stoi compile error

dmoeller
i was never able to finish my assembler program before it was due, i was using notepad++ for editing those...
Reply | Threaded
Open this post in threaded view
|

Re: stoi compile error

cadet1620
Administrator
This post was updated on .
I don't think that I can help you with this.  I've never had much luck using anything but Microsoft's C/C++ compilers on Windows.

If you need to use C++ I think that you may need to find somebody local that can help you get your C++ environment working, or move to the latest free version of Visual Studio.
    https://www.visualstudio.com/free-developer-offers/

I did all my n2t programs in Python.  Most students appear to be using Java.  Both work well on Windows.

--Mark