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?

user4581301
  • 33,082
  • 7
  • 33
  • 54
proceooo
  • 13
  • 1

1 Answers1

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.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • You cannot write a function which takes in `void` as an argument. Writing an `operator<<` that "handles `void`" is impossible – Justin Sep 12 '18 at 22:52
  • 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 Sep 13 '18 at 15:42
  • @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](https://stackoverflow.com/a/11912039/4581301) in general. Both should be covered in detail in a good C++ text. If you don't have a [good C++ reference, get one](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). C++ is very hard to learn from Internet tutorials and trial and error. Some call it impossible. – user4581301 Sep 13 '18 at 17:47
  • Once you get what's going on with overloads, give [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/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 Sep 13 '18 at 18:02
  • @user4581301 Thank you so much! I will read through one by one ! – proceooo Sep 13 '18 at 19:14