0

I just started learning c++ and got an error while doing practicing. I was practicing using namespace and cin, cout.

I tried to print out the function of each namespace by each input. Here below is the code I wrote :

#include "iostream" using namespace std; namespace ns1 { int pp() { int x = 1; for (int i = 0; i < 9; i++) { cout << x << endl; x++; } return 0; } } namespace ns2 { void pp() { double x = 2; while (x < 6) { cout << x << endl; x += 1.7; } } } int main() { bool check = 0; cout << "Type 0 or 1 then you will have the following answer" << endl; cin >> check; if (check == 0) { cout << ns1::pp() << endl; } else { cout << ns2::pp() << endl; } return 0; } 

I don't know why void pp() cannot be printed out.

Could anyone let me know why it happens?

4
  • 4
    Because iostreams libraray doesn't know how to print value of type void. Silly library... Commented Sep 12, 2018 at 22:20
  • Please adjust the indentation in the code. As currently written it's very confusing. Commented Sep 12, 2018 at 22:23
  • 1
    A function that returns void returns nothing. As such there is nothing to print. Commented Sep 12, 2018 at 22:25
  • 3
    It's nothing to do with namespaces. What is cout << ns2::pp() << endl; supposed to print? Commented Sep 12, 2018 at 22:25

1 Answer 1

0

First, let's reduce the problem to a MCVE and remove as much noise as possible from the program.

#include <iostream> void pp() { } int main() { std::cout << pp(); } 

This produces the exact same error with no namespaces or other distractions.

The crux of the problem is pp returns void, that is nothing, from the function. Since nothing is returned, there is nothing to output.

Because writing an output function that outputs nothing is essentially wasted programmer time, no one has seen fit to specify that standard library implementors must implement an operator<< that handles void. As Justin points out in the comments below you can't use void as a function parameter because variables of type void cannot be instantiated. This makes it not just a waste of time but impossible.

You will find you will have a similar problem with custom classes. Unless someone has taken the time to write a << overload for the class, the class cannot be printed unless it can first be converted into a class or datatype that can be printed.

In the case of Standard Library containers there is no agreed upon default way a container should be output, so there are no built-in << overloads for the library containers. This is often a shock the first time you try to print a std::vector.

Sign up to request clarification or add additional context in comments.

5 Comments

You cannot write a function which takes in void as an argument. Writing an operator<< that "handles void" is impossible
Thanks ! I started learning c++ few days ago so I did not think about all you mentioned! By the way, what is the overload for the class ??
@proceooo If you have just started learning C++ it may be too soon to be worrying about operator overloading to handle custom classes. The first thing you need is a decent grip on overloading and overriding in general. Both should be covered in detail in a good C++ text. If you don't have a good C++ reference, get one. C++ is very hard to learn from Internet tutorials and trial and error. Some call it impossible.
Once you get what's going on with overloads, give What are the basic rules and idioms for operator overloading? a read-through. It has an excellent section on writing streaming operators for your classes and packs quite a bit of added wisdom and technique about other related problems.
@user4581301 Thank you so much! I will read through one by one !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.