Inconsistent stringstream errors - c++

I'm having bizarre behavior with stringstreams. It seems that if I create two stringstreams, one will write correctly and one will raise errors. (test is a char*)
ostringstream s;
ostringstream d;
s<<test<<endl;
d<<test<<endl;
This gives the message "error: invalid operands of types 'int' and 'const char*' to binary 'operator<<'" for the last line.
ostringstream s;
ostringstream d;
d<<test<<endl;
d<<test<<endl;
This gives the message "error: invalid operands of types 'int' and 'const char*' to binary 'operator<<'" for both lines writing to d.
The two streams should be identical, so I don't know why d doesn't work. Switching the order of the declarations of s and d doesn't change anything. Anyone have an ideas why this might happen?
Thanks!

I have the same error when the variable d has already been declared so it has another type.

Related

Cant use std::istream::get

I want use the 6th implementation of std::istream::get(...) method.
https://en.cppreference.com/w/cpp/io/basic_istream/get
It takes basic_streambuf& and char_type. I write:
char quote = '\'';
std::stringstream str;
input.get(str, quote); //input is std::istream object
But compiler as if it doesn't see this implementation, it try convert stringstream to char*:
error C2664: 'std::basic_istream<char,std::char_traits> &std::basic_istream<char,std::char_traits>::get(_Elem *,std::streamsize)': cannot convert argument 1 from 'std::stringstream' to '_Elem *'
Couldn't find answer for this. What's wrong? Thanks for help
To get the streambuf associated with a std::stringstream, you need to call rdbuf().
input.get(*str.rdbuf(), quote);

c++ how to convert char to int while using `tolower`

I'm try to compile a simple expression:
char_to_int(tolower(row[y]))
However I'm getting the following errors when trying to compile it:
error: implicit conversion loses integer precision: 'int' to 'char' [-Werror,-Wimplicit-int-conversion]
if (char_to_int(tolower(row[y])) > n
The signature of char_to_int is:
unsigned long char_to_int(char c)
and the type of row[y] is char.
Why am I getting this error and how can I fix it?
From your error information I assume you are using std::tolower from <cctype> (or equivalently, ::tolower from <ctype.h>), not std::tolower from <locale>.
Why you are getting the error is straightforward from your error information: your char_to_int expects a char, but tolower returns an int. This will cause loss of information.
Why does tolower return an int, not just a char? Because it can accept and return EOF, which may fall out of range of any char.
The fix can be straightforward: change your char_to_int to accept int, or do an intermediate step to discard the possible EOF.
std::tolower doesn't actually operate on chars: it operates on ints! Moreover, there is risk of undefined behaviour: if on your machine char is a signed type, then the "negative" characters will correspond to negative integers, which std::tolower is not equipped to deal with.
A way to fix this for your use is to manually cast the types before use:
char_to_int(static_cast<char>(
std::tolower(static_cast<unsigned char>(row[y]))));
... which unfortunately is a bit of a mess, but that's what you have to do.
Alternatively, you may use the locale version of std::tolower, which is templated and will correctly handle char types. You may use it like so:
// std::locale{} is an object representing the default locale
// you may specify a locale precisely if needed; see the above links
char_to_int(std::tolower(row[y], std::locale{}));
tolower returns an int. std::tolower is however a template, and will work correctly for char. In general, if there is a std:: version of any func you are calling, use it! :)

invalid conversion from 'const char*' to 'char*[-fpermissive](might be a multi dimensional array problem, not sure)

I am trying to write a code that deletes a word in a array of strings.
this is my whole code
int cancella(char v[],int nv,char ele,char vt[]){ int i,j;
for(i=0;i<nv;i++){
if (strcmp(v[i],ele))!=0;{
strcpy(vt[j],v[i]);j++
}
Return j;
}
}
int main()
{
char a[DIM][L]={"pane","pizza","pasta","cafe","panino","kebab","patatine"};
char aT[DIM][L];
int naT,na=7;
char elem={"kebab"};
nat= cacella(a,naT,elem,aT);
cout<<nat;
}
How to fix the Error
invalid conversion from 'const char*' to 'char*' [-fpermissive]
(might be a multi dimensional array problem, not sure)
at:
if (strcmp(v[i],ele))!=0;{
There are many problems with this code.
Incorrect indirect level at many places (for ex. char ele instead of char *ele).
Variables used before initialisation (for ex. naT; should probably be na instead).
Improper indentation and formatting.
Some suggestions
Write only a few line at a time and verify that it compile.
Read error message carefully. If you don't understand it, then see your compiler help.
Usually, it is easier to fix error at the top first.
As some errors are dependent on previous errors, compile again after fixing some errors.
Be careful with punctuation. Compiler care about misplaced parenthesis or semi-colon.
Read again your course notes or find a good C++ book.

left operand has type 'std::stringstream (__cdecl *)(std::string)'

what the diff in two codes blow:
char buf[2048];
stringstream in(string(buf));
int tmpInt;
while ((in >> tmpInt)) { // wrong, error C2296: '>>' : illegal, left operand has type 'std::stringstream (__cdecl *)(std::string)'
}
and
char buf[2048];
string tmpStr(buf);
stringstream in(tmpStr);
while ((in >> tmpInt)) { // right
}
I think they do same thing: both use string to construct a stringstream object. No matter temp object or a real object, we will call string copy constructor in stringstream(just copy buf content)
IDE: vs2010
So, what the different between this two ways ? or stringstream implement ways .
thanks.
Chris gave away the answer. The code is the equivalent of the following:
stringstream in(string buf);
In C++, people call this the most vexing parse.
The compiler sees it as a function declaration. in is a function which returns a stringstream and accepts a string as an argument. Note that your compiler is telling you this in the error message std::stringstream (__cdecl *)(std::string).
You will need an extra set of parentheses or C++11 uniform initializer syntax to tell the compiler its not a function you are declaring:
stringstream in((string(buf)));
stringstream in{string(buf)};

initialization with ' ... ' expected for aggregate object

I'm trying to write a code in C++ that allows you to enter some text and it will open a website with the variable s_input appended to it. However, I get this error:
'system' : cannot convert parameter 1 from 'std::string' to 'const
char *'
I get that error for the last line you see.
cin >> s_input;
transform(s_input.begin(), s_input.end(), s_input.begin(), tolower);
s_input = "start http://website.com/" + s_input + "/0/7/0";
system(s_input);
I am new to C++ and this is more of a learning program.. So please show as many examples as possible! Thanks!
If s_input is a std::string (I'm betting it is):
system(s_input.c_str());
The function system takes a const char* as parameter, as the error message clearly states.