- 使用变量编译器说ERROR [英] -Wunused-variable compiler says ERROR

查看:253
本文介绍了 - 使用变量编译器说ERROR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始在C ++中编程(我来自Java,我花了一点点改变哈哈)。在Windows下一切权利。问题是,我切换到Linux,这是我有编译器的问题。它通常是当你声明一个变量,不使用,编译器显示一个警告说,该变量不使用,但我(在linuxmint 15)抛出我为错误,没有编译:C.我做不知道有没有发生这种情况,但我在生产大型程序时(多个类)。



一个例子:

解决方案

未使用的变量警告是使用


$ b调用GCC的结果$ b

  g ++ -Wunused-variable ... 

如果是这种情况,请不要指定该参数。或者是因为-Wall:

  g ++ -Wall ... 
pre>

在这种情况下,指定-Wno-unused-variable



它被抛出为错误,也有'-Werror'参数。



这个警告有几个原因:在C ++的范围之间可以阴影变量,因此是一个常见的原因未使用的变量是当有两个同名的变量时。

  int i = 5; 
for(int i = 0; i <10; ++ i){//<这是SECOND变量,称为i隐藏以前的
}
if(day ==Monday){
int i; //<<你不能看到第二个我在这里,这是第三也隐藏第一。
...
}
// std :: cout< i =< i<< std :: endl; //我们可以看到原来的我再次在这里

有两个 我这里。如果你取消注释最后一行,它将打印5,与两个额外的变量[i]无关。



没有std :: cout,虽然,原,外,我从来没有使用。也许最后一个int i是一个错误。



另一个常见的问题是在C ++中拥有全局变量的能力

  int Whoops; // GLOBAL:NEVER EVER TOUCH THIS。 

int func(){
int whoops; //本地:总是触摸这个。
Whoops = 42; //> W< hoops!
}

您会收到一条警告:whoops检测您修改了错误的变量。


Recently I'm starting to program in C + + (I come from Java, and it costs me a little change haha). Under Windows everything right. The problem is that I switched to Linux and this is where I have problems with the compiler. It is usually when you declare a variable and is not used, the compiler displays a "warning" saying that the variable is not used, but I (under linuxmint 15) throws me as "error" and leaves no compile: C. I do not know if anyone has this happened, but I'm sick at the time of making large programs (more than one class).

a little example:

解决方案

The unusued variable warning is the result of invoking GCC with either

g++ -Wunused-variable ...

If this is the case, don't specify that argument. Or it's because of -Wall:

g++ -Wall ...

In which case, specify -Wno-unused-variable

It's being thrown as an error because you have the '-Werror' argument too.

There are a few reasons for this warning: It is possible to "shadow" variables between scopes in C++ and so a common cause of unused variables is when you have two variables of the same name.

int i = 5;
for (int i = 0; i < 10; ++i) { // << this is SECOND variable called i that hides the previous
}
if (day == "Monday") {
    int i; // << you can't see the second i here, this is a third that also hides the first.
    ...
}
// std::cout << "i = " << i << std::endl; // we can see original i again here

There are two variables called "i" here. If you uncommented the last line, it would print 5, unrelated to the two additional variables called 'i'.

Without the std::cout, though, the original, outer, i is never used. Perhaps that last "int i" is an error.

Another common problem relates to the ability to have global variables in C++

int Whoops; // GLOBAL: NEVER EVER TOUCH THIS.

int func() {
    int whoops; // LOCAL: ALWAYS TOUCH THIS.
    Whoops = 42; // >W<hoops!
}

You would receive a warning that "whoops" was an unused variable to aide in detecting you had modified the wrong variable.

这篇关于 - 使用变量编译器说ERROR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