0

I am still learning C++ hard and have now generated a circular dependency that, according to C2039: Class is not a member of Namespace may be the cause to my issue that I get a C2039 error. Can somebody help me how to cut this circle?

I have two template classes and the template class tXmlGeometry<Part> has a member function that shall declare an instance of template class tXmlStraightLine. Both are inside namespace nXml but the compiler complains that tXmlStraightLine is not member of nXml. I have to say that I bound the tXmlGeometry.h into the tXmlStraightLine header but I get an error when I try to bind the tXmlStraightLine.h into the tXmlGeometry header at the same time. I also just tried to remove the #include nXml/tXmlGeometry from the tXmlStraightLine header to no avail.

So here's a simplified code for the tXmlGeometry template class inside namespace nXml:

namespace nXml
{
    template<class Part>
    class tXmlGeometry : public tXmlNode<Part> 
    {
    public:
        tXmlGeometry(Part* part);
        ~tXmlGeometry();
        
        void AddStraightLine2D(const pugi::xml_node& node) {};
    };
}
;

and the implementation of the AddStraightLine2D method that causes the issue:

template<class Part>
inline void nXml::tXmlGeometry<Part>::AddStraightLine2D(const pugi::xml_node& this_node)
{
    nXml::tXmlStraightLine<Part> straightline_xml(this);
    //do more stuff
}

Here's the simplified code for the tXmlStraightLine template class:

namespace nXml
{
    template<class Part>
    class tXmlStraightLine : public tXmlSegment2D<Part>
    {
    public:
        tXmlStraightLine(tXmlGeometry<Part>* geo, const int npos);
        ~tXmlStraightLine();
    }
    ;
}
;

Can somebody advice me how to avoid that circular dependency?

EDIT: I corrected an error in member function naming.

5
  • move the definition of the method after the definition of tXmlStrightLine. Its not really "circular" Commented May 12, 2021 at 11:05
  • I am not sure if I understand what you mean with moving the definition of the method after the definition of tXmlStraightLine. Do you mean I should move the method behind the class tXmlStraightLine or do you mean I should declare straightline_xml bevore and outside the method? Commented May 12, 2021 at 11:17
  • i mean this: godbolt.org/z/PGhvG7nd1 (i just had to fix some typos and add dummy definitions of missing templates, and there is AddCircle2D vs AddStraightLine2D and you have a definition of AddStraightLine2D when I think you only want to declare it in the first snippet) Commented May 12, 2021 at 11:21
  • Please read about minimal reproducible example Commented May 12, 2021 at 11:22
  • Very much sorry, I made a mistake. In an attempt to reduce the code I deleted the wrong member function. I'll edit the original post Commented May 12, 2021 at 12:02

1 Answer 1

1

Since they're both template classes, I'd consider placing them in the same header. In order to avoid dependency issues, you can separate the declarations and definitions. Something like this:

namespace nXml
{
    // tXmlGeometry<Part> declaration
    template<class Part>
    class tXmlGeometry : public tXmlNode<Part> 
    {
    public:
        tXmlGeometry(Part* part);
        ~tXmlGeometry();
        
        inline void AddStraightLine2D(const pugi::xml_node& this_node);
    };

    // tXmlStraightLine  declaration
    template<class Part>
    class tXmlStraightLine : public tXmlSegment2D<Part>
    {
    public:
        tXmlStraightLine(tXmlGeometry<Part>* geo, const int npos);
        ~tXmlStraightLine();
    };

    // tXmlGeometry<Part> definitions
    template<class Part>
    inline void nXml::tXmlGeometry<Part>::AddStraightLine2D(const pugi::xml_node& this_node)
    {
        nXml::tXmlStraightLine<Part> straightline_xml(this);
        //do more stuff
    }
}
;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Ideally I could avoid placing tXmlStraightLine in the same file because I have other classes and furthers to be developed that I would need to copy there too (it's not in the example code for simplicity). Each of it coming with their own member functions and implementations. The file will be terrible to read.
@ManyQuestions So there are 2 ways to split it into more files: a. create a file that contains the definitions and include it at the bottom of the .h file - This way after preprocessing you'll be left with everything in a single file. b. explicit instantiation - add the line: template class tXmlStraightLine<Part>; in your header. This tells the compiler that tXmlStraightLine will be instantiated with Part. Then, you can create a .cpp file with the definitions and add it to your project.

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.