Problems compiling and executing this code in C++ - c++

I'm trying to compile this code in TurboC++ 3.0. However, I got these errors:
DOS.H 77: Too many types in declaration
DOS.H 77: { expected
DOS.H 77: Declaration does not specify a tag or an identifier
SARSAL.CPP 72: Cannot cast from 'int' to 'time'
I checked the directories of the libraries and I have run the code in BorlandC++ 5.02 (unfortunately, I get the graphics error or this error: Constructor cannot have a return type specification, in the method void Agente::Agente), DevC++ and Code::Blocks without success.
The code was provided by our AI teacher and supposedly works fine. How do I get it to compile?
Thanks for the help.

I normally wouldn't answer this kind of post (and not just because of the "TurboC++" issue) but we were all newbies at some point and needed help but didn't know how to ask for it, so I'll give you a hand.
First and foremost: DON'T USE TurboC++. As others have said, it's ancient and will require you to learn a language that's very different than the C++ of today and will teach you many many bad habits (e.g. #include <iostream.h> which is wrong).
With that out of the way, let's get started, shall we?
You define a constructor (around line 70) and give it a return type of void. This is wrong: constructors don't have return types. The correct syntax is:
Agente::Agente(void)
{
randomize();
}
Perhaps TurboC++ requires a return type (see?) or perhaps this was just your mistake, but either way, this is a bug because that is not C++ code.
Moving forward, you have this on line 127:
if((Archivo = fopen("C:\Documents and Settings\ArCiGo\Escritorio\SOFTWARE_2\DATOS.TXT","r"))!=NULL)
The character \ is special in C++ (e.g. \n represents a newline and \x01 is the character with value 1.
If you want to use it, you must escape it with another \ like this:
if((Archivo = fopen("C:\\Documents and Settings\\ArCiGo\\Escritorio\\SOFTWARE_2\\DATOS.TXT","r"))!=NULL)
There are other places where you do the same thing. Fix those and try again. I bet that you will have a lot better luck and fewer errors to worry about.
For future reference, when you are looking for help try to post a SHORT, self-contained program that exhibits the error that you are getting, so that others don't need to wade through hundreds of lines of code and worry about missing header files and platform-specific differences.

Related

A lot of ambiguity errors using C++ Builder 10 Seattle

I have gotten a lot of ambiguity errors when compiling older code in the new C++ Builder 10 Seattle. For functions like log() for example.
Calling log(10) generates the following error.
[bcc32 Error] E2015 Ambiguity between 'std::log(float) at c:\program
files (x86)\embarcadero\studio\17.0\include\windows\crtl\math.h:394'
and 'std::log(long double) at c:\program files
(x86)\embarcadero\studio\17.0\include\windows\crtl\math.h:430'
Feels like it should be able to handle that conversion. A warning, fine... but an error and unable to compile? Has the compiler gotten way more strict about this with later versions? Code is originally from C++ Builder 2010.
Other ambiguity errors include void* when HWND was expected, doing things like arithmetic on TDateTimePicker->Time (->Time.Val must be used now instead) etc. This was swallowed by the compiler before but not now. I'm happy it seems to be stricter now though... But it brings with it a lot of fixes to the old code.
It's because you gave it an integer and it doesn't know what the resulting floating-point type should be. If you said log(10.0) the type defaults to double and it has no ambiguity. Just because the original integer had no suffix doesn't mean the compiler can assume you want a no-suffix version of the converted floating-point value. C++ gets a lot more picky about strong typing as the versions wear on, so this sort of insistence popping up in newer compilers isn't surprising.
NB: Just adding a suffix won't work. 10f doesn't mean it's a float. You need the decimal or the exponent (or both), also, so 10.f works and 1e1f works and 1.e1f works as well.

How can I declare variables with illegal names (such as "int double = 0")?

I have tried to do this but I'm unable. How can i declare a number of variables with legal and illegal names (such as int double = 0;), so that you can see how the compiler reacts.
The short answer to this is DON'T DO IT.
There are a number of reserved words in the C and C++ standards that should not be used for any purpose other than which they are originally intended. Going out of your way to recycle these for your own perverse purpose is going to create problems for a lot of people. One of those people might be yourself fin the future when you have to fix a bug.
If you want to use double as a variable name, the best method to make this happen is to successfully petition the C++ committee constructing the next standard to allow it. Then you will have a valid program.
If you want to see how the compiler behaves when encountering this problem, create tiny programs that are as small as practical. For example:
// invalid_double.c
int double = 0;
You'll immediately see a syntax error when trying to compile that. Repeat as necessary with other keywords. This is often how things like configure run tests to verify the behaviour and capabilities of the local compiler.
Your compiler will probably halt compilation at the first invalid use of a keyword so you may need to construct one file per experiment. Subsequent errors in the same file may be ignored, such as if you had int class = 0
it is like a Quote from (Programming__Principles_and_Practice_Using C++),I think the auther asks us to try it and see the error no more(just we try it ourselfes).

boost::bind doesn't work in VC++ 2010 when binding a function that throws exceptions

I have some code which compiles fine under Linux, but I am trying to port it to Windows. I have used the Boost 1.50 precompiled binaries from Boost Pro, but when I compile my code I get this cryptic error:
error C2664: 'boost::_bi::bind_t<R,F,L>::bind_t(const boost::_bi::bind_t<R,F,L> &)' :
cannot convert parameter 1 from 'boost::_bi::bind_t<R,F,L>'
to 'const boost::_bi::bind_t<R,F,L> &'
C:\Program Files (x86)\boost\boost_1_50\boost\bind\bind_cc.hpp [line] 50
The error is most unhelpful because it shows up deep in the Boost header files, with no indication of where in my code the problem is. Nevertheless by commenting out various blocks of code I have narrowed it down to this as the cause:
void test(int a)
throw (int) // removing this line makes it compile
{
return;
}
...
boost::function<void(int)> fn = boost::bind<void>(test, _1);
It works if I remove the throw specifier in the function definition. It doesn't matter what I throw, whether it's a class or just an int. Am I doing something wrong, or can't you bind to functions that throw exceptions in Visual C++? The Boost Bind docs don't seem to suggest any issues with this, and GCC doesn't have a problem with it either way.
[Side note: The code above is not my actual code, but when compiled it exhibits the same problem. Please avoid comments about throwing ints being bad and the like, as this is only supposed to be a trivial example in case anyone wishes to reproduce the problem.]
I don't know why your code fails on VC++. However, in general exception specifications are best avoided because they can introduce very subtle effects. See this excellent column A Pragmatic Look at Exception Specifications by Herb Sutter:
So here’s what seems to be the best advice we as a community have
learned as of today:
Moral #1: Never write an exception specification.
Moral #2: Except possibly an empty one, but if I were you I’d avoid
even that.

Strange compiler error due to use of xml in c++ comments

I'm working on a proprietary unix-like os (I don't know if that's relevant though) and compiling with g++.
I noticed recently that if I put xml-like tags in my C++ comments I get compiler errors. I don't particularly need to do this, but I thought it was strange and I'd like to know why it's an issue for the compiler. For example:
// <debugoutput>
std::cerr << "I'm debugging!" << std::endl;
// </debugoutput>
would cause massive compiler errors if it were in the middle of my code somewhere. Changing the last comment line </debugoutput> to <debugoutput> makes it compile fine though.
Does anyone know why the compiler would be confused by that line being in a comment? The compiler errors generated when this happens don't seem related at all - they're more like what you'd see if you missed the semi colon on the end of a class, undefined references to well defined classes, etc. I can't paste the output from my dev system, but trust me that it doesn't look related to the issue - its more like the compiler got confused.
This sounds suspiciously like a digraph related issue, but without the actual error message or a small code sample that exhibits the problem it's hard to tell for sure.
Try changing the whitespacing between the <, / and actual text, as well as try it within a C-style comment to see if that provides additional insight.
For information on C/C++ digraphs and trigraphs see http://en.wikipedia.org/wiki/C_trigraph#C and also Purpose of Trigraph sequences in C++? and Why are there digraphs in C and C++? from SO.
It seems possible that there is some sequence being picked up (for example </ as a digraph and it's throwing off the compiler).

VC++ compiler and type conversion?

When I moved a program from a Mac to this Windows PC, the VC++ 2008 compiler is giving me errors for passing unsigned ints to the cmath pow() function. As I understand, this function is not overloaded to accept anything but floating-point numbers.
Is there some compiler flag/setting that will ignore these errors? Also does anyone know how to find the documentation on the VC++ compiler?
Edit
This isn't a warning, it's an error. However, for me it's not an issue since my program is only dealing with numbers that come out as integers, so I don't care that they aren't floats. If it was just warnings I would move on with my life, but it's not letting me compile. Can I suppress errors somehow? Like I said, the errors aren't coming up on my Mac and the program is fine.
Regarding other answers here, it is not a good idea to tell the question author to turn off this warning. His code is broken - he's passing an unsigned int instead of a float. You should be telling him to fix his code!
This isn't a warning, it's an error. However, for me it's not an issue since my
program is only dealing with numbers that come out as integers, so I don't care that
they aren't floats. If it was just warnings I would move on with my life, but it's not
letting me compile. Can I suppress errors somehow? Like I said, the errors aren't
coming up on my Mac and the program is fine.
Integers and floats use different representations internally. If you have the same number in an int and a float, the bit pattern inside the storage for them is completely different. You cannot under any circumstances whatsoever expect your code to work if you are passing an integer when you should be passing a float.
Furthermore, I assert your Mac code either is silently using an overloaded version of that function (e.g. you are on that platform compiling with C++) or you believe it works when in fact it is working by chance or is not actually working.
Addendum
No compilers ever written has the ability to turn off errors.
A warning means the compiler thinks you're making a mistake.
An error means the compiler doesn't know what to do.
There are a couple of options:
In C, the solution is simply to cast the ints to doubles:
pow((double)i, (double)j)
In C++, you can do the same, although you should use a C++-style cast:
pow(static_cast<double>(i), static_cast<double>(j))
But a better idea is to use the overload C++ provides:
std::pow(static_cast<double>(i), j);
The base still has to be a floating-point value, but the exponent can be an int at least
The std:: prefix probably isn't necessary (most compilers make the function available in the global namespace as well).
Of course, to access the C++ versions of the function, you have to include the C++ version of the header.
So instead of #include <math.h> you need to #include <cmath>
C++ provides C++ versions of every C header, using this naming convention. If the C header is called foo.h, the C++ version will be cfoo. When you're writing in C++, you should always prefer these versions.
I don't know of a flag, but getting rid of the warnings was easy enough for me. Just double click on each of the warnings in the "Task List" and add the appropriate casting, whether you prefer
(double) my_variable
or
static_cast<double>(my_variable)
I'm guessing if you're getting the ambiguous warning, there are multiple pow functions defined somewhere. It's better to be explicit in my opinion anyway. For what it's worth, my vote goes with the static_cast option.
As Mehrdad mentioned, use the #pragma warning syntax to disable a warning. Documentation is here - http://msdn.microsoft.com/en-us/library/2c8f766e.aspx
I would be inclined to fix the warnings rather than hide them though!
C++ has overloads for pow/powf for int exponents. Heed the warning.
Don't ignore this or any warnings. Fix them. The compiler is your friend, trying to get you to write good code. It's a friend that believes in tough love, but it is your friend.
If you have an unsigned int and need a float, convert your unsigned in to a float.
And the MSDN Library is the documentation for both the VC++ implementation of the language and the IDE itself.