【发布时间】: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;
}
【问题讨论】: