dimckissickjr
Posts: 5
Joined: Sun Feb 28, 2016 7:25 pm

Using namespace c++

Sat Mar 05, 2016 10:59 pm

Hello I'm trying to wrap my head around how namespace works. After reading about it I build a super simple program to test my understanding. Unfortunately I'm getting a multiple definition error and not sure why. Here is my program.

Code: Select all

include <iostream>
include "file_one.cpp"
using namespace std;

int main()
{
    int x = 2;
    int y = 1;
    cout << file_one::addNum(x,y);
}
* file_one.cpp

Code: Select all


namespace file_one{
    int addNum(int num1, int num2){           <<<** getting the error here**
        int total;
        total =  num1 + num2;
        return total;
    }
}


User avatar
AndyD
Posts: 2331
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: Using namespace c++

Sat Mar 05, 2016 11:23 pm

Why are you trying to insert one C++ source file into another using #include? This is not the usual practice. Normally, you #incldue a header file with class and function declarations and template definitions.

However, It works for me.

How are you compiling? If the file with main in it is called main.cpp. If you compile

Code: Select all

g++ -Wall main.cpp -o main
It works

If you are trying to compile with

Code: Select all

g++ -Wall main.cpp file_one.cpp -o main
You get link errors because addNum is compiled in main.cpp and file_one.cpp

Code: Select all

/tmp/ccoMoDHV.o: In function `file_one::addNum(int, int)':
file_one.cpp:(.text+0x0): multiple definition of `file_one::addNum(int, int)'
/tmp/ccXTf1Se.o:main.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status

dimckissickjr
Posts: 5
Joined: Sun Feb 28, 2016 7:25 pm

Re: Using namespace c++

Sun Mar 06, 2016 6:15 pm

Oh crap your right it does work. I wasn't using a terminal to compile the code, I was using CodeBlocks IDE on my windows machine.
Why are you trying to insert one C++ source file into another using #include?
I had a feeling that was kind of weird. I did a YouTube search trying to get more information on what a namespace was and that was how the video demonstrated it.

Thanks for your replay :P

swampdog
Posts: 221
Joined: Fri Dec 04, 2015 11:22 am

Re: Using namespace c++

Fri Mar 11, 2016 1:01 am

The namespace exists to stop things getting polluted. If I wrote a function called foo() and someone else writes another function called foo() they'd collide in what (iirc) is called the global namespace.

Code: Select all

#include <iostream>

namespace a {
void foo(){std::cout<<"a::foo\n";}
}//namespace a

namespace b {
void foo(){std::cout<<"b::foo\n";}
}//namespace b

void foo(){std::cout<<"::foo\n";}
int
main(void)
{
 #if defined(OK)
 a::foo();
 b::foo();
 foo();
 #endif

 #if defined(ERR)
 using namespace a;
 using namespace b;
 foo();
 #endif

 #if defined(FIX)
 using namespace a;
 using namespace b;
 a::foo();
 b::foo();
 ::foo();
 #endif

 return 0;
}
Tip: Always write the closing brace and put a comment on it like I have above immediately you write the initial "namespace blah {" otherwise you'll lose it(*).

Code: Select all

$ g++ -o a -DOK a.cc && ./a
$ g++ -o a -DERR a.cc && ./a
$ g++ -o a -DFIX a.cc && ./a
1st and 3rd compile. 2nd one won't. It's okay to have "using namespace blah" inside a source file but never put one into a header file (because it will affect everything that #include's it).

(*) Look what happens when I delete the closing brace for "namespace b"..

Code: Select all

$ g++ -o a -DOK a.cc && ./a
a.cc: In function ‘void b::foo()’:
a.cc:11: error: redefinition of ‘void b::foo()’
a.cc:8: error: ‘void b::foo()’ previously defined here
a.cc: At global scope:
a.cc:37: error: expected ‘}’ at end of input
..you'll never find that in a large compilation unit. It could be a missing closing brace for anything where you can use "{" ;-)

Return to “C/C++”