473,804 Members | 3,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(bu f));

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

ChainOfExpressi ons line1chn;
ChainOfExpressi ons line2chn;

line1 >> line1chn;
line2 >> line2chn;

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

istream &operator >>(istream &strm, ChainOfExpressi ons &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&, ChainOfExpressi ons&)

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 3007
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(bu f));

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

ChainOfExpressi ons line1chn;
ChainOfExpressi ons line2chn;

line1 >> line1chn;
line2 >> line2chn;

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

istream &operator >>(istream &strm, ChainOfExpressi ons &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&, ChainOfExpressi ons&)

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.********@com Acast.net> wrote in message
news:je******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...

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(cons t Expression &other);

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

bool sameDegrees(con st 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::Exp ression()
: m_coeff(0), m_xDegree(0), m_yDegree(0)
{}

Expression::Exp ression(int coeff, int xDegree, int yDegree)
: m_coeff(coeff), m_xDegree(xDegr ee), m_yDegree(yDegr ee)
{}

Expression::Exp ression(const Expression &other)
: m_coeff(other.m _coeff), m_xDegree(other .m_xDegree),
m_yDegree(other .m_yDegree)
{}

Expression Expression::ope rator *(const Expression &other) const
{
return Expression(m_co eff * other.m_coeff,
m_xDegree * other.m_xDegree ,
m_yDegree * other.m_yDegree );
}

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

bool Expression::ope rator <(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::sam eDegrees(const Expression &other) const
{
return (m_xDegree == other.m_xDegree ) && (m_yDegree == other.m_yDegree );
}

string Expression::top Line() 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((in t) 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_x Degree);

//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_y Degree);

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

return retVal;
}

string Expression::bot tomLine() 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((in t) 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 ChainOfExpressi ons
{
ChainOfExpressi ons operator *(const ChainOfExpressi ons &other) const;

void insert(const Expression &expr);

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

ChainOfExpressi ons
ChainOfExpressi ons::operator *(const ChainOfExpressi ons &other) const
{
vector< Expression >::const_iterat or thisIt(expressi ons.begin());
ChainOfExpressi ons retChain;

while (thisIt != expressions.end ()) {
vector< Expression >::const_iterat or otherIt(other.e xpressions.begi n());
while (otherIt != other.expressio ns.end()) {
Expression newExpr((*thisI t) * (*otherIt));

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

++otherIt;
}
++thisIt;
}

sort(retChain.e xpressions.begi n(), retChain.expres sions.end());
}

void ChainOfExpressi ons::insert(con st Expression &expr)
{
vector<Expressi on>::iterator it(expressions. begin());
bool same = false;

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

if (!same)
expressions.pus h_back(expr);
}

ostream &operator <<(ostream &strm, const ChainOfExpressi ons &chain)
{
string topLine;
string bottomLine;
vector<Expressi on>::const_iter ator it(chain.expres sions.begin());

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

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

istream &operator >>(istream &strm, ChainOfExpressi ons &chain)
{
while (strm) {
Expression expr;
strm >> expr;
chain.insert(ex pr);
}
}

string &stringToSpaces (string &toSpaces)
{
string::iterato r it(toSpaces.beg in());

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(bu f));

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

ChainOfExpressi ons line1chn;
ChainOfExpressi ons line2chn;

line1 >> line1chn;
line2 >> line2chn;

ChainOfExpressi ons product(line1ch n * line2chn);

cout << product;
}
}

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


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.********@com Acast.net> wrote in message
news:5F******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...

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(bu f)));

(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
3434
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 allocated memory correctly? Or am I missing something glaringly simple? Thanks in advance, S. Armondi std::istringstream** ArgStream;
8
5375
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 istringstream is exhausted, but that is not the case. What am I missing? #include<iostream> #include<sstream> #include<string>
3
4743
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 string"); cout << ist.str() << endl;
7
748
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 encounters the two byte shorts, it either thinks it has reached the null terminator? or eof and consequently stops reading values back in. It doesn't matter whether or not I use the std::ios::binary flag when opening the istringstream or the...
4
2333
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 data. The data comes in string lines like "OREBlegQ 14854 731.818" which need to be split into a string, int and double when stored in the class. Can anyone help? This is what i have so far: /* Begin Code */ #include <sstream>
6
2376
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 unmodified... However when I try to pass it fn(istringstream s) // doesn't compile but fn(istringstream & s) // does.
8
2097
by: Randy Yates | last post by:
Why does this: string AWord(string& line) { return line; } bool MYOBJECT::MyFunction(string &line) { int day;
1
7034
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 what I have so far: std::vector<std::string> tokenize (std::string foo) { std::istringstream s (foo); std::vector <std::string> v; std::string tok;
11
2919
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
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.