C++ error to call void function in namespace

proceooo

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

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ Error: "no matching function to call to " when calling a void function with argc, *argv[], and a multidimensional array

From Dev

Call function outside of current namespace in C++

From Dev

C - casting function call return values to void

From Dev

Error handling in a C, void returning function

From Dev

C++ Void Function with File Stream Error

From Dev

C call function type error

From Dev

How to call specific namespace's function in C#

From Dev

How to wrap or call a C function with void as return in Emscripten?

From Dev

Delphi how to call c++ dll function with typedef void * parameter?

From Dev

c++ : "no matching function for call to" error with function

From Dev

Nashorn: Call function inside of a namespace

From Dev

How to call Function<Void,Void>.apply()?

From Dev

c++ template function error: variable or field 'swapAdjacent' declared void

From Dev

How to remove "end of non-void function" error in c++?

From Dev

C++ Error: no matching function for call

From Dev

C++ Polymorphism error: no matching function for call to

From Dev

error: no matching function for call to c++ enumeration

From Dev

C++ "No matching function for call" error

From Dev

C++ ERROR No matching function for call

From Dev

occur no matching function for call to error in c++

From Dev

C runtime error during recursive function call

From Dev

unexpected compilation error in a function call (C++)

From Dev

Declare a void function in C

From Dev

Declare a void function in C

From Dev

Calling of a VOID function in C

From Dev

Keep getting error "is not a class or namespace" or "cannot call member function without object"

From Dev

How to call - (void) function in ordinary void function in cocoa

From Dev

Error in calling a static function in a namespace

From Dev

Getting a Namespace Not A function Error in Javascript

Related Related

  1. 1

    C++ Error: "no matching function to call to " when calling a void function with argc, *argv[], and a multidimensional array

  2. 2

    Call function outside of current namespace in C++

  3. 3

    C - casting function call return values to void

  4. 4

    Error handling in a C, void returning function

  5. 5

    C++ Void Function with File Stream Error

  6. 6

    C call function type error

  7. 7

    How to call specific namespace's function in C#

  8. 8

    How to wrap or call a C function with void as return in Emscripten?

  9. 9

    Delphi how to call c++ dll function with typedef void * parameter?

  10. 10

    c++ : "no matching function for call to" error with function

  11. 11

    Nashorn: Call function inside of a namespace

  12. 12

    How to call Function<Void,Void>.apply()?

  13. 13

    c++ template function error: variable or field 'swapAdjacent' declared void

  14. 14

    How to remove "end of non-void function" error in c++?

  15. 15

    C++ Error: no matching function for call

  16. 16

    C++ Polymorphism error: no matching function for call to

  17. 17

    error: no matching function for call to c++ enumeration

  18. 18

    C++ "No matching function for call" error

  19. 19

    C++ ERROR No matching function for call

  20. 20

    occur no matching function for call to error in c++

  21. 21

    C runtime error during recursive function call

  22. 22

    unexpected compilation error in a function call (C++)

  23. 23

    Declare a void function in C

  24. 24

    Declare a void function in C

  25. 25

    Calling of a VOID function in C

  26. 26

    Keep getting error "is not a class or namespace" or "cannot call member function without object"

  27. 27

    How to call - (void) function in ordinary void function in cocoa

  28. 28

    Error in calling a static function in a namespace

  29. 29

    Getting a Namespace Not A function Error in Javascript

HotTag

Archive