homework 16 inter error help

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
ayman awadallah
Posts: 3
Joined: October 31st, 2020, 3:50 pm

homework 16 inter error help

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

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
Last edited by albinopapa on December 2nd, 2020, 9:23 am, edited 1 time in total.
Reason: Just added the [code][/code] tag around the code portion.

ayman awadallah
Posts: 3
Joined: October 31st, 2020, 3:50 pm

Re: homework 16 inter error help

Post by ayman awadallah » December 6th, 2020, 4:37 pm

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... */

ayman awadallah
Posts: 3
Joined: October 31st, 2020, 3:50 pm

Re: homework 16 inter error help

Post by ayman awadallah » December 6th, 2020, 4:38 pm

but how come it works with chili in the tutorial??????????????

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: homework 16 inter error help

Post by albinopapa » December 14th, 2020, 11:28 pm

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply