Home > Enterprise >  How to use a const pair from one cpp file in another
How to use a const pair from one cpp file in another

Time:11-13

I have 2 structs: S and R. R has an instance of type S. In S there is defined a const pair that I want to use also in R but I get the following errors. S.hpp:11:12: error: redefinition of ‘const conf n1::n2::def1’ 11 | const conf def1 = std::make_pair(10, 2); | ^~~~

These are the structs and main function

#include <string>
#include <iostream>
#include <utility>
#include <memory>

namespace n1
{
    namespace n2
    {
        typedef std::pair<uint32_t, uint32_t> conf;
        const conf def1 = std::make_pair(10, 2);
        const conf def2 = std::make_pair(20, 4);

        struct S
        {
            int x;
            inline void print();
        };
        using Sptr = std::shared_ptr<S>;
    }
}

#include "S.hpp"
namespace n1
{
    namespace n2
    {
        void S::print()
        {
            std::cout<<"S-print\n";
        }

    }
}

include "S.hpp"
#include <memory>

namespace n1
{
    namespace c1
    {
        struct R
        {
            R(n1::n2::Sptr s);
            void r();
            n1::n2::Sptr s_;

        };

    }
}

#include "R.hpp"

namespace n1
{
    namespace c1
    {
        R::R(n1::n2::Sptr s):s_(s){}

        void R::r()
        {
            n1::n2::conf c;
            std::cout<<"---s.first: " << c.first;
        }

    }
}

#include <iostream>
#include "R.cpp"
#include "S.cpp"
#include <memory>




int main()
{
    auto s = std::make_shared<n1::n2::S>();
    auto r = std::make_shared<n1::c1::R>(s);
    r->r();
    s.print();

    return 0;
}

CodePudding user response:

I have made 3 changes in your program and it compiles:

Change 1

Added header guards. This is my habit(and advice) to add the header guards whenever i don't see in headers. So now your headers look like:

S.hpp

 #ifndef S_H
 #define S_H
  #include <string>
    #include <iostream>
    #include <utility>
    #include <memory>
    
    namespace n1
    {
    namespace n2 
    {
    typedef std::pair<uint32_t, uint32_t> conf;
    const conf def1 = std::make_pair(10, 2);
    const conf def2 = std::make_pair(20, 4);
    
    struct S 
    {
        int x;
        inline void print();
    };
    using Sptr = std::shared_ptr<S>;
    }
    }
    
    #endif

R.hpp

#ifndef R_H 
#define R_H
#include "S.hpp"
    #include <memory>
    
    namespace n1 
    {
    namespace c1 
    {
    struct R 
    {
        R(n1::n2::Sptr s);
        void r();
        n1::n2::Sptr s_;
        
    };
    
    } 
    }
    #endif

Change 2

In main.cpp i have changed #include "R.cpp" and #include "S.cpp" to #include "R.hpp" and #include "S.hpp" respectively. So your main.cpp now looks like:

main.cpp

#include <iostream>
#include "R.hpp"
#include "S.hpp"
#include <memory>

int main()
{
    auto s = std::make_shared<n1::n2::S>();
    auto r = std::make_shared<n1::c1::R>(s);
    r->r();
    //s.print();

    return 0;
}

Change 3

Note in the main.cpp i have commented our the statement s.print(); because s is of type shared_ptr which has no print method.

The program now compiles and gives output as can be seen here.

  • Related