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.

Ovidiu Firescu
  • 385
  • 3
  • 11
  • 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. – Max Langhof Jul 15 '19 at 11:43
  • 5
    `using namespace Coordinates;` in a header. very bad idea – UKMonkey Jul 15 '19 at 11:43
  • Without `using namespace` it shouldn't compile at all... – user6556709 Jul 15 '19 at 11:45
  • @UKMonkey • it's less bad (but still something I'd discourage) when it is done within another namespace. – Eljay Jul 15 '19 at 11:46
  • 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 – UKMonkey Jul 15 '19 at 11:46
  • 1
    Because of the name collision, you'll have to disambiguate it: `std::vector <::Coordinates::Coordinates> coordTile;` – Eljay Jul 15 '19 at 11:47
  • @MaxLanghof I see, it works. I have a lot of classes with a lot of headers, would be ideal if I don't use ```using namespace namespaceName;``` in the headers and write the whole name like in this case: coordinates::Coordinates ? And use it only in the .cpp files? – Ovidiu Firescu Jul 15 '19 at 11:47
  • @OvidiuFirescu As UKMonkey said, don't use one namespace per class because that's pointless. Namespaces _group_ classes (and other names) – Max Langhof Jul 15 '19 at 11:48
  • 1
    @Eljay https://stackoverflow.com/questions/6175705/scope-of-using-declaration-within-a-namespace – UKMonkey Jul 15 '19 at 11:48
  • 1
    Even in your .cpp files, I urge you not to pull in an entire namespace. Pull in explicitly just the symbols you use, and nothing else. – Eljay Jul 15 '19 at 11:49
  • Someone adviced me to wrap my classes in it's own namespace to avoid nameclashes. But if I don't use ```using namespace name;``` the code gets harder to read because the length of the lines get a lot bigger ex: ```x::x::A```. – Ovidiu Firescu Jul 15 '19 at 11:52
  • 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 – UKMonkey Jul 15 '19 at 11:57
  • @UKMonkey "You should wrap all your functions and classes in its own namespace to prevent nameclashes." Does that mean that all functions and classes should be in a single namespace ? Or every class should have a namespace? I never used namespaces before, I'm not sure how this is done. I though that I need to use a namespace on every class like my example. – Ovidiu Firescu Jul 15 '19 at 12:04
  • 1
    @OvidiuFirescu "Does that mean that all functions and classes should be in a single namespace?" no - because that would again defeat the point of namespaces. However, you might want to put all YOUR functions and classes into a single namespace. It's done by just replacing `namespace Coordinates` and `namespace Tile` with `namespace MyThings` (and deleting the `using`) – UKMonkey Jul 15 '19 at 12:08
  • @UKMonkey done it put all classes and functions in a single namespace, and it works and makes so much sense, now I see what he meant by use a namespace. Glad I asked this question, thanks. One more thing, I know ```using namespace std;``` is a bad practice and I shouldn't use it. Should I use and include ```std::cout``` ```std::cin``` ```std::vector`` etc. or just use std:: everywhere? – Ovidiu Firescu Jul 15 '19 at 12:22
  • 1
    @OvidiuFirescu putting `using std::cout` at the top of your cpp file or using `std::cout` every time are the 2 safest options... as it ensures you don't bring in more than you need. You can also put `using` within functions if you want; so if you want to bring in the whole of std just for that 1 function because it uses a lot of std things.... (then maybe use a smaller function?) – UKMonkey Jul 15 '19 at 12:41

1 Answers1

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 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 – Ovidiu Firescu Jul 15 '19 at 12:33
  • 1
    @OvidiuFirescu Yes the information & solutions given in the comments should have been posted as answers instead. – Lightness Races in Orbit Jul 15 '19 at 12:38