The title says most of it, how do I do this? I've Googled around a bit and nothing has told me that it can't be done, but nothing has explained how to do it either.
Take this snippet of code here:
#include <cstdio>
#include <memory>
int main(void)
{
struct a_struct
{
char first;
int second;
float third;
};
std::unique_ptr<a_struct> my_ptr(new a_struct);
my_ptr.first = "A";
my_ptr.second = 2;
my_ptr.third = 3.00;
printf("%c\n%i\n%f\n",my_ptr.first, my_ptr.second, my_ptr.third);
return(0);
}
As the people who can answer this already know, this doesn't work, it doesn't even compile.
My question is how do I make something like this work?
The compilation error (using g++-7) looks like
baduniqueptr6.cpp: In function ‘int main()’:
baduniqueptr6.cpp:15:12: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘first’
my_ptr.first = "A";
^~~~~
baduniqueptr6.cpp:16:12: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘second’
my_ptr.second = 2;
^~~~~~
baduniqueptr6.cpp:17:12: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘third’
my_ptr.third = 3.00;
^~~~~
baduniqueptr6.cpp:19:34: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘first’
printf("%c\n%i\n%f\n",my_ptr.first, my_ptr.second, my_ptr.third);
^~~~~
baduniqueptr6.cpp:19:48: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘second’
printf("%c\n%i\n%f\n",my_ptr.first, my_ptr.second, my_ptr.third);
^~~~~~
baduniqueptr6.cpp:19:63: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘third’
printf("%c\n%i\n%f\n",my_ptr.first, my_ptr.second, my_ptr.third);
^~~~~
Aucun commentaire:
Enregistrer un commentaire