I have a very simple bit of code that won't work, and have no idea why
The following:
int flag = 0;
if (flag == 0)
{
flag = 1;
}
Will not compile. It is already a quite complex program, and I am able to do other actions within the program with no problems at all, yet for some reason I can't reference a variable I have just created. The variable name is unique, and the application is a Windows app including windows.h. It is written in C, and up until now I have not attempted to create my own variables.
I can't publish the full code here, least of all because there's pages of it, but can anyone speculate as to why it can't compile? I am using Visual C++ and have the following errors:
syntax error : missing ';' before 'type' (this applies to line 1)
'flag' : undeclared identifier (line 2)
'flag' : undeclared identifier (line 4)
I have tried using bool as well, with 'true' and 'false' in place. I am relatively new to C++. Note that the code compiles fine without it here at all. It comes immediately after a previous action within a larger 'if' statement, of which this is a part. I have successfully added other nested if statements in the exact same place as this. Taking it outside the if statement entirely makes no difference. Putting it all right at the top of my main.c file, just after the #includes, makes no difference. Removing it completely means the program compiles absolutely fine. The problem seems to lie with defining the variable.
I assume you compile it to C language. You need to declare the variable at the beginning of the block:
// beginning of block
int flag=0;
//Some code
if (flag == 0)
{
flag = 1;
}
are you missing a ; at the end of the preceding line?
Looks to me, as if the line before the variable declaration has not been finished with a ;. Check this line for the missing semicolon.
Related
Can you please upvote this post since others show interest in answering and commenting on it? The guy who likes to devote things clearly doesn't know how to remove devoting or upvote a post back.
I'm trying to use labels in my project but when I jump over a set of instructions using goto to transfer control to another section of the code, I get this error that says: transfer control bypasses initialization of (certain variables).
This is the code that produces the error:
goto label1;
label00:
int a = 0;//the compiler can't let me skip this line
int b; // but this line is fine to skip over
b = 0; //because i initialize it here instead of doing it like the a variable
label1:
//other instructions
as you can see I have two variables initialized but one of them is defined then initialized but the other one is defined and initialized at the same line.
The one that is defined and initialized at the same line variable a does not produce an error when skipped over, but the other one does.
I'm using VS2019 to compile this code. I think this should not throw an error at all and the compiler should give you a warning so that you know you're skipping something in both cases a and b initializations.
Is there any solution to this like disabling something in the settings?
I don't want to declare my variables then initialized them when using labels.
I think this should not throw an error at all
The compiler is free to refuse to compile ill-formed programs.
Is there any solution to this
Solutions:
Don't initialise a.
Declare a before the jump.
Declare a after the label.
Don't use the goto (my favourite).
Limit the scope of a by declaring it within a block statement that ends before the label.
I'm trying to write a very simple piece of code for a class, but I'm just stumped as to why I can't compile it. Sorry if this is a duplicate or silly question, but I couldn't find any others that answered this for me. My full program is pasted below. When I try to compile, I get the following error:
test.c: In function 'main':
test.c:7:27: error: expected ';' before '{' token
Here's the code:
#include<stdio.h>
#include<linux/sched.h>
#include<linux/kernel.h>
int main(){
struct task_struct *task;
for_each_process(task){
printf("I found task: %d\n", task->pid);
}
return 0;
}
I feel like I'm missing something painfully obvious, can anyone point out what the problem is here? I've tried initializing the 'task' object as NULL and using a simpler printf statement that just prints 'test', but nothing I've tried has fixed this compilation error.
The macro has been moved to <linux/sched/signal.h> in this commit c3edc4010e9d102eb7b8f17d15c2ebc425fed63c in 2017. (https://github.com/torvalds/linux/commit/c3edc4010e9d102eb7b8f17d15c2ebc425fed63c).
(For someone who has trouble in compiling for this reason.)
The preprocessor token you are using for_each_process is either not defined, or not defined to do what you think it does.
Every C++ compiler I've used can be told to dump the post-preprocessing output. If you pass the flag that makes this happen when building your source file, you'll see the code that the compiler is seeing and screwing up on.
gcc -E clang -E or the /E flag in visual studio (heh) for example.
As #Ulrich has mentioned above, apparently that macro is only available within the kernel. Attempting to read <linux/sched.h> directly and determine this is challenging, as there are many, many ifdef/endif pairs.
I have a file that contains an arbitrary number of lines of c++ code, each line of which is self-contained (meaning it is valid by itself in the main function). However, I do not know how many, if any, of the lines will have valid c++ syntax. An example file might be
int length, width; // This one is fine
template <class T className {}; // Throws a syntax error
What I want to do is write to a second file all the lines that have valid syntax. Currently, I've written a program in python that reads each line, places it into the following form
int main() {
// Line goes here
return 0;
}
and attempts to compile it, returning True if the compilation succeeds and False if it doesn't, which I then use to determine which lines to write to the output file. For example, the first line would generate a file containing
int main() {
int length, width;
return 0;
}
which would compile fine and return True to the python program. However, I'm curious if there is any sort of try-catch syntax that works with the compiler so I could put each line of the file in a try-catch block and write it to the output if no exception is thrown, or if there's a way I can tell the compiler to ignore syntax errors.
Edit: I've been asked for details about why I would need to do this, and I'll be the first to admit it's a strange question. The reason I'm doing this is because I have another program (of which I don't know all the implementation details) that writes a large number of lines to a file, each of which should be able to stand alone. I also know that this program will almost certainly write lines that have syntax errors. What I'm trying to do is write a program that will remove any invalid lines so that the resulting file can compile without error. What I have in my python program right now works, but I'm trying to figure out if there is a simpler way to do it.
Edit 2: Though I think I've got my answer - that I can't really play try-catch with the compiler, and that's good enough. Thanks everyone!
Individual lines of code that are syntactically correct in the context of a C++ source file are not necessarily syntactically correct by themselves.
For example this:
int length, width;
happens to be valid either as part of a main function or by itself -- but it has a different meaning (by itself it defines length and width as static objects).
This:
}
is valid in context, but not by itself.
There is typically no way for a compiler to ignore syntax errors. Once a syntax error has been encountered, the compiler has no way to interpret the rest of the code.
When you're reading English text, adfasff iyufoyur; ^^$(( -- but you can usually recover and recognize valid syntax after an error. Compilers for programming languages aren't designed to perform that kind of recovery; probably the nature of C++'s syntax would make it more difficult, and there's just not enough demand to make it worth doing.
I'm not sure what your criterion for a single line of code being "correct" is. One possibility might be to write the line of code to a file, contained in a definition of main:
int main() {
// insert arbitrary line here
}
and then compile the resulting source file. I'm not sure that I can see how that would be particularly useful, but it's about the closest I can come to what you're asking for.
What do you mean by "each line is self-contained"? If the syntax of a line of C++ code is valid may depend largely on the code before or after that line. A given line of code might be valid within a function, but not outside a function body. So, as long as you can't define what you mean by "self-contained" it is hard to solve your problem.
does anyone knows if its possible to set a multiple condition breakpoint on a specific line in Visual Studio 2013 (C++) ?
I was trying using the '&&' but it didn't worked. I also couldn't find an answer on MSDN.
the breakpoint that i wanna set is inside the WindowProc, the condition that i wanna set is - message = WM_MOUSEMOVE, WPARAM = MK_LBUTTON
thanks in advace, Igor.
WM_MOUSEMOVE is a macrodefinition and gets replaced in the source code by the compiler during compilation. It is unknown to the debugger, so you can't use it in a breakpoint condition expression; use explicit number constant instead.
BTW, are you aware you used operator '=', which is not the same as '=='...?
Using && is allowed and should work. What's more, a lot of common C++ expressions are allowed. This page lists what is and what isn't allowed.
Note that using this kind of breakpoint will considerably slow down your application. To the point where debugging is no longer feasible. This might be what has led you to believe && isn't allowed. To overcome this particular problem you might want to use a construct like this:
//untested code
#ifdef _DEBUG
if(condition a && condition b)
{
//either output something (option A)
std::cout << "condition a and b are true"
//or create a nop statement (option B)
__nop(); //and set a breakpoint
//or create a 'nop statement' with compiler warning (option C)
int breakpoint = 0;
}
#endif
This will yield much better performance.
Since this code is only compiled in when you are compiling in debug, you can leave this bit of code in (and option B would therefore be the best). If you however want to be reminded to remove the debugging clause, option C is probably the way you wanna go. As this will generate a variable breakpoint is declared but never used warning. As kindly suggested by borisbn.
If you are using this statement a lot it's probably most useful to wrap it into a precompiler macro.
While compiling a while loop without a body, I saw this message:
warning: suggest a space before ';' or explicit braces around empty body in
'while' statement
Why should I put a space? Is it to make it clearer that my loop isn't really doing anything?
Yes -- the usual convention is to put it on a line by itself, something like this:
while (*d++ = *s++)
;
A few people prefer to use things like:
while (*d++ = *s++)
{
/* intentionally empty */
}
Why should I put a space? Is it to make it clearer that my loop isn't really doing anything?
Yes, exactly that.
Many inexperienced programmers often put the ; after a control flow statement like for or while because they assume that ; goes at the end of every line, and not just at the end of statements and declarations. The warning is just suggesting to make it clearer that you understand that you've created an empty loop.
A more obvious empty-body loop syntax is to use the continue statement:
while (condition_with_side_effects)
continue;
That way no one will think you've accidentally added an extraneous ;:
while (condition); // bad style
Yes, it is just to show that you have not automatically placed a semicolon at the end of the line by mistake.