473,326 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

C++ istringstream problem

Hello all,

I am trying to use an istringstream to do some input off of cin by lines.
The following snippet does not work:

char buf[90];
cin.getline(buf, 90);

istringstream line1(string(buf));

cin.getline(buf, 90);
istringstream line2(string(buf));

ChainOfExpressions line1chn;
ChainOfExpressions line2chn;

line1 >> line1chn;
line2 >> line2chn;

I have defined an extraction operator on istream and ChainOfExpressions.
Here is the prototype:

istream &operator >>(istream &strm, ChainOfExpressions &chain);

I get this error when I try to compile:

errantphysicist.cpp:310: error: no match for 'operator>>' in 'line1 >>
line1chn'
errantphysicist.cpp:159: error: candidates are: std::istream&
operator>>(std::istream&, Expression&)
errantphysicist.cpp:269: error: std::istream&
operator>>(std::istream&, ChainOfExpressions&)

I think the problem is that istringstream is not being converted to istream.
However, I thought that istringstream was a child of istream. What is my
problem here?

- JFA1
Jul 22 '05 #1
6 2962
James Aguilar wrote:
I am trying to use an istringstream to do some input off of cin by lines.
The following snippet does not work:

char buf[90];
cin.getline(buf, 90);

istringstream line1(string(buf));

cin.getline(buf, 90);
istringstream line2(string(buf));

ChainOfExpressions line1chn;
ChainOfExpressions line2chn;

line1 >> line1chn;
line2 >> line2chn;

I have defined an extraction operator on istream and ChainOfExpressions.
Here is the prototype:

istream &operator >>(istream &strm, ChainOfExpressions &chain);

I get this error when I try to compile:

errantphysicist.cpp:310: error: no match for 'operator>>' in 'line1 >>
line1chn'
errantphysicist.cpp:159: error: candidates are: std::istream&
operator>>(std::istream&, Expression&)
errantphysicist.cpp:269: error: std::istream&
operator>>(std::istream&, ChainOfExpressions&)

I think the problem is that istringstream is not being converted to istream.
However, I thought that istringstream was a child of istream. What is my
problem here?


I think FAQ 5.8 should be of some help.

V
Jul 22 '05 #2

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:je*******************@newsread1.mlpsca01.us.t o.verio.net...

I think FAQ 5.8 should be of some help.


OK then. Naturally, it is not compilable because of the error I mentioned
before, but this is what I have:

** CODE BEGINS **

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

string &stringToSpaces(string &toSpaces);
string intToString(int from);

class AddingException {};

class Expression
{
public:
Expression();
Expression(int coeff, int xDegree, int yDegree);
Expression(const Expression &other);

Expression operator *(const Expression &other) const;
void operator +=(const Expression &other);

bool sameDegrees(const Expression &other) const;

string topLine() const;
string bottomLine() const;

bool operator <(const Expression &other) const;

friend istream &operator >>(istream &strm, Expression &expr);
private:
int m_coeff, m_xDegree, m_yDegree;
};

Expression::Expression()
: m_coeff(0), m_xDegree(0), m_yDegree(0)
{}

Expression::Expression(int coeff, int xDegree, int yDegree)
: m_coeff(coeff), m_xDegree(xDegree), m_yDegree(yDegree)
{}

Expression::Expression(const Expression &other)
: m_coeff(other.m_coeff), m_xDegree(other.m_xDegree),
m_yDegree(other.m_yDegree)
{}

Expression Expression::operator *(const Expression &other) const
{
return Expression(m_coeff * other.m_coeff,
m_xDegree * other.m_xDegree,
m_yDegree * other.m_yDegree);
}

void Expression::operator +=(const Expression &other)
{
if (sameDegrees(other))
m_coeff += other.m_coeff;
else
throw AddingException();
}

bool Expression::operator <(const Expression &other) const
{
if (m_xDegree == other.m_xDegree)
return (m_yDegree < other.m_yDegree);
else return (m_xDegree > other.m_xDegree);
}

bool Expression::sameDegrees(const Expression &other) const
{
return (m_xDegree == other.m_xDegree) && (m_yDegree == other.m_yDegree);
}

