3

I have this and I can't seem to include the namespace correctly.

main.cpp

#include <iostream>

int main()
{
    my_space::Print(); // main.cpp:5:5: error: use of undeclared identifier 'my_space'

    return 0;
}

otherclass.cpp

#include <iostream>

namespace  my_space {
    int x, y;

    void Print()
    {
        std::cout << "Hello from namespace my_space." << std::endl;
    }
}

I have tried adding a otherclass.h with namespace my_space {}; in it and in main.cpp include #include "otherclass.h" but this didn't worked either.

3
  • Declare the namespace and all variables and functions in a header file. Implement in a cpp file. Include the header in main. Compile ALL your cpp files. Your namespace variables likely should be static as well. Commented May 27, 2020 at 20:09
  • Btw just as a helpful tip, you should add ``` using namespace std; ``` after your #define / #include It will make it easier than having to do a std:: before common cout / end statements. Commented May 27, 2020 at 21:16
  • @kushplank That is not a helpful tip. It's a bad practice that bites you the second a project gets mildly complicated. Commented May 28, 2020 at 2:05

3 Answers 3

6

You need to split up the declaration from the definition.

Your declaration looks like this:

namespace my_space {
  void Print();
}

Your definition looks like this:

#include <iostream>
#include "my_space.h"

void my_space::Print() {
    std::cout << "Hello from namespace my_space." << std::endl;
}

Then you add #include "my_space.h" to your main file so it knows about the declaration. The linker will take care of combining the final executable.

Things like x and y need more clarification as having random global variables laying around is asking for trouble.

Sign up to request clarification or add additional context in comments.

Comments

2

Leave your otherclass.cpp file as it is. Looks good.

Make a new otherclass.h file as you said you did, but make it look like this:

#pragma once

namespace my_space {
    void Print();
}

Then build it like this (if using GCC):

gcc -O2 -W -Wall -std=c++17 main.cpp otherclass.cpp -o testprogram

It is important to NOT get into the habit of writing all of your function code in the header file. Instead learn to hide everything that you can. Notice that in my example I didn't include your x and y variables. If those aren't needed by any other part of your program then no one else needs to know about them.

Code in header files adds compile time to every file that includes it. Worse, that code probably requires more header files to support it. Which have to be included and compiled for every cpp file that includes the first header.

This can lead to atrocities where 500 source files each rebuild half of Boost and include Windows.h for no good reason.

Comments

0

In the compilation unit with main.cpp the name my_space is not declared. So the compiler issues an error.

You should common declarations that will be used by several compilation units to place in a header and include this header in all compilation units where a declaration from the header is used.

As for the namespace then you could place either the declaration of the function Print in the namespace and the namespace itself place in a header. Or you could define the function inline within the namespace.

As for variables then either you should declare them in the namespace with the storage specifier extern or also to declare them inline.

For example:

// a header with the namespace

namespace  my_space {
    inline int x, y;

    inline void Print()
    {
        std::cout << "Hello from namespace my_space." << std::endl;
    }
}

or:

// a header with the namespace

namespace  my_space {
    extern int x, y;

    void Print();
}

In the last case the corresponding definitions of the variables and of the function should be placed in some cpp file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.