1

Coordinates.h

namespace Coordinates
{
    class Coordinates
    {
    public:
        Coordinates(int x = 0, int y = 0) : x(x), y(y) {}

    private:
        int x;
        int y;
    };
}

Tile.h

#include "Coordinates.h"
#include <vector>

namespace Tile
{
    using namespace Coordinates;

    class Tile
    {
    private:
        std::vector <Coordinates> coordTile;
    };
}

On the second header Tile.h, it says at std::vector <Coordinates> coordTile; that Tile::Coordinates is ambiguous. Without namespaces the program doesn't give any error.

16
  • 5
    The ambiguity is because you named the class the same thing as the namespace. This is easy to avoid if you use CamelCase for classes and everythinginlowercase for namespaces. Commented Jul 15, 2019 at 11:43
  • 5
    using namespace Coordinates; in a header. very bad idea Commented Jul 15, 2019 at 11:43
  • 2
    I would suggest your namespace might be simplified further if you just put them both into "MyGraphicsLib" ; if you have 1 namespace / class then you kinda defeat the point of namespaces Commented Jul 15, 2019 at 11:46
  • 1
    Because of the name collision, you'll have to disambiguate it: std::vector <::Coordinates::Coordinates> coordTile; Commented Jul 15, 2019 at 11:47
  • 3
    @OvidiuFirescu "Someone adviced me to wrap my classes in it's own namespace" I think that was some mis-communication; it should be in "A namespace" - maybe. There's nothing wrong with the global namespace for smaller projects at all Commented Jul 15, 2019 at 11:57

1 Answer 1

2

You have a namespace Coordinates, and a class Coordinates, and due to your use of using namespace both names are in scope. Although a vector element type cannot be a namespace, this is still an ambiguity at that particular phase of compilation.

Your class Coordinates does not need to be in a namespace Coordinates at all. Good advice is to put all your code into a namespace to "shield" it from other peoples' code — you may wish to further organise your code into multiple namespaces, but there is no benefit in putting each class in its own namespace, and certainly you shouldn't re-use their names like this.

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

2 Comments

I understood from reading the comments that what I was trying to do was wrong and I shouldn't wrap each class in its own namespace. Also should't use same name for namespaces and classes. Thanks
@OvidiuFirescu Yes the information & solutions given in the comments should have been posted as answers instead.

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.