-5

I use g++ to compile the .c file(in that file is write by c++) enter image description here and then, it said: enter image description here I don't think my file was wrong, however the file looks like:

    #include <iostream>
    #include <fstream>    // File i/o.
    using namespace std;

    const short int VERTICES = 318;
    const short int n        = 318;
    const int       LARGE    = 10000.0;
    const int m        = n*(n-1)/2;

Can anyone tell me how to solve this problem?

MD XF
  • 7,860
  • 7
  • 40
  • 71
wwang
  • 19
  • 1
  • 3
  • it's just a warning, and you explicitly asked for all warnings with the `-Wall` flag. It's not a problem, it's just telling you "hey, you wasted time/effort defining this variable, then never used it". – Marc B Sep 20 '16 at 19:28
  • I can't remove this variable, I need it in the text. – wwang Sep 20 '16 at 20:52
  • And, the file is really long, however, after I write g++ things, the computer didn't give me an answer of my file. – wwang Sep 20 '16 at 20:53
  • ***the computer didn't give me an answer of my file.*** Then you have a bug in your code. It is highly unlikely the two have anything to do with each other. Time to learn how to use your debugger. – drescherjm Sep 20 '16 at 21:39
  • 1
    Also why are you posting images of text instead of the text itself? – drescherjm Sep 20 '16 at 21:40

4 Answers4

4

If the variable is unused, then remove it.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
  • @J.Chomel I think you are quite wrong. The question asks how to deal with the warning, and it can be dealt with by removing the definition. – Brian Bi Sep 21 '16 at 19:02
  • This could easily be a comment. But you as well (and how much more!) as me decide what should be on the site. – J. Chomel Sep 22 '16 at 06:11
  • You might also want to declare it extern before const, or use C++17 inline variables as shown at: https://stackoverflow.com/questions/177437/what-does-const-static-mean-in-c-and-c/53883715#53883715 – Ciro Santilli OurBigBook.com Feb 03 '19 at 21:12
2

I think you can say:

__unused const int m = n*(n-1)/2;

To suppress the warning.

Or perhaps more correctly:

const int m __attribute__ ((unused)) = n*(n-1)/2;

See the documentation.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
0

Note: Why do use the extension ".c" for a C++ file? It would be more correct to use '.cpp'

Now - here is your answer:

First of all - it is NOT an error - it is a huge difference between an error and a warning. Any warning can be ignored - it is not the same with errors.

It looks like you are not using the constant in your code (you are just declaring it). If this happens, G++ will tell you that you might want to remove it.

  • Well, yes, you can't ignore an "error", because that means the compiler refused to compile the code. A "warning" can sometimes reflect a serious problem, and should only be ignored if you understand what it refers to. – Pete Becker Sep 20 '16 at 19:46
0

This is just a simple warning and you're free to ignore it - it's not an error, so your code compiled fine. It wouldn't have if you passed the -Werror to g++, which turns all warnings into errors.

If you are going to use this variable someday in your code, just keep it or comment it out until you need it. Else, you can remove it safely : That's what the compiler warned you about, it's unused.

To silence out this warning, you can pass -Wno-unused-variable to g++. clang probably will accept this flag as well.

Do note this warning exists for a reason : It can highlight issues you didn't see. I stumbled upon this question on SO, for example.

Community
  • 1
  • 1
asu
  • 1,875
  • 17
  • 27