Weird Compiler behavior (C++) - c++

I'm trying to add GA library (GALib) to my error-free program, when I add it, the compiler returns strange errors and repeat them so many times... For an example, "syntax error: missing '{' before '<' " is returned for the first line of the following template code:
template<class _Ty>
_Check_return_ inline _Ty _Pow_int(_Ty _Xx, int _Yx) _NOEXCEPT
{
unsigned int _Nx;
if (_Yx >= 0)
_Nx = static_cast<unsigned int>(_Yx);
else
_Nx = static_cast<unsigned int>(-_Yx);
for (_Ty _Zx = static_cast<_Ty>(1); ; _Xx *= _Xx)
{
if ((_Nx & 1) != 0)
_Zx *= _Xx;
if ((_Nx >>= 1) == 0)
return (_Yx < 0 ? static_cast<_Ty>(1) / _Zx : _Zx);
}
}
The error is in cmath.h
The error is repeated for the same line like for 25 times or so. The same for so many others. (The mentioned error is the first one on the list)
PS. I added the GA files using the following sequence:
1- Project properties>C++>Additional include libraries>select folder
2- Drag-Drop the folder containing the headers and sources to the project solution manager
PPS. All source files are with extension .C not .cpp
How can I solve such situation?

Following procedures in this page: msdn.microsoft.com/en-us/library/032xwy55.aspx Most errors just vanished (including the mentioned one).
The main problem was that the compiler deals with .C files with default option, which I changed to C++ as mentioned in aforementioned page.

Related

incorrect clion warning for 'condition always false'

I just coded this in my c++ project in clion
int step_size_x = someValue;
int step_size_y = anotherValue;
while (!(step_size_x == 1 && step_size_y == 1)) {
step_size_x = (step_size_x != 1) ? (step_size_x / 2) : step_size_x;
step_size_y = (step_size_y != 1) ? (step_size_y / 2) : step_size_y;
}
and clion gives me a warning, that the second statement (line 5) in my while loop step_size_y != 1 will always be false.
I could not comprehend how it comes to that conclusion so I quickly threw this into a c++ online shell - and it seemes to side with me.
Since I do not have that much experience with c++ I really am not convinced to have found a bug and am rather tending towards that the answer is stupefying obvious. In that case, please enlighten me, what am I missing here?
Note:
step_size_y will be set external files, therefore the compiler should be able to predict its value. Anyway, if it was 1 the warning would appear in the second half of the while()'s brackets.
Here is a screenshot of the warning:

How do I ensure that the read index of the directory object is rewound in FATFS?

I'm guessing this is a basic question: I'm using the f_readdir function in FatFs to read the contents of a directory from a command line, but I'm having trouble getting the function to run multiple times. It works fine the first go around: I type "directory" into the CLI and it displays every file in the directory on line at time. Asking it to repeat the f_readdir operation again, however, (i.e. by typing in the "directory" command again, after the first one has successfully completed) outputs nothing.
I believe this is because the file read object isn't being rewound back to zero at the end of f_readdir operation, and subsequent requests to read the directory are starting at a portion of the index that doesn't exist. That's the best explanation I can see at this point, at least. It says on Elm Chan's FatFs website that "When all directory items have been read and no item to read, a null string is stored into the fno->fname[] without any error. When a null pointer is given to the fno, the read index of the directory object is rewinded."
Here's my code. Ignore the function arguments, they're RTOS stuff to run the command:
void Cmd_directory::run(XString param, XRTOS::XCLI::userIO* io){
DIR dj; /*[IN] Directory search object */
FILINFO fno; /*[OUT] File information structure */
FRESULT res;
res = f_opendir(&dj, "0:");
while (res == FR_OK && fno.fname[0]) {
res = f_readdir(&dj, &fno);
io->print((const char*)fno.fname);
io->print("\r\n");
}
f_closedir(&dj);
}
This while loop was something I found on the web, so unfortunately I don't fully understand how the fname index works, as many times as I've read the detailed explanation. Perhaps it's not realizing it's hitting the end of the directory based on the conditions of my while loop, though it certainly completes and closes the directory successfully. When I run the function again, I can see that the fno object still has the file information stored in it from the previous go around.
Things I've tried (and at this point it's worth adding I'm quite new to the world of programming):
&fno = nullptr; //produces "error: lvalue required as left operand of assignment"
FILINFO *p = nullptr; //produces all sorts of errors
fno = p;
//these were shots in the dark
fno.fname[0] = 0;
memset(fno.fname, 0, sizeof(fno.fname));
I imagine this is some basic stuff that I'm juts not getting, though forgive me if I'm way off on this. I don't have access to a real programmer IRL unfortunately so I'm forced to poll the community.
Oh right, and I'm using the Eclipse IDE, GNU ARM Build tools with an STM32L.
Here:
while (res == FR_OK && fno.fname[0]) {
fno.fname[0] is unitialised - you get lucky the first time as it contains non-zero junk. The second time it likely contains whatever the function previously left in it - i.e. the NUL from the previous call - which terminates the loop immediately.
The following should work:
res = f_readdir( &dj, &fno ) ;
while( res == FR_OK && fno.fname[0] )
{
io->print((const char*)fno.fname);
io->print("\r\n");
res = f_readdir(&dj, &fno);
}
From your previous attempts fno.fname[0] = 0; was close, if only you'd not explicitly set to the loop termination value! The following minor change to your code should work too:
fno.fname[0] = 1;
while (res == FR_OK && fno.fname[0]) {
res = f_readdir(&dj, &fno);
io->print((const char*)fno.fname);
io->print("\r\n");
}
but it will print a blank line if the directory is empty.
To be honest the semantics of the ELM FatFs f_readdir() are somewhat odd. You might consider a wrapper to give it a more consistent and conventional interface:
FRESULT readdir( DIR* dp, /* [IN] Directory object */
FILINFO* fno /* [OUT] File information structure */ )
{
FRESULT res = f_readdir( dp, fno ) ;
if( res == FR_OK && fno->fname[0] == 0 )
{
res = FR_NO_FILE ;
}
return res ;
}
Then you can write (for example):
while( readdir( &dj, &fno ) == FR_OK )
{
io->print((const char*)fno.fname);
io->print("\r\n");
}

Error C2146: syntax error : missing ')' before identifier 'and' [duplicate]

This question already has answers here:
Why does VS not define the alternative tokens for logical operators?
(5 answers)
Closed 7 years ago.
I'm currently coding an app that will use OCR to read page numbers. I am using Visual Studio 2013 on a PC. I'm using C++ with OpenCV and Tesseract to complete this.
An error keeps on coming up and while I have come across similar problems, I can't find anything where it specific relates to the identifier 'and'. As such, I don't know how to fix this problem. Here is the section of code that it applies to:
vector<string> PgNrOCR::runRecognition(const vector<Mat> &pgnrImage, int pgnrType)
{
vector<string> output;
output.resize(pgnrImage.size());
for (size_t i = 0; i < pgnrImage.size(); i++)
{
if (!pgnrImage[i].empty() and pgnrType == 1)
output[i] = runPrediction1(pgnrImage[i], i);
if (!pgnrImage[i].empty() and pgnrType == 2)
output[i] = runPrediction2(pgnrImage[i], i);
}
return (output);
}
The 'and' identifiers in the if statement are bringing up the error, so I need to find an alternative solution. The full error appears as so.
Error 3 error C2146: syntax error : missing ')' before identifier 'and' c:\users\andrew\documents\visual studio 2013\projects\project1\project1\pgnrocr.cpp 152 1 PgTurn
I appreciate any help!
can you try && instead of and?

Can't find my mistake! error: expected identifier before '(' token

This is my main code, I did search related mistakes before asking but it just doesn't seem wrong...The IDE says the error is in line 11.
#include<stdio.h>
int main()
{
float sal;
printf("Digite o salário bruto: ");
scanf("%f",&sal);
if(sal<=2246.75){
printf("Salário líquido : ",sal);
}
else{
if(sal>2246.75)&&(sal<2995.70){
printf("Salário Líquido: ",sal * 0.925);
}
else{
if(sal>2995.70)&&(sal<=3743.19){
printf("Salário Líquido: ",sal * 0.845);
}
else{
printf("Salário Líquido: ", sal * 0.775);
return 0;
}
}
}
}
if(sal>2246.75)&&(sal<2995.70){
The problem is that the entire condition must be placed within a set of parentheses.
It's fine if you want to further enclose the sub-conditions, but you must surround the entire lot, too:
if ((sal > 2246.75) && (sal < 2995.70)) {
Your if statement has to correct as follows, here you were missing bracket() for if.
if( (sal>2246.75)&& (sal<2995.70)){
You have to specify the formatter for printf correctly as follows; here you are missing type formatter.
printf("Salário Líquido: %f", sal * 0.775);
Both these errors are there in multiple occasions in your code.
there are actually two major kinds of problems with the posted code.
printf("Salário líquido : ",sal);
is missing a format specifier for the 'sal' variable
it should be:
printf("Salario liquido : %f", sal);
Note: each of the printf() statements have this same problem
if(sal>2246.75)&&(sal<2995.70){
is missing the outside parens
it should be:
if( (sal>2246.75) && (sal<2995.70) ) {
Note: I added some horizontal spacing for clarity only
the last two 'if' statements have this same problem
Suggest compiling with all warnings enabled.
For gcc, at a minimum, use '-Wall -Wextra -pedantic'
main always returns an 'int'
To avoid that return code being a random value, always end the function with:
return(0);
I think that if(sal>2246.75)&&(sal<2995.70) is supposed to be
if(sal>2246.75 && sal<2995.70).

if and else without braces

I expect the following code to compile. Clang and VC++ both give me an error on the line with else.
void MyFunction(std::int32_t& error)
{
std::int32_t variable = 0;
if(GetSomething())
error = EOK;
else
error = ERROR;
}
If I put curly braces around error = EOK;, then it compiles. Why does VC++ say:
illegal else without matching if
?
My full code is below, replacing std::uint32_t with a typedef. It still gives the same error in VC++.
using sint32 = int;
#define ERROR 5;
#define EOK 0;
bool GetSomething();
void MyFunction(sint32& error)
{
sint32 variable = 0;
if (GetSomething())
error = EOK;
else
error = ERROR;
}
If your definition of EOK is as follows:
#define EOK 0;
then it would cause this type of error, because it forcibly terminates the if-statement before the else is reached, making it an else without a matching if. The compiler sees this code after macro replacement:
if(GetSomething())
error = 0;;
else
Here is a possible fix:
enum
{
EOK = 0,
ERROR = 5
};
Note that all identifiers starting with E followed by either another uppercase letter or a number are reserved for use as macro names by <cerrno>, so to avoid name clashes consider using a different naming convention for your errors.
To be implest and more efficient, you can do :
error = (GetSomething()) ? 0 : 5 ;
And if you want to with enum as Matt say , it become :
error = (GetSomething()) ? enum.EOK : enum.ERROR ;