创建一个新对象并将该对象存储在新类的向量中,属性消失
Creating a new object and storing that object in a new class' vector, attributes disappear
标题太长了,很抱歉。但我现在的代码确实有点问题。它应该很一般,代码中有很多内容,所以我不会全部发布,但我确实有一个问题。这是:
Sentence newSentence(currentSentence, this, sentenceCount);
this->sentencesNonP.push_back(newSentence);
现在,newSentence
有一个名为words
的属性,其类型为std::vector<Word *>
,Word
也是项目中的另一个类。
当我调试和检查newSentence
的属性时,它显示words
填充有长度4,然而当我检查作为std::vector<Sentence>
的sentencesNonP
时,words
矢量长度是0
。我正在检查sentencesNonP
的第一个点,因为它是第一个被推入的值,所以这并不是说我看错了sentencesNonP
向量的位置。
我的数据在转换过程中丢失的原因是什么?
EDIT:我已经实现了=
运算符重载和复制运算符。然而,words
在sentencesNonP
中仍然是空的。
第2版:句子.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
编辑3:这是我的复制构造函数:
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]);
}
}
for (int i = 0; i < newSent.wordNonP.size(); i++) {
this->wordNonP.push_back(newSent.wordNonP[i]);
this->words.push_back(newSent.words[i]);
}
如果wordNonP
为空,则根本不会复制任何words
。写入任一:
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]);
或者更简单:
this->wordNonP = newSent.wordNonP;
this->words = newSent.words;
相关文章:
- 将Ref对象作为类成员
- 如何在C++中获取该对象的类声明中对象的地址?
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 具有包含其他对象的类的对象创建顺序
- 将指针类分配给新类,C++
- 如何在对象 B 类 C++ 中使用对象 A?
- C++,创建新对象时类的对象更改
- 如何从传递到对象的对象内部访问对象的类属性?
- VS2015/VC++ 在新类模板中禁用默认 #include "stdafx.h"
- 如何允许用户在对象和类中输入?
- 在C++中遇到对象和类问题
- 如何通过对象的类属性删除对象,并返回其对象值?
- 如何在不同的开关大小写语句上使用对象的类成员函数?
- 如何将对象从类线程传递到帖子?
- C++ 提升 - 包含类层次结构对象的类的序列化
- 如何生成派生同类对象的类
- 从类中的对象调用类中的函数的最佳方法
- 如何创建新类以部署opencv和对象的方法
- 新的新类对象的函数;错误:非静态成员引用必须相对于特定对象
- 创建一个新对象并将该对象存储在新类的向量中,属性消失