reference prob with namespace C++
-
Hello all, I'm looking on the way that i could create my own namepsace and use it under C++. i wrote a .h file
#ifndef FOO_H
#define FOO_Hnamespace FOO
{
int add (int a , int b);
}#endif//FOO_H
and a cxx file
#include "Foo.h"
namespace FOO
{
int add (int a , int b)
{
return (a+b);
}
}And in the main prog (test.cxx) i did as shown below
#include #include "Foo.h"
using namespace FOO;
int main(int argc, char **argv)
{std::cout << add(5, 6) << std::endl ; return 0;
}
when i compile the prog it mentions no problems but when i try to execute it it said undefined reference to `FOO::add(int, int)':~ i look on the net but still having that issue. how could i make it correct ? thank you. ;)
"The Ultimate Limit Is Only Your Imagination."
-
Hello all, I'm looking on the way that i could create my own namepsace and use it under C++. i wrote a .h file
#ifndef FOO_H
#define FOO_Hnamespace FOO
{
int add (int a , int b);
}#endif//FOO_H
and a cxx file
#include "Foo.h"
namespace FOO
{
int add (int a , int b)
{
return (a+b);
}
}And in the main prog (test.cxx) i did as shown below
#include #include "Foo.h"
using namespace FOO;
int main(int argc, char **argv)
{std::cout << add(5, 6) << std::endl ; return 0;
}
when i compile the prog it mentions no problems but when i try to execute it it said undefined reference to `FOO::add(int, int)':~ i look on the net but still having that issue. how could i make it correct ? thank you. ;)
"The Ultimate Limit Is Only Your Imagination."
Ok i think that i resolved it by changing the .h file like this
#ifndef FOO_H
#define FOO_Hnamespace Foo
{
int add (int a , int b)
{
return (a+b);
}
}#endif//FOO_H
So that mean's that : 1: when using namespace we can only use header files. or 2: i ve an error somewhere in my previous files. could someone help me with that ? i get :confused:
"The Ultimate Limit Is Only Your Imagination."
-
Ok i think that i resolved it by changing the .h file like this
#ifndef FOO_H
#define FOO_Hnamespace Foo
{
int add (int a , int b)
{
return (a+b);
}
}#endif//FOO_H
So that mean's that : 1: when using namespace we can only use header files. or 2: i ve an error somewhere in my previous files. could someone help me with that ? i get :confused:
"The Ultimate Limit Is Only Your Imagination."
What you did in your first post is correct. The problem you run into is a LINKER problem, not compiler (if you are unsure about the difference, study a while how compile and linking process works) Your "program" is actually made by two sources (main.cxx and foo.cxx): you have to compile them both, and link them together to produce a final executable. both the files must be inside your "project" configuration. By placing the function definition in the header you solved the problem because now everything you need in known to main.cxx (that's probably the only file you compiled) But if you compile bot files now you will get a "symbol defined more than once" linked error. So either: - use separate files, but compile them all - use only a single "cxx" and define everything the the header. But -in this second case- declare the functions you will define at header level as "inline" (
inline int add(int a, int b) { ... }
) to avoid multiple definitions in case of multiple compilations.2 bugs found. > recompile ... 65534 bugs found. :doh:
-
What you did in your first post is correct. The problem you run into is a LINKER problem, not compiler (if you are unsure about the difference, study a while how compile and linking process works) Your "program" is actually made by two sources (main.cxx and foo.cxx): you have to compile them both, and link them together to produce a final executable. both the files must be inside your "project" configuration. By placing the function definition in the header you solved the problem because now everything you need in known to main.cxx (that's probably the only file you compiled) But if you compile bot files now you will get a "symbol defined more than once" linked error. So either: - use separate files, but compile them all - use only a single "cxx" and define everything the the header. But -in this second case- declare the functions you will define at header level as "inline" (
inline int add(int a, int b) { ... }
) to avoid multiple definitions in case of multiple compilations.2 bugs found. > recompile ... 65534 bugs found. :doh:
You are all reasons, I compiled the first post files with VS and in the project settings i included the Foo.cxx file and all worked fine. I used Geany under ubuntu and in the console i wrote gcc test.cxx Foo.cxx -o Test.o and then some warnings were releaved. i think, as you said, i need to go backward and read more about linking files in C++ :omg: thank you for your help. :cool:
"The Ultimate Limit Is Only Your Imagination."
-
You are all reasons, I compiled the first post files with VS and in the project settings i included the Foo.cxx file and all worked fine. I used Geany under ubuntu and in the console i wrote gcc test.cxx Foo.cxx -o Test.o and then some warnings were releaved. i think, as you said, i need to go backward and read more about linking files in C++ :omg: thank you for your help. :cool:
"The Ultimate Limit Is Only Your Imagination."
Blood_HaZaRd wrote:
gcc test.cxx Foo.cxx -o Test.o
This is not correct, the output should be your executable file
Test
(note executables do not have.o
extension). You should then be able to run your program by typingTest
at the command prompt. Take a look at the man pages for gcc for more information on compile and link options.Unrequited desire is character building. OriginalGriff