【问题标题】:How to use a const pair from one cpp file in another如何在另一个 cpp 文件中使用 const 对
【发布时间】:2021-11-12 11:34:09
【问题描述】:

我有 2 个结构:S 和 R。R 有一个类型为 S 的实例。在 S 中定义了一个我也想在 R 中使用的 const 对,但出现以下错误。 S.hpp:11:12:错误:重新定义‘const conf n1::n2::def1’ 11 | const conf def1 = std::make_pair(10, 2); | ^~~~

这些是结构体和主要功能

#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;
}

【问题讨论】:

    标签: c++ c++14


    【解决方案1】:

    我对您的程序进行了 3 处更改,并且可以编译:

    更改 1

    添加了标题保护。这是我在标题中看不到时添加标题保护的习惯(和建议)。所以现在你的标题看起来像:

    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
    

    更改 2

    在 main.cpp 中,我将 #include "R.cpp"#include "S.cpp" 分别更改为 #include "R.hpp"#include "S.hpp"。所以你的 main.cpp 现在看起来像:

    ma​​in.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;
    }
    
    

    变化3

    请注意在 main.cpp 中我已经评论了我们的声明 s.print(); 因为 sshared_ptr 类型,它没有打印方法。

    程序现在编译并提供输出,如 here 所示。

    【讨论】:

    • 您可能会强调一点永远不会包含.cpp文件...除了仍然很好的答案。
    • 非常感谢!我有一个新问题,我会提出一个新问题,如果你有时间请帮忙,我需要创建一个新帐户,因为这不允许使用这个
    • @Anoop Rana 你也可以检查一下stackoverflow.com/questions/69943142/…
    • @JohnLauren 我已经在那里添加了答案。看看吧。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多