0

Bit of a long title so I am sorry with that. But I do have a bit of a problem with my code at the moment. It should be pretty general and there is quite a bit going on in the code so I won't post it all but I do have this one problem. Here it is:

Sentence newSentence(currentSentence, this, sentenceCount);
this->sentencesNonP.push_back(newSentence);

Now, newSentence has an attribute called words which is of type std::vector<Word *>, Word is also another class within the project.

When I am debugging and checking the attributes of newSentence it shows that words is populated with a length of 4, however when I check sentencesNonP, which is a std::vector<Sentence>, the words vector length is 0. I am checking the first point at sentencesNonP because it is the first the first value being pushed in so it's not that I'm looking at the wrong location of the sentencesNonP vector.

Any reason why my data is being lost in the conversion process?

EDIT: I have implemented both a = operator overload and a copy operator. However, words is still empty in sentencesNonP.

EDIT2: Sentence.h (excluding include's)

class Word;
class Document;

class Sentence {
public:
    //Take out Document * document
    Sentence();
    Sentence(std::string value, Document * document = NULL, int senNum = 0);
    Sentence(const Sentence& newSent);
    //Sentence(std::string value);
    ~Sentence(void);

    Sentence & operator=(const Sentence newSent);

    Document * getDocument(void);
    void setDocument(Document * document);
    //__declspec(property(get = getDocument, put = setDocument)) Document * document;

    std::string getSentence(void);
    void setSentence(std::string word);
    //__declspec(property(get = getSentence, put = setSentence)) std::string str;

    void setSentenceNumber(unsigned int i);

    Word * operator[] (unsigned int i);
    unsigned int wordCount(void);
    unsigned int charCount(void);
    unsigned int sentenceNumber(void);

    std::vector<Word *> getWordsVector(void);

private:
    std::string sentence;
    std::vector<Word *> words;
    std::vector<Word> wordNonP;
    Document * myd;
    unsigned int senNum;
};

Ignore the commented out declspec

EDIT3: Here is my copy constructor:

Sentence::Sentence(const Sentence& newSent) {
    this->sentence = newSent.sentence;
    this->myd = newSent.myd;
    this->senNum = newSent.senNum;
    for (int i = 0; i < newSent.wordNonP.size(); i++) {
        this->wordNonP.push_back(newSent.wordNonP[i]);
        this->words.push_back(newSent.words[i]);
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Brandon
  • 7,625
  • 5
  • 19
  • 16
  • What does the copy constructor of `Sentence` do? It must have lost the attributes on the way. – Vlad Oct 22 '11 at 11:32
  • That's what I did think as well, but there is no overloaded `=` operator. Should I implement one and see if it works then? Didn't really think it would need one since it's only being pushed onto a vector. – Brandon Oct 22 '11 at 11:35
  • Wait, is it `=` overload or another operator? – Brandon Oct 22 '11 at 11:35
  • It must be copy constructor: `Sentence(const Sentence&)`. Overloaded `=` is assignment operator, that's a different beast. – Vlad Oct 22 '11 at 11:36
  • Okay, I'll go ahead and implement that one now. If you check back soon, I'll let you know how it goes. – Brandon Oct 22 '11 at 11:37
  • ... and perhaps you need both of them. – Vlad Oct 22 '11 at 11:38
  • Okay, I'll add them both now, should do the exact same thing anyway. I am having a problem implementing the copy operator though. I have this `Sentence(const Sentence& newSent)` and within that I have `newSent.sentence = this->sentence`. `sentence` is only a `std::string` but it is saying `No viable overloaded '='`? – Brandon Oct 22 '11 at 11:42
  • You should do it other way round :) `newSent` is source, and `this` is destination! – Vlad Oct 22 '11 at 11:43
  • possible duplicate of [What is The Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – fredoverflow Oct 22 '11 at 11:45
  • Ahhh true, blimey. Thanks, Vlad. Okay, impleementing them now. What's that Fred? EDIT: Ahh, yes it is Fred, well, it sounds like it anyway. – Brandon Oct 22 '11 at 11:46
  • After both the copy and the `=` operator have been implemented, `sentencesNonP` is still saying that `words` is empty even though `newSentence` still has a populated `words` vector... – Brandon Oct 22 '11 at 11:52
  • Can we see the definition of `Sentence`? – fredoverflow Oct 22 '11 at 12:03
  • As in the constructor @FredOverflow – Brandon Oct 22 '11 at 12:07
  • show the copy constructor definition. – Puppy Oct 22 '11 at 12:22
  • Okay, adding now @deadmg – Brandon Oct 22 '11 at 12:25
  • 1
    Why do you have a `vector` as opposed to a `vector`? – fredoverflow Oct 22 '11 at 12:31
  • There is both there, @fred only because the Words are allocated memory during the Sentence constructor and if I store a pointer to them in the class, the pointers will point to nothing. Since this is for an assignment, the lecturer wants an attribute of type `vector` so I store the `Word` in the class and in the Sentence Constructor, populate the `Word *` vector with a reference to the `Word` vector's elements. – Brandon Oct 22 '11 at 12:34

1 Answers1

1
for (int i = 0; i < newSent.wordNonP.size(); i++) {
    this->wordNonP.push_back(newSent.wordNonP[i]);
    this->words.push_back(newSent.words[i]);
}

If wordNonP is empty you won't copy any words at all. Write either:

for (int i = 0; i < newSent.wordNonP.size(); i++)
    this->wordNonP.push_back(newSent.wordNonP[i]);
for (int i = 0; i < newSent.words.size(); i++)
    this->words.push_back(newSent.words[i]);

Or even simpler:

this->wordNonP = newSent.wordNonP;
this->words = newSent.words;
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • `words` is actually a vector of pointers to each element in `wordNonP`, so that's why I wrote my copy constructor the way I did. Changing to the way you have, it still won't work though. I do thank-you for your suggestion and I have changed the code a little bit. EDIT: As I mentioned though, it still won't copy across all the words stored in the vector? – Brandon Oct 22 '11 at 12:50