string Expression::topLine() const
{
//0: Don't print anything if multiplied by zero
if (m_coeff == 0)
return "";

string retVal;

//1: Make a string out of the coefficient
if (m_coeff > 1 || m_coeff < -1 || (m_xDegree == 0 && m_yDegree == 0))
retVal += intToString((int) abs(m_coeff));

//2: Prepare the coefficient section
//2.1: Change all of those elements into spaces
stringToSpaces(retVal);
//2.2: Add two spaces that correspond to the operator and the space after
it
retVal += " ";

//3: Display the xDegree
//3.1: If the xDegree is greater than zero, add a space for the x
if (m_xDegree > 0)
retVal += ' ';
//3.2: If the xDegree is greater than 1, print it out
if (m_xDegree > 1)
retVal += intToString(m_xDegree);

//4: Display the yDegree
//4.1: If the yDegree is greater than zero, add a space for the y
if (m_yDegree > 0)
retVal += ' ';
//4.2: if the yDegree is greater than 1, print it out
if (m_yDegree > 1)
retVal += intToString(m_yDegree);

//5: Add the trailing space
retVal += ' ';

return retVal;
}

string Expression::bottomLine() const
{
if (m_coeff == 0)
return "";

string retVal;

//1: add the addition or subtraction sign
retVal += (m_coeff < 0) ? "- " : "+ ";

//2: Prepare the coefficient section
if (m_coeff > 1 || m_coeff < -1 || (m_xDegree == 0 && m_yDegree == 0))
retVal += intToString((int) abs(m_coeff));

//3: Display the xDegree
//3.1: If the xDegree is greater than zero, add the x
if (m_xDegree > 0)
retVal += 'x';
//3.2: If the xDegree is greater than 1, print out spaces for it
if (m_xDegree > 1) {
string rep(intToString(m_xDegree));
retVal += stringToSpaces(rep);
}

//4: Display the yDegree
//4.1: If the yDegree is greater than zero, add the y
if (m_yDegree > 0)
retVal += 'y';
//4.2: if the yDegree is greater than 1, print out spaces for it
if (m_yDegree > 1) {
string rep(intToString(m_yDegree));
retVal += stringToSpaces(rep);
}

//5: Add the trailing space
retVal += ' ';

return retVal;

}

istream &operator >>(istream &strm, Expression &expr)
{

expr.m_coeff = 1;
if (strm.peek() == '-') {
expr.m_coeff = -1;
strm.get();
} else if (strm.peek() == '+') {
strm.get();
}

if (strm.peek() != 'x' && strm.peek() != 'y') {
int coeff;
strm >> coeff;
expr.m_coeff *= coeff;
}

for (int i = 0; i < 2; ++i) {
if (strm.peek() == 'x') {
strm.get();
if (strm.peek() >= '0' && strm.peek() <= '9')
strm >> expr.m_xDegree;
else expr.m_xDegree = 1;
} else if (strm.peek() == 'y') {
strm.get();
if (strm.peek() >= '0' && strm.peek() <= '9')
strm >> expr.m_yDegree;
else expr.m_yDegree = 1;
}
}
}

class ChainOfExpressions
{
ChainOfExpressions operator *(const ChainOfExpressions &other) const;

void insert(const Expression &expr);

friend istream &operator >>(istream &strm, ChainOfExpressions &expr);
friend ostream &operator <<(ostream &strm, const ChainOfExpressions
&expr);
private:
vector< Expression > expressions;
};

ChainOfExpressions
ChainOfExpressions::operator *(const ChainOfExpressions &other) const
{
vector< Expression >::const_iterator thisIt(expressions.begin());
ChainOfExpressions retChain;

while (thisIt != expressions.end()) {
vector< Expression >::const_iterator otherIt(other.expressions.begin());
while (otherIt != other.expressions.end()) {
Expression newExpr((*thisIt) * (*otherIt));

bool same = false;
vector< Expression >::iterator it(retChain.expressions.begin());
while (it != retChain.expressions.end()) {
if (it->sameDegrees(newExpr)) {
(*it) += newExpr;
same = true;
}
}
if (!same)
retChain.expressions.push_back(newExpr);

++otherIt;
}
++thisIt;
}

sort(retChain.expressions.begin(), retChain.expressions.end());
}

void ChainOfExpressions::insert(const Expression &expr)
{
vector<Expression>::iterator it(expressions.begin());
bool same = false;

while (it != expressions.end())
if (it->sameDegrees(expr)) {
(*it) += expr;
same = true;
}

if (!same)
expressions.push_back(expr);
}

