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复制构造函数,但不可复制(参见定义#5)。这里的语法非常微妙:ifstreamifstream

ifstream myFile = ifstream(argv[2]);

这条单行实际上构造了两个 ifstream对象(一个在左侧,一个在右侧)。然后将右侧复制到左侧。

将行更改为:

ifstream myFile(argv[2]);

将仅构造一个ifstream具有自动存储功能的对象(例如,基于范围/堆栈的)。

于 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 回答