Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::cout stream error #101

Closed
githubuser0xFFFF opened this issue Nov 26, 2020 · 2 comments
Closed

std::cout stream error #101

githubuser0xFFFF opened this issue Nov 26, 2020 · 2 comments

Comments

@githubuser0xFFFF
Copy link

githubuser0xFFFF commented Nov 26, 2020

The usage code from the documentation:

	using namespace units;
	measurement length1=45.0*m;
	measurement length2=20.0*m;

	measurement area=length1*length2;

	std::cout<<"the area is "<<area<< " or "<<area.convert_to(ft.pow(2))<<".\n";

Produces the following compile error (MinGW 7.3.0).

error: no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'units::measurement')
  std::cout<<"the area is "<<area<< " or "<<area.convert_to(ft.pow(2))<<".\n";
  ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~

Searching the code I have found the following line in units_decl.h

// std::ostream &operator<<(std::ostream &stream, units::unit u);

So does this mean, the stream operator is currently disabled and does not work or am I doing something wrong?

Thank you

@phlptp
Copy link
Collaborator

phlptp commented Nov 26, 2020

I should remove that comment.

Anyway at one point there was a stream operator but it ended up causing a few issues and added a lot to the header files for not much value.

The recommended way of doing this is either add a stream operator yourself

std::ostream &operator<<(std::ostream &stream, units::measurement m)
{
     stream<< units::to_string(m);
     return stream;
}

or if it is just a one time use.

std::cout<<"the area is "<<to_string(area)<< " or "<<to_string(area.convert_to(ft.pow(2)))<<".\n";

depending on the compiler you might need to add units:: in front of to_string.

@githubuser0xFFFF
Copy link
Author

Thank you and yes, you should definitely remove the comment.

And thank you for this great library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants