Ovidiu Firescu
Ovidiu Firescu

Reputation: 415

Variable is ambiguous when using namespaces c++

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.

Upvotes: 1

Views: 970

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385385

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.

Upvotes: 2

Related Questions