简体   繁体   English

C++ 错误:在此 scope 中未声明“变量”

[英]C++ error: 'variable' was not declared in this scope

I have this simple C++ program and when I try to compile it I get two errors: 'klasa' was not declared in this scope 'oznaka' was not declared in this scope我有这个简单的 C++ 程序,当我尝试编译它时出现两个错误: 'klasa' was not declared in this scope 'oznaka' was not declared in this scope

Does anybody know how can I fix it?有人知道我该如何解决吗?

Note: I am still beginner in C++.注意:我还是 C++ 的初学者。 :] :]

#include <iostream>
#include <string>
using namespace std;

int main(){
    int T;
    cin >> T;

    if(T>=73){
        string klasa = "A";
        string oznaka = "XL";
    }
    else if(63<=T && T<73){
        string klasa = "A";
        string oznaka = "L";
    }
    else if(53<=T && T<63){
        string klasa = "A";
        string oznaka = "M";
    }
    else if(43<=T && T<53){
        string klasa = "A";
        string oznaka = "S";
    }
    else if(T<43){
        string klasa = "B";
        string oznaka = "XS";
    }

    cout << klasa << " " << oznaka;
}

The reason for the error is that you're declaring each of klasa and oznaka inside the "if statement", as such you can only reach these variables within their scope (ie inside each of their respective "if statements")错误的原因是您在“if 语句”中声明了每个klasaoznaka ,因此您只能在它们的 scope 中访问这些变量(即在它们各自的每个“if 语句”中)

#include <iostream>
#include <string>
using namespace std;

int main(){
    int T;
    cin >> T;
    string klasa = "";
    string oznaka = "";

    if(T>=73){
        klasa = "A";
        oznaka = "XL";
    }
    else if(63<=T && T<73){
        klasa = "A";
        oznaka = "L";
    }
    else if(53<=T && T<63){
        klasa = "A";
        oznaka = "M";
    }
    else if(43<=T && T<53){
        klasa = "A";
        oznaka = "S";
    }
    else if(T<43){
        klasa = "B";
        oznaka = "XS";
    }

    cout << klasa << " " << oznaka;
}

The code above places the declaration of the variables in a more global scope, so you can access them inside the "if statements" and outside of them too.上面的代码将变量的声明放在更全局的 scope 中,因此您可以在“if 语句”内部和外部访问它们。

Along with declaring the variables outside of the scope of the conditionals, I'd also reverse your order of the comparisons to make it read better and less chance of making a mistake:除了在条件句的 scope 之外声明变量外,我还将颠倒您的比较顺序,以使其更好地阅读并减少出错的机会:

string klasa, oznaka;

if (T < 43) {
    klasa = "B";
    oznaka = "XS";
}
else if (T < 53) {
    klasa = "A";
    oznaka = "S";
}
else if (T < 63) {
    klasa = "A";
    oznaka = "M";
}
else if (T < 73) {
    klasa = "A";
    oznaka = "L";
}
else {
    klasa = "A";
    oznaka = "XL";
}

Variables are declared in a scope.变量在 scope 中声明。 { opens a scope and } closes a scope. {打开 scope 并}关闭 scope。 Within a scope you can access variables from outside scopes but not from inside scopes.在 scope 中,您可以从外部范围访问变量,但不能从内部范围访问。

You can think of it like storeys of a building.你可以把它想象成建筑物的楼层。 On each floor, you can only look down, not up.每层楼只能往下看,不能往上看。

int main() { // "ground-level"
    int x = 0;

    { // "1st floor"
        x = 1;     // we can "look down" 

        { // "2nd floor" 
            int y = 0;
            x = 2;
        }
     
        y = 1;     // error ! we cannot "look up"

        { // another "2nd floor", but not connected to above
             x = 3;
             y = 2; // error ! (there are no "windows to look horizontally")
        }
    }

}

For your case, that means you need to declare the variables outside of the if if you want to access them outside of the if :对于您的情况,这意味着您需要在if之外声明变量,如果您想在if之外访问它们:

#include <iostream>
#include <string>

int main(){

    std::string klasa;
    std::string oznaka;
    if(...){
        klasa = "A";
        oznaka = "XL";
    }
    std::cout << klasa << " " << oznaka;
}

Read the code to get the explanation:阅读代码以获得解释:

#include <iostream>
#include <string>
using namespace std;

int main(){
    int T;      // T is defined here and exists until the closing bracket
    cin >> T;

    if(T>=73){
        string klasa = "A"; // klasa is defined here
        string oznaka = "XL"; // oznaka is defined from here on.
    } // here klasa, and oznaka don't exist anymore.
    else if(63<=T && T<73){
        string klasa = "A"; // a different klasa is defined here
        string oznaka = "L"; // a different oznaka is defined here.
        // the second set of klasa & oznaka cease to exist after this point.
    } // here klasa, and oznaka don't exist anymore.
    else if(53<=T && T<63){
        string klasa = "A"; // a third klasa is defined here.
        string oznaka = "M"; // a third oznaka is defined here.
        // the third group cease to exist after this point.
    } // here klasa, and oznaka don't exist anymore.
    else if(43<=T && T<53){
        string klasa = "A"; // Idem....
        string oznaka = "S";
    } // here klasa, and oznaka don't exist anymore.
    else if(T<43){
        string klasa = "B";
        string oznaka = "XS";
    } // here klasa, and oznaka don't exist anymore.

    // neither klasa nor oznaka are defined at this point, only T
    // (and cin with is defined in the global scope, outside of main())

    cout << klasa << " " << oznaka; // error.
} // here, T doesn't exist anymore.

this code will do the work:此代码将完成工作:

#include <iostream>
#include <string>
using namespace std;

int main(){
    int T;
    cin >> T;

    string klasa = "?";  // put some initializer here.
    string oznaka = "?"; // and here.

    if(T>=73){
        klasa = "A";
        oznaka = "XL";
    }
    else if(63<=T && T<73){
        klasa = "A";
        oznaka = "L";
    }
    else if(53<=T && T<63){
        klasa = "A";
        oznaka = "M";
    }
    else if(43<=T && T<53){
        klasa = "A";
        oznaka = "S";
    }
    else if(T<43){
        klasa = "B";
        oznaka = "XS";
    }

    // so if you get here with klasa or oznaka having "?" you can test
    // that you have not covered all the possible values in the tests
    // above.

    cout << klasa << " " << oznaka;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM