Initialize pair constructor

Good evening to everyone,

I'm learning how to use pair function, and my compiler says to me that i'm making an error because my pair contructor is initialized by default to delete

 
pair <object, char> test;


seems that because i'm using an object from a class into pair i cannot inizialize it with make_pair.

What's your opinion?
Your question is not entirely clear, but it may be that object doesn't have a default constructor.
Adding a constructor with the signature object() that inits the members to default values may fix it.
I'll try to be clearer, the compiler gives me this error: https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-2/compiler-error-c2512?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev16.query%3FappId%3DDev16IDEF1&l=IT-IT&k=k(C2512)&rd=true&view=vs-2019

This error is about the undefined pair constructor.
The object has already a default constructor like object(), but i don't know how to create it for the pair.

To be clearer, in a struct i should made it like this:
1
2
3
4
5
 struct structure {
       structure() {
              // constructor definition
       }
}


how can i do this for the pair?

Maybe adding there my code line of the object too could be more usefull:
1
2
3
4
5
6
7
8
9
10
11
12
 typedef union structure {
          shared_ptr<int> i;
          shared_ptr<float> f;
          
          structure() {
               i.reset();
               f.reset();
          }
          ~structure() {}
} name;

typedef pair <name, char> test;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
 typedef union structure {
          shared_ptr<int> i;
          shared_ptr<float> f;
          
          structure() {
               i.reset();
               f.reset();
          }
          ~structure() {}
} name;

typedef pair <name, char> test;

Are you sure this is the right code ?
The snippet compiles in VS 2017 and on cpp.sh
Yes is the same, the only difference is that is on class.hpp because is part of a bigger program.

That's strange, maybe is why into union that's another variable that i don't there add to be more clear, here you are the complete union code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
typedef union structure {
          shared_ptr<int> i;
          shared_ptr<float> f;
          shared_ptr<Text> txt;
          
          structure() {
               i.reset();
               f.reset();
               txt.reset();
          }
          ~structure() {}
} name;

typedef pair <name, char> test;


where txt is
1
2
3
4
5
6
7
8
9
10
11
12
13
class Text {
         public:
                Text() {
                        ptr.reset(new char);
                }
                
                void Assign() {
                       char c = 'c';
                       ptr = &c;
                }
         private:
                shared_ptr<char> ptr;
};


could be a problem include smart_ptr into other smart_ptr?
Last edited on
ptr = &c; This is not right. You can't store a char* in ptr. They are different types.
Your code could be much simpler:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Text
{
public:
  /*Text()
  {
    ptr.reset(new char);
  }*/

  void Assign()
  {
    ptr = make_shared<char>('c');
  }
private:
  shared_ptr<char> ptr;
};

typedef union structure
{
  shared_ptr<int> i;
  shared_ptr<float> f;
  shared_ptr<Text> txt;

  /*structure() no need to that since the pointers are empty
  {
    i.reset();
    f.reset();
    txt.reset();
  }*/
  ~structure() {}
} name;
Okay, but if i delete that constructor "structure()" the compiler gives me an error saying that structure has not a default appropriate constructor.

Side note: i forgot to add that i'm using VC 2019 with default compiler in c++11 version.
OK, I am using VS 2017 with C++17.
Just create an empty constructor.
Last edited on
You need to rethink the whole 'union' thing TBH.

https://en.cppreference.com/w/cpp/language/union
As soon as you introduce any non POD types into a union, there's a whole load of ifs, buts, maybes.
Okay i get it, i'll try to solve it thank you!
Topic archived. No new replies allowed.