1

私は C++ プログラミングが初めてで、明らかに何かが欠けています。次のコードでは、コンパイラはこの行を認識していないようです:lengths[counter] = findDups(crtLine);エラーが発生しました: variable "lengths" set but not used. names[counter] = getNameOnly(crtLine)が完全に機能し、本質的に同じ形式であるのに、なぜこの行が認識されないのかわかりません。この問題についての洞察をいただければ幸いです。

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

using namespace std;


string getNameOnly (string line) {
    return line.substr(0, line.find(' '));
}

int findDups (string line) {
    string lnCpy = line;
    sort(lnCpy.begin(), lnCpy.end());   
    int i = 0; 
    int dups = 0;
    int j = 1;
    int len = lnCpy.length();
    while (j < len) {
        if (lnCpy.at(i) == lnCpy.at(j))
                dups++;
        i++;
        j++;
    }

    if (dups != 0)
        return 0;
    else
            return lnCpy.length();
}

int main() {

string names[1219];
int lengths[1219];
string crtLine;
int counter = 0;

ifstream myfile ("dist.male.first");
if (myfile.is_open()) {
    while (!myfile.eof()) {
        getline(myfile,crtLine);
        lengths[counter] = findDups(crtLine);           
        names[counter] = getNameOnly(crtLine);                  
        counter++;
    }
    myfile.close();
}   
else cout << "Unable to open file";


return (0);

}

4

1 に答える 1

6

これはエラーではなく警告であり、問​​題が何であるかを正確に示しています: という名前の変数に何かを入れますがlengths、その内容を決してチェックしないでください。

namesの代入演算子にstd::stringは副作用があり、コンパイラは、(後で値を取得するのではなく) 副作用に関心があると想定するため、に対して同様の警告は表示されません。

于 2012-06-11T23:43:30.340 に答える