问题
I need to get code work in Turbo C++. But the data type string
does not work;
using namespace std
gives Compiler Error and I can't use string
without it. Even std::string
does not work.
It works perfectly fine in Code::Blocks but I want it to work in Turbo C++. I know Turbo is a very old compiler and I should be using the new ones. But it is a college project which has to be done in Turbo C++. Are there any ways to make it work in Turbo C++?
回答1:
This kind of depends on which version of Turbo C++ you have. Some software archeology:
Ancient DOS versions up to 3.1 didn't support STL well, nor did they support #include <string>
. They used the pre-standard include formats with .h extensions: #include <string.h>
etc. Try to add a .h
and you might get lucky.
Somewhere around version 4 or 5.0 they started to support #include <string>
header formats and most of STL. These were still pre-standard compilers.
STL support remained questionable in earlier versions of Borland Builder, until somewhere around Builder 5. That should be version 5.5 or so of the BCC compiler.
The RAD tool called Turbo C++, released somewhere around 2005, should have full support for C++98.
回答2:
Turbo C++ doesn't support namespaces.
I think you need to include cstring.h
and not use any namespaces or even the using
directive.
#include <cstring.h>
And I don't think it supports templates either.
回答3:
There is absolutely no way whatsoever to make modern C++ code work in Turbo C++ as is. Lots of it needs to be rewritten.
There is nothing std:: in turbo c++. There are no namesoaces. There are no templates. There is very little of what we know as the standard library. Basically you have to unlearn most of what you know about C++. Classes and functiins mostly do work. Iostreams may somewhat work if you #include <iostreams.h>
(note the .h) and omit std::
. Otherwise you are pretty much confined to the C standard library.
If you need a string class, you probably will have to make your own.
Tread carefully, read the built-in help, examine the included example programs, and you might be able to pull it off.
Note, later versions of the product (not called Turbo C++ IIRC, but rather Borland C++ or Borland Builder) have improved support for C++98, including the standard library.
回答4:
I had the same problem then I realized that I had forgotten using namespace std
That solved everything.
回答5:
In place of String you can use the character array. For example, we need to declare the variable str as a string. It can be done simply as:
char a[10]; // the 10 is the size of the array.
A seperate header file is included to use the library functions.
回答6:
Okay, after a lot of hassle, I found the way. Unfortunately, you can't use string
and other such data types as they were not even implemented at that time. You need to do what used to be done before. Use char
array instead of string
and create functions related to that.
Now char
array has a lot of limitations and problems, that's the reason string
was implemented. But you have to write char
array functions the same way string
was written from scratch.
If you want to compare or copy two char arrays, you have to loop and compare them. It will be little complicated, but that's the best way which worked for me.
I can give some sample code for a certain task if needed.
来源:https://stackoverflow.com/questions/40398720/string-problems-with-turbo-c