我在非常简单的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
答案 0 :(得分:2)
您对myFile
的声明导致调用ifstream
的复制构造函数,但ifstream
为not copyable (see definition #5)。这里的语法非常微妙:
ifstream myFile = ifstream(argv[2]);
这一行实际构建了两个 ifstream
个对象(一个在左侧,另一个在右侧)。然后将权限复制到左侧。
将行更改为:
ifstream myFile(argv[2]);
将仅使用自动存储构建一个ifstream
对象(例如,基于范围/堆栈)。
答案 1 :(得分:1)
看起来编译器不喜欢构建myFile的方式。
尝试使用ifstream myFile(argv[2]);
代替ifstream myFile = ifstream(argv[2]);
答案 2 :(得分: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指出的那样。)