0

So my problem is simple, the data i am initalizing in my constructor is not saving at all.

basically I have pin pointed to this one point. I have a constructor in a class

strSet::strSet(std::string s){
std::vector<std::string> strVector;
strVector.push_back(s);
}

and in my main file i have

        else if(command == "s"){
        cout << "Declare (to be singleton). Give set element:";
        cin >> input >> single_element;
        vector_info temp;
        temp.name = input;
        strSet set(single_element);
        set.output();
        temp.vector = set;
        list.push_back(temp);
    }

when my constructor is called and i check the length of my vector the answer is appropriate, but then when i check the length of my vector in output, it resets to 0?

can anyone help me, much appreciated!!!!!!!

EDIT

this is my .h file

class strSet
{
private:
    std::vector<std::string> strVector;
    // This is initially empty (when constructed)
    bool isSorted () const;

public:
    strSet ();  // Create empty set
    strSet (std::string s); // Create singleton set

    void nullify (); // Make a set be empty
    bool isNull () const;
    int SIZE() const;

    void output() const;

    bool isMember (std::string s) const;

    strSet  operator +  (const strSet& rtSide);  // Union
    strSet  operator *  (const strSet& rtSide);  // Intersection
    strSet  operator -  (const strSet& rtSide);  // Set subtraction
    strSet& operator =  (const strSet& rtSide);  // Assignment

};  // End of strSet class
Nick
  • 1,417
  • 1
  • 14
  • 21
Vko
  • 3
  • 2
  • 1
    as i see, you are declaring strVector in a method - it means when this method ends, your strVector is destroyed - you need to declare it in class body. provide more sourcecode – fazo Mar 07 '11 at 06:05
  • @fazo: In C++ we have "functions" not methods, but I think you're absolutely correct about Vko's problem. Make that your answer. – Ben Voigt Mar 07 '11 at 06:08
  • possible duplicate of [What is the scope of variables declared in a class constructor?](http://stackoverflow.com/questions/690579/what-is-the-scope-of-variables-declared-in-a-class-constructor) – Ben Voigt Mar 07 '11 at 06:10
  • @Ben Voigt: i see this is an constructor method – fazo Mar 07 '11 at 06:11
  • okay i just uploaded my .h file with the class, because i realized that i wasnt showing enough code ><, thx for the help!!! – Vko Mar 07 '11 at 06:16

5 Answers5

2
strSet::strSet(std::string s){
   std::vector<std::string> strVector; //line 1
   strVector.push_back(s);             //line 2
}

This is your constructor, and here you're storing the value in the local vector which gets destroyed on returning from the constructor. How do you expect your data to be saved in strSet?

I believe you need to know the basic of C++ and class, by reading an introductory C++ book yourself. So please get a book and read it first; here is a list of really good books:

The Definitive C++ Book Guide and List

EDIT:

If you've declared strVector as class member, then you need to remove the line 1. Then it will work.

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • okay mabye i should have shown my .h file too, strVector is defined in my class as a private variable, srry T-T – Vko Mar 07 '11 at 06:13
  • @Vko: You may have that. But in the constructor you're declaring ANOTHER vector which is local to the constructor, and into that you're pushing the data. – Nawaz Mar 07 '11 at 06:21
1

looks, like your class body already has strVector, so you was declaring it again in constructor, which was wrong, because compiler referenced your new local strVector and not class strVector.

you need to change constructor to:

strSet::strSet(std::string s){
   strVector.push_back(s);
}
fazo
  • 1,807
  • 12
  • 15
  • THANK YOU SO MUCH!! LIFE SAVER – Vko Mar 07 '11 at 06:20
  • 2
    @Vko: It'll not save your life. Read a book, that will save your life, because you need to study the basics of C++ and classes yourself. Otherwise you've to come here very often to ask such a basic question which you can learn by yourself only if you read a book! – Nawaz Mar 07 '11 at 06:30
  • 1
    even better would be `strSet::strSet(const std::string& s) : strVector(1, s) {}` – Ben Voigt Mar 07 '11 at 07:31
1

Tip: GCC has a compiler warning for this. Use -Wshadow to catch such problems in the future.

0
std::vector<std::string> strVector;
strVector.push_back(s);

That creates a new local vector of strings in your constructor and adds a string to it. When the constructor exits, it will destroy your vector. If you want strVector to last longer than the constructor, make it a class member.

Chris
  • 6,642
  • 7
  • 42
  • 55
0

You're initializing a stack local variable in your strSet::strSet constructor. When you are returning from your constructor, all stack local variables are destroyed.

The way to fix this would be to declare the strVector variable as a class member variable.

An example:

class strSet {
public:
  strSet(std::string s) {
    this->strVector.push_back(s);
  }

private:
  std::vector<std::string> strVector;
};
Mahmoud Abdelkader
  • 23,011
  • 5
  • 41
  • 54