为什么编译器说我的整数变量没有被使用?

问题描述 投票:0回答:1

我收到警告说我的int变量未使用,但是我正在使用它来存储我正在读取的文本文件中的整数值。

警告消息

menu.cpp:48:6: warning: unused variable ‘Release_year’ [-Wunused-variable]
   int Release_year;

menu.cpp

void Menu::LoadMovies(string filename)
{
    ifstream file;
    string line;
    string myString;

    string Movie_title, Lead_actor_actress, Description;
    int Release_year;

    file.open(filename.c_str());

    if (!file)
    {
        cout << "Unable to open file" << endl;
        exit(1);
    }

    while (getline(file, line))
    {
        stringstream ss(line);
        getline(ss, Movie_title, ',');
        getline(ss, Lead_actor_actress, ',');
        getline(ss, Description, ',');
        getline(ss, myString, ',');       //reading in the integer as a string for purpose of getline
        Release_year = stoi(myString);   //converting it to an integer

    }

    file.close();

}

我的语法读入和转换变量是错误的吗?我得到了警告的意思,然后又说了什么,但是可能导致此警告的原因是什么?

c++ stream compiler-warnings
1个回答
0
投票

您分配给Release_year,但从未阅读过。

因此未使用。

您确实在努力产生它的价值,但将其丢弃。

© www.soinside.com 2019 - 2024. All rights reserved.