Search code examples
c++classnamespaceseclipse-cdt

Is it possible to define classes with same name in same namespace but in different nested projects?


I have one big project in Eclipse, which contains multiple nested projects. Each one of those nested projects is in a separate folder and is compiled and linked separately. At the end, each one of them outputs a separate static library. Then there are few executables which are linked against those static libraries. In two of these projects I have two classes with the same name and same constructor arguments, but they have different implementations and also different extra members. Both classes are in the same namespace. After I compile each project I use the static libraries which they have created for a separate executables. Each executable is linked against the correct class, they don't mix implementations. Everything seems to work fine.

The problem is that when I compile one of the classes, the compiler gives me an error that I haven't initialized some member variables, which actually belong to the other class. During compilation those classes don't have an access to each other - they don't include the other one or they don't include headers, which then include the other one. They are in separate projects, in separate folders and are compiled separately. How is then possible that when compiling the first class the compiler somehow looks for the definition of the second class and gives me an error that I haven't initialized a member from it? Since I am using Eclipse and my projects' structure is like this: Main C++ Project (which is not compiled) holds the other C++ projects as nested projects (which are compiled separately) inside it - is it possible that this is something related to Eclipse?

Am I violating the One Definition Rule, since my classes are in separate projects and they are compiled in separate compilations, only having some common header files and no other connection between them? And if so, how is it possible that the compiler catches such an issue but it is still able to get the correct class definition? Because for sure it gets it right and everything works fine.

So my problem is the warning that the compiler gives me, because I have to clean all compilation warning before shipping the code. The code itself works fine.

Best Regards

=== Update After Comments ===

First of all, apologies for not being clear. Here is an image of the whole Project structure (I have changed the names obviously :) ):

The structure of my project

I have the MainProject project, which acts as a container for the other projects. I don't build it. In nested1 I have projects which make static libraries, which I then link with the Executable1 and Executable2. In nested2 I have other projects which again make static libraries, which I then link with the Executable3.

I have the MyFooBar class in both nested1 and nested2. Here is the code for both of them

// nested1/StaticLib5/MyFooBar.hpp
namespace foo
{
    namespace bar
    {
        class MyFooBar : public FooBar
        {
            public:
                   MyFooBar(int a, int b, double c);
                   // Getters and Setters
                   // Other user-defined functions
            private:
                   int memA;
                   int memB;
                   double memC;
                   int memDiff1;
        };
    }
}
// nested2/StaticLib9/MyFooBar.hpp
namespace foo
{
    namespace bar
    {
        class MyFooBar : public FooBar
        {
            public:
                   MyFooBar(int a, int b, double c);
                   // Getters and Setters
                   // Other user-defined functions
            private:
                   int memA;
                   int memB;
                   double memC;
                   int memDiff2;
                   char memDiff3;

        };
    }
}
// nested1/StaticLib5/MyFooBar.cpp
namespace foo
{
    namespace bar
    {
        MyFooBar::MyFooBar(int a, int b, double c) :
        memA{a}, memB{b}, memC{c}, memDiff1{0}
        {
        }         
    }
}
// nested2/StaticLib9/MyFooBar.cpp
namespace foo
{
    namespace bar
    {
        MyFooBar::MyFooBar(int a, int b, double c) :
        memA{a}, memB{b}, memC{c}, memDiff2{0}, memDiff3{0}
        {
        }         
    }
}

Executable1 and Executable2 don't use any of the libraries in nested2. Executable3 uses the only the static library StaticLib1 nested1.

Non of the Executables is linked to both StaticLib5 and StaticLib9. Also, I don't get an error during the linking of the executables, I get warning when compiling any of the MyFooBar classes. The warning says:

Member memDiff2 was not initialized in this constructor

The only thing that might be common for both classes is one header, where I have a class forward declaration and some typedefs, which I don't use in the class itself.

class MyFooBar;
typedef ListWidget* MyFooBarPtr;
typedef std::vector< MyFooBarPtr > MyFooBarVec;
typedef MyFooBarVec* MyFooBarVecPtr;

Solution

  • First of all, thanks for your help. The issue seems to be with the QNX Momentics IDE itself. If I build the projects from the command line no warnings are printed. If I build the first MyFooBar from nested1 then one project from nested2 then the second MyFooBar from nested2 I am given this warning. If I build the first MyFooBar then several other projects from nested1 then the second MyFooBar, there are no such warnings. In either cases, the build is fine and everything works as expected.

    Best Regards, Ahmed