创建一个新对象并将该对象存储在一个新类中向量,属性消失
标题有点长,所以我很抱歉。但目前我的代码确实有点问题。它应该是非常通用的,并且代码中发生了很多事情,所以我不会将其全部发布,但我确实遇到了这个问题。现在
Sentence newSentence(currentSentence, this, sentenceCount);
this->sentencesNonP.push_back(newSentence);
,newSentence
有一个名为 words
的属性,其类型为 std::vector
, Word
也是项目中的另一个类。
当我调试和检查 newSentence
的属性时,它显示 words
的长度为 4,但是当我检查 sentencesNonP
时,是一个 std::vector
,words
向量长度为 0
。我正在检查 sentencesNonP
处的第一个点,因为它是第一个被推入的值,所以这并不是说我正在查看 sentencesNonP
向量的错误位置。
我的数据在转换过程中丢失的原因是什么?
编辑:我已经实现了 =
运算符重载和复制运算符。然而,sentencesNonP
中的words
仍然为空。
编辑2: Sentence.h(不包括包含的)
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;
};
忽略注释掉的 declspec
EDIT3:这是我的复制构造函数:
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]);
}
}
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]);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
wordNonP
为空,则根本不会复制任何单词
。写:或者更简单:
If
wordNonP
is empty you won't copy anywords
at all. Write either:Or even simpler: