1

非常に単純なC++プロジェクトでg++コンパイラによってスローされる非常に奇妙なエラーが発生します。次のエラー:

In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ios:39,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:40,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iostream:40,
                 from findWord.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h: In copy constructor ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h:790: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd:47: error: within this context
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd: In copy constructor ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd:81: note: synthesized method ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’ first required here 
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/streambuf: In copy constructor ‘std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/streambuf:770: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]’ is private
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd:78: error: within this context
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd: In copy constructor ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd:81: note: synthesized method ‘std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)’ first required here 
findWord.cpp: In function ‘int main(int, char**)’:
findWord.cpp:19: note: synthesized method ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)’ first required here 

私のコード:

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>

using namespace std;

int main(int argc, char* argv[]){
//a char pointer is a c-string
//the array is just an array of char pointers
//argv[0] = pointer to the word to search for
//argv[1] = pointer to fileNames

//includes program name @ 0, so three args
if (argc == 3){

    int wordCounter = 0;

    ifstream myFile = ifstream(argv[2]);

    if (!myFile){
        cout << "File '" << argv[2] << "' could not be opened" << endl;
        return 1;
    }

    else {
        //counts the number of lines in file
        int counter = 0;

        //holds the new line in the file
        char line[100];

        //copies string into buffer that is length of word
        const char * word = argv[1];

        //holds whether found word
        bool found = false;

        cout << "Searching for '" << word << "' in the file '" << argv[2] << "'" << endl;

        while (!myFile.getline(line, 100).eof()) {
            //starts every new new at not having found the word
            found = false;
            //read in new line, so increases line counter
            counter ++;

            for (unsigned int i = 0; i <= myFile.gcount() - strlen(argv[1]); i++){
                if (line[i] == word[0]){
                    for (unsigned int j = 0; j < strlen(argv[1]); j++){
                        if (word[j] != line [i+j]){
                            break;
                        }
                    }//end second for loop
                    wordCounter++;
                    found = true;
                }//end if (char == word[0]
            }//end first for loop
            if (found){
                cout << counter << ": " << line;
            }//end if found word
        }//end while

        cout << "# occurrences of '" << word << "' = " << wordCounter << endl;
        myFile.close();
    }//end else
}//end if
return 0;
}//end main
4

3 に答える 3

2

の宣言によりmyFile、 のコピー コンストラクターifstreamが呼び出されますが、コピー可能ifstreamではありません (定義 #5 を参照)。ここでの構文は非常に微妙です。

ifstream myFile = ifstream(argv[2]);

この 1 行は、実際には 2 つのオブジェクト (左側に 1 つ、右側に 1 つ) を構成 ifstreamます。次に、右が左にコピーされます。

行を次のように変更します。

ifstream myFile(argv[2]);

ifstream自動ストレージ (スコープ/スタックベースなど) を使用して1 つのオブジェクトのみを構築します。

于 2012-12-15T01:50:40.067 に答える
1

コンパイラはmyFileを構築するあなたの方法を好まないようです。

ifstream myFile(argv[2]);代わりに試してくださいifstream myFile = ifstream(argv[2]);

于 2012-12-15T01:41:44.803 に答える
0

次回に役立つように、正確なエラーは次の行にあることに注意してください。

/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/streambuf:770: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]’ is private

関数がプライベートであることを明確に示しています。明確ではないのは、これがコピー演算子として使用されるコンストラクターであることです (Bret Kuhns が指摘したように)。

于 2012-12-15T01:54:26.727 に答える