gpt4 book ai didi

c++ - 自定义流对象错误?

转载 作者:行者123 更新时间:2023-11-28 06:11:23 25 4
gpt4 key购买 nike

我正在尝试编写一个程序:

parses text, removes pre-specified (punctuation) characters and creates a alphabetically ordered dictionary containing the parsed words from the text, which are then displayed on the screen. From Bjarne Stroustup's C++ Programming: Principle and Practice

这是我写的:

Chapter11Exercise7Execute.cpp

#include "stdAfx.h"
#include "Chapter11Exercise7.h"

const string endInput("x");

int main(){
try{
// instantiate a PunctStream object and initialize it to: cin
PunctStream ps(cin);
// construct initial set of characters to be replaced with whitepace
ps.whitespace(",");
// set case sensitivity
ps.caseSensitive(false);

string word;
vector<string> dictionary;
do{
cout << "Type a word\n>>";
cin >> word;

createDictionary(ps, dictionary);
printDictionary(dictionary);
}while(word != endInput);
}catch(exception& e){
cerr << e.what() << endl;
getchar();
}
return 0;
}

Chapter11Exercise7.h

#include "stdAfx.h"
#ifndef _Chapter11Exercise7_H
#define _Chapter11Exercise7_H

class PunctStream{
public:
// constructors
PunctStream(istream& is): source(is), sensitive(true){ }
// member-functions
// creates a set of characters to be searched and replaced with whitespace
void whitespace(const string& s){white = s;};
void addWhitespace(char c){white += c;};
bool isWhitespace(char c);
// set case sensitivity
void caseSensitive(bool b){sensitive = b;};
bool isCaseSensitive(){return sensitive;};
// overwritten extraction operator
PunctStream& operator>>(string& s);
// operator that returns a bool value on success/failure of PunctStream object
operator bool();
private:
// stream containing text to be parsed
istream source;
// stringstream containing a parsed text
istringstream buffer;
// case sensitivity
bool sensitive;
// characters to be replaced with whitespace
string white;
};
// Non-member functions
void createDictionary(PunctStream& ps, vector<string>& d);
void printDictionary(vector<string>& d);
#include "Chapter11Exercise7V1.cpp"
#endif

Chapter11Exercise7.cpp

 #include "Chapter11Exercise7.h"

// Member functions definition
bool PunctStream::isWhitespace(char c){
for(size_t i; i < white.size(); i++) if (c == white[i]) return true;
return false;
}
// Overwritten operators definition
PunctStream& PunctStream::operator>>(string& s){
while(!(buffer >> s)){
// if buffer bad() or source not food => return PunctStream object
// this is a pointe to the object in context
if(buffer.bad() || !source.good()) return *this;
buffer.clear();

string line;
// get a line from source
getline(source, line);
// replace "white" characters in "line"
for(size_t i = 0; i < line.size(); ++i){
if(isWhitespace(line[i])) line[i] = ' ';
// if case sensitivity is set => convert to lowercase
else if (!sensitive) line[i] = tolower(line[i]);
}
// initialize the stringstream buffer to "line"
buffer.str(line);
}
return *this;
}

PunctStream::operator bool(){
// to return true: fail() and bas() should be unset, good()-set
return !(source.fail() || source.bad()) && source.good();
}
// Non-member functions
void createDictionary(PunctStream& ps, vector<string>& d){
string word;
while(ps >> word) d.push_back(word);

sort(d.begin(), d.end());
}

void printDictionary(vector<string>& d){
for(size_t i = 0; i < d.size(); ++i) cout << d[i] <<'\n';
}

我得到的错误如下:

1>------ Build started: Project: bjarneStroustroupC, Configuration: Debug Win32 ------
1> Chapter11Exercise7Execute.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(860): error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(176) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_istream<_Elem,_Traits>::basic_istream(const std::basic_istream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我已阅读错误消息,但它没有响铃。欢迎任何见解或建议!

最佳答案

如果您尝试复制 istream 对象,这是您收到的错误消息,因为 istream 不可复制。我认为这特别发生在这里:

class PunctStream{
public:
PunctStream(istream& is): source(is), sensitive(true){ }

private:
istream source;
};

在这里,您试图将 source 数据成员初始化为输入流参数 is 的拷贝,因此出现错误。

要解决此问题,请考虑将 source 更改为 istream& - 对 istream 的引用 - 而不是具体的 istream 对象。

希望这对您有所帮助!

关于c++ - 自定义流对象错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31210943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com