Code below is a simplified version of something I was working with today. It has 2 classes A and B. Class B tries to use a private constructor of class A, but it fails. If I make the constructor public the code compiles fine. Why is that?
#include <vector>
class A
{
friend class B;
private:
A(int * int_ptr) {
m_int = *int_ptr;
}
private:
int m_int;
};
class B
{
friend class A;
public:
static void create_vec_a() {
int v1(1);
int v2(2);
std::vector<int *> int_ptr_vec{
&v1,
&v2
};
std::vector<A> a_vec(int_ptr_vec.begin(),
int_ptr_vec.end());
}
};
int main(int argc, char *argv[])
{
B::create_vec_a();
return 0;
}
Error I get in Visual Studio is:
'A::A': cannot access private member declared in class 'A'
On Clang:
test.cpp:28:18: note: in instantiation of function template specialization 'std::vector<A, std::allocator<A> >::vector<__gnu_cxx::__normal_iterator<int **, std::vector<int *, std::allocator<int *> > >, void>' requested here
std::vector<A> a_vec(int_ptr_vec.begin(),
^
test.cpp:7:2: note: declared private here
A(int * int_ptr) {
^
Aucun commentaire:
Enregistrer un commentaire