2012-12-15 41 views
1

我在非常简单的C++项目中发现了g ++编译器抛出的非常奇怪的错误。以下错误:#include中的标准库编译器错误 - 所有库导致终端中出现错误

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 

回答

2

你的myFile声明引起的ifstream拷贝构造函数被调用,但ifstreamnot copyable (see definition #5)。这里的语法是很微妙:

ifstream myFile = ifstream(argv[2]); 

这一行实际上构建 ifstream对象(一个在左边,一个在右边)。然后将权利复制到左侧。

更改行:

ifstream myFile(argv[2]); 

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

+0

没有办法设置一个流等于另一个。 –

+0

对,这就是为什么我说'ifstream'不可复制。它的拷贝构造函数是'delete'd。 –

1

看来,编译器不喜欢你的构建MYFILE的方式。

尝试ifstream myFile(argv[2]);而不是ifstream myFile = ifstream(argv[2]);

+0

OMG。谢谢。它能够开始运行。虽然显然我有段错误,但我必须弄清楚。 – katiea

+0

它在我的第一个cout陈述后立即出现,但我必须弄清楚在哪里。 :) – katiea

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指出的那样)。

+0

谢谢 - 这是有用的信息知道。 – katiea