0

I'm having some issue with returning vectors of objects of a class in functions because everytime my destructor erases the data twice and all the code just dies when the functions ends

here a simple code I wrote just to show my problem:

#include <iostream>
#include <vector>
using namespace std;

class identity{
public:
    string name;
    identity(string Name);
    ~identity();
};

vector<identity> function();

int main(){
    function();
    cout << "Hello world!";
}

identity::identity(string Name)
    : name{Name}{
        cout << "Object created!" << endl;
    }

identity::~identity(){
    cout << "Object " << name << " destroyed!" << endl;
}

vector<identity> function(){
    identity me("Isaias");
}

in this case the cout "Hello world" doesn't work and the program always ends with "Object" without displaying the name like this:

Object created!
Object Isaias destroyed!
Object

and then the program just stops. I kind of fixed the problem by seting the type of the function to "void" or anything else instead of "vector" but I'd like to know why this problem occurs. Also I'm new to programming in general and in this community so I'm sorry if I'm not doing this in the right way.

I'd like to thank you all for your attention before anything and sorry again if i am messing everything up here.

Isaias
  • 43
  • 1
  • 5
  • Can you change the function to accept a reference to a vector to fill? – vandench Dec 20 '21 at 03:50
  • 2
    Please read: [What is the Rule of Three?](https://stackoverflow.com/q/4172722/1553090) – paddy Dec 20 '21 at 03:52
  • 5
    Your function that is supposed to return a `vector` returns nothing. Therefore, undefined behavior. Therefore, pointless to figure out what's going on. Just fix your non-compliant program. – JohnFilleau Dec 20 '21 at 03:54
  • 4
    Turn on your compiler warnings. You should have received a warning about this. – JohnFilleau Dec 20 '21 at 03:54
  • 1
    Whole lot of copying and assigning (and moving if you let it) going on in a `vector`. If you want to see the whole picture, the Rule of Three is a must. But promising to return a `vector` and then not doing it, that's just a bad thing. always keep your promises. – user4581301 Dec 20 '21 at 04:00
  • The rule of 3 doesn't apply here. Yes, they have a destructor - but it's not managing resources. It's only present as a debug aid. The rules of 0/3/5 only apply when the class is doing some resource management. – JohnFilleau Dec 20 '21 at 04:04
  • Not necessary for a valid program, but I recommend following through on the Rule of Three because the next question is likely to be, "I see only one constructor, but multiple destructors. What gives?" – user4581301 Dec 20 '21 at 04:17

1 Answers1

3
vector<identity> function(){
    identity me("Isaias");
}

The behaviour of the program is undefined because you don't return anything from the function even though you've declared that the function returns a vector<identity>.

To fix the bug, return a value. Example:

using namespace std::string_literals;
return {"Isaias"s};

Another bug is that you've chosen to use using namespace std, but you've declared identity which is an identifier that is already in the std namespace. This makes the program ambiguous and ill-formed.

To fix this, choose another name for the class, such that the name doesn't conflict with any of the current or future names in the std namespace. This is very difficult because there are many names in std and you cannot predict the future. There is a simpler solution though! Simply don't use using namespace std.


Third bug is that you've used std::string without including the header that defines it. The consequence is that the program may not necessarily compile in any current or future language implementation.

Solution: Include the header <string>.

eerorika
  • 232,697
  • 12
  • 197
  • 326