0

I took a bit of code from this website(last code). BOO has been referred to a variable in GOO. But how do we refer a function? I have tried with no luck. It gives out random number.

#include<iostream>
 namespace Foo
{
namespace Goo
{
    int add(int a,int b){
        std::cout << a+b; 
    }
}
}

namespace Boo = Foo::Goo; 

int main()
{
std::cout << Boo::add(5,8); 
return 0;
}
Andrei Suvorkov
  • 5,559
  • 5
  • 22
  • 48
  • Please don't SHOUT! – Some programmer dude Jun 01 '18 at 07:05
  • 3
    As for your problem, what do your function *return*? Perhaps you should [get a couple of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to learn C++ from instead? – Some programmer dude Jun 01 '18 at 07:05
  • Seems like you need to learn the difference between C and C++. – Barmar Jun 01 '18 at 07:05
  • 4
    Your problem has nothing to do with namespaces. `std::cout << a+b;` should be `return a+b;` – Barmar Jun 01 '18 at 07:06
  • 1
    It's hard to make a good title when you have no clue what's going on. A good title would be something like "Why does my program print garbage when I forget to return a value from a function?" but if you've figured that out, no need for the question. – user4581301 Jun 01 '18 at 07:18

1 Answers1

0

Instead of typing

int add(int a,int b){
    std::cout << a+b; 
}

You(i) must type:-

int add(int a,int b){
    return a+b; 
}