ostream &operator <<(ostream &strm, const ChainOfExpressions &chain)
{
string topLine;
string bottomLine;
vector<Expression>::const_iterator it(chain.expressions.begin());

while (it != chain.expressions.end()) {
topLine += it->topLine();
bottomLine += it->bottomLine();
}

if (*(bottomLine.begin()) == '+') {
topLine = topLine.substr(2, 2000);
bottomLine = bottomLine.substr(2, 2000);
} else {
topLine = topLine.substr(1, 2000);
bottomLine = bottomLine.substr(1, 2000);
bottomLine[0] = '-';
}
}

istream &operator >>(istream &strm, ChainOfExpressions &chain)
{
while (strm) {
Expression expr;
strm >> expr;
chain.insert(expr);
}
}

string &stringToSpaces(string &toSpaces)
{
string::iterator it(toSpaces.begin());

while (it != toSpaces.end()) {
*it = ' ';
++it;
}

return toSpaces;
}

string intToString(int from)
{
ostringstream strm;
strm << from << flush;
return string(strm.str());
}

int main()
{
while (cin.peek() != '#') {
char buf[90];
cin.getline(buf, 90);

istringstream line1(string(buf));

cin.getline(buf, 90);
istringstream line2(string(buf));

ChainOfExpressions line1chn;
ChainOfExpressions line2chn;

line1 >> line1chn;
line2 >> line2chn;

ChainOfExpressions product(line1chn * line2chn);

cout << product;
}
}

** CODE ENDS **
Jul 22 '05 #3
James Aguilar wrote:
[...]
istringstream line1(string(buf));
istringstream line2(string(buf));


Sorry I didn't catch them at first. These two are function declarations.

V
Jul 22 '05 #4
Oops. Skipped the minimal guideline. Here's a minimal demonstration of the
problem:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

class Test {
friend istream &operator >>(istream &strm, Test &tst);
};

istream &operator >>(istream &strm, Test &tst)
{
return strm;
}

int main()
{
char buf[80];
Test tst;

cin.getline(buf, 80);

istringstream strm(string(buf));

strm >> tst;
}
Jul 22 '05 #5

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:5F*******************@newsread1.mlpsca01.us.t o.verio.net...

Sorry I didn't catch them at first. These two are function declarations.


Changed it, and you're right. Can you help me fix my meta-understanding
now? What can I do (besides pulling out the use of the string constructor
into another statement) to make those not function declarations?

- JFA
Jul 22 '05 #6
James Aguilar wrote:
Oops. Skipped the minimal guideline. Here's a minimal demonstration of the
problem:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

class Test {
friend istream &operator >>(istream &strm, Test &tst);
};

istream &operator >>(istream &strm, Test &tst)
{
return strm;
}

int main()
{
char buf[80];
Test tst;

cin.getline(buf, 80);

istringstream strm(string(buf));
The simplest way to fix it is to do

istringstream strm((string(buf)));

(extra parentheses surrounding the argument list).

strm >> tst;
}


Victor
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Samuele Armondi | last post by:
Hi everyone, Since istringstream objects are not assignable, I'm using the following code to allocate some dynamically. My question is: Is this the correct way of doing it? Am I deleting all the...
8
by: Agent Mulder | last post by:
I try to remove the spaces from a string using an old trick that involves an istringstream object. I expect the while-condition while(istringstream>>string) to evaluate to false once the...
3
by: bml | last post by:
Could you help and answer my questions of istringstream? Thanks a lot! 1. Reuse an "istringstream" istringstream ist; ist.str("This is FIRST test string"); ist.str("This is SECOND test...
7
by: Luther Baker | last post by:
Hi, My question is regarding std::istringstream. I am serializing data to an ostringstream and the resulting buffer turns out just fine. But, when I try the reverse, when the istringstream...
4
by: dinks | last post by:
Hi I'm really new to c++ so please forgive me if this is really basic but im stuck... I am trying to make a data class that uses istringstram and overloaded << and >> operators to input and output...
6
by: JustSomeGuy | last post by:
I am passing an istringstream to a function. I want that function to get a copy of the istringstream and not a refrence to it. ie when the function returns I want the istringstream to be...
8
by: Randy Yates | last post by:
Why does this: string AWord(string& line) { return line; } bool MYOBJECT::MyFunction(string &line) { int day;
1
by: Adam Parkin | last post by:
Hello all, I'm trying to write a function which given a std::string parses the string by breaking the sentance up by whitespace (\t, ' ', \n) and returns the result as a vector of strings. Here's...
11
by: icanoop | last post by:
I would like to do this MyClass x; istringstream("XXX") >> x; // Works in VC++ but not GCC instead of MyClass x; istringstream iss("XXX"); iss >> x; // Works in both GCC and VC++
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.