Page 1 of 1

homework 16 inter error help

Posted: November 29th, 2020, 3:18 pm
by ayman awadallah

Code: Select all

	// Problem 2:
	// output nambies as string of digits without spaces
	// (can be done in single statement!)
	std::cout << "<< Number Words to Digits >>" << std::endl;
	{
		std::transform(
			std::istream_iterator<std::string>(std::istringstream(nambies)),
			std::istream_iterator<std::string>(),
			std::ostream_iterator<int>(std::cout),
			[&numbers](const std::string& word) {
				return std::find_if(numbers.begin(), numbers.end(),
					[&word](const Pube& p) {
						return p.str == word;
					}
				)->num;
			}
		);
	}
i got no suitable convertion right her "std::istream_iterator"<std::string>(std::istringstream(nambies))
any help???????? please

Re: homework 16 inter error help

Posted: December 6th, 2020, 4:37 pm
by ayman awadallah
answer was by mbozzi @cplusplus
std::istream_iterator<std::string>(std::istringstream(nb))
the expression
std::istream(nb)
is an rvalue, which is disallowed because it is error prone. For example, if it compiled, the declaration
std::istream_iterator<std::string> it(std::istringstream(nb));
would declare it a dangling iterator because the stream being traversed is destroyed at the semicolon.

To fix the problem, declare the stream on a prior line. The stream argument must be an lvalue expression. For example,

std::istringstream ss(nb)
std::transform(std::istream_iterator<std::string>{ss}, /* etc... */

Re: homework 16 inter error help

Posted: December 6th, 2020, 4:38 pm
by ayman awadallah
but how come it works with chili in the tutorial??????????????

Re: homework 16 inter error help

Posted: December 14th, 2020, 11:28 pm
by albinopapa
Here is my guess as to why it works for chili. It is possible that when he used it, the compiler at the time didn't find it to be an error. Either VS2017 or some version of VS2017 didn't consider it to be an invalid binding from r-value to l-value reference. Another guess would be that a switch in the settings referred to as /permissive might have been enabled allowing for non-standard behavior.