Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. reference prob with namespace C++

reference prob with namespace C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Schehaider_Aymen
    wrote on last edited by
    #1

    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_H

    namespace 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."

    S 1 Reply Last reply
    0
    • S Schehaider_Aymen

      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_H

      namespace 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."

      S Offline
      S Offline
      Schehaider_Aymen
      wrote on last edited by
      #2

      Ok i think that i resolved it by changing the .h file like this

      #ifndef FOO_H
      #define FOO_H

      namespace 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."

      E 1 Reply Last reply
      0
      • S Schehaider_Aymen

        Ok i think that i resolved it by changing the .h file like this

        #ifndef FOO_H
        #define FOO_H

        namespace 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."

        E Offline
        E Offline
        Emilio Garavaglia
        wrote on last edited by
        #3

        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:

        S 1 Reply Last reply
        0
        • E Emilio Garavaglia

          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:

          S Offline
          S Offline
          Schehaider_Aymen
          wrote on last edited by
          #4

          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."

          L 1 Reply Last reply
          0
          • S Schehaider_Aymen

            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."

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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 typing Test 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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups