如何制作包含对象向量的 class?
How do I make a class containing a vector of objects?
我在 Flashcard.h
和 Flashcard.cpp
中声明了 class Flashcard
。我想制作一个 class CardList
来存储 std::vector<Flashcard>
以及一些其他信息,并提供一些操作它的方法。
CardList.h
#include <vector>
#ifndef LC_FLASHCARD
#include "Flashcard.h"
#endif
class CardList
{
public:
/* Constructor */
CardList (int number_of_cards = 1);
/* Returns the j'th card. */
Flashcard getCard ( int j );
/* Replaces the j'th card with new_card. */
void setCard ( int j, Flashcard new_card );
/* ... */
private:
std::vector<Flashcard> card_list;
/* ... */
};
CardList.cpp
#include "CardList.h"
CardList::CardList ( int number_of_cards ) {
std::vector<Flashcard> card_list(number_of_cards);
}
CardList::~CardList ( void ) { }
Flashcard CardList::getCard ( int j ) {
return this->card_list[j];
}
void CardList::setCard ( int j, Flashcard new_card ) {
this->card_list[j] = new_card;
}
Flashcard CardList::drawCard ( void ) {
return this->getCard(0);
}
问题
每当我调用 CardList::getCard
或 CardList::setCard
时,我都会遇到段错误。例如:
#include "Flashcard.h"
#include "CardList.h"
/* ... */
int main( int argc, char** argv ) {
/* Creates flashcard card */
Flashcard card;
/* (do things to card) */
CardList card_list(7);
std::cout << "Declaration and initialisation of card_list successful" << std::endl;
card_list.setCard(0, card); // *** SEGFAULT ***
std::cout << "Assignment successful" << std::endl;
/* ... */
return 0;
}
我认为问题出在我的构造函数 CardList::CardList
上,但我该如何解决?
这段代码根本不是你想的那样:
CardList::CardList ( int number_of_cards ) {
std::vector<Flashcard> card_list(number_of_cards);
}
你应该做的是:
CardList::CardList ( int number_of_cards ) :
card_list(number_of_cards)
{
}
你的 seg 错误是因为你从未调整 card_list 的大小。您的构造函数版本中的代码创建了一个不同的临时 card_list,该临时 card_list 在构造函数退出后立即被销毁。同时,card_list 成员默认构造为大小为零。
您的 class 中有一个名为 card_list
的 属性,但您没有在构造函数中正确进行初始化。在您的代码中,您有:
CardList::CardList ( int number_of_cards ) {
std::vector<Flashcard> card_list(number_of_cards);
}
在这种情况下,您正在初始化一个名为 card_list
的 local variable
而不是 class' 属性 card_list
.
正确的做法如下:
CardList::CardList ( int number_of_cards ) {
this->card_list = std::vector<Flashcard>(number_of_cards);
}
我在 Flashcard.h
和 Flashcard.cpp
中声明了 class Flashcard
。我想制作一个 class CardList
来存储 std::vector<Flashcard>
以及一些其他信息,并提供一些操作它的方法。
CardList.h
#include <vector>
#ifndef LC_FLASHCARD
#include "Flashcard.h"
#endif
class CardList
{
public:
/* Constructor */
CardList (int number_of_cards = 1);
/* Returns the j'th card. */
Flashcard getCard ( int j );
/* Replaces the j'th card with new_card. */
void setCard ( int j, Flashcard new_card );
/* ... */
private:
std::vector<Flashcard> card_list;
/* ... */
};
CardList.cpp
#include "CardList.h"
CardList::CardList ( int number_of_cards ) {
std::vector<Flashcard> card_list(number_of_cards);
}
CardList::~CardList ( void ) { }
Flashcard CardList::getCard ( int j ) {
return this->card_list[j];
}
void CardList::setCard ( int j, Flashcard new_card ) {
this->card_list[j] = new_card;
}
Flashcard CardList::drawCard ( void ) {
return this->getCard(0);
}
问题
每当我调用 CardList::getCard
或 CardList::setCard
时,我都会遇到段错误。例如:
#include "Flashcard.h"
#include "CardList.h"
/* ... */
int main( int argc, char** argv ) {
/* Creates flashcard card */
Flashcard card;
/* (do things to card) */
CardList card_list(7);
std::cout << "Declaration and initialisation of card_list successful" << std::endl;
card_list.setCard(0, card); // *** SEGFAULT ***
std::cout << "Assignment successful" << std::endl;
/* ... */
return 0;
}
我认为问题出在我的构造函数 CardList::CardList
上,但我该如何解决?
这段代码根本不是你想的那样:
CardList::CardList ( int number_of_cards ) {
std::vector<Flashcard> card_list(number_of_cards);
}
你应该做的是:
CardList::CardList ( int number_of_cards ) :
card_list(number_of_cards)
{
}
你的 seg 错误是因为你从未调整 card_list 的大小。您的构造函数版本中的代码创建了一个不同的临时 card_list,该临时 card_list 在构造函数退出后立即被销毁。同时,card_list 成员默认构造为大小为零。
您的 class 中有一个名为 card_list
的 属性,但您没有在构造函数中正确进行初始化。在您的代码中,您有:
CardList::CardList ( int number_of_cards ) {
std::vector<Flashcard> card_list(number_of_cards);
}
在这种情况下,您正在初始化一个名为 card_list
的 local variable
而不是 class' 属性 card_list
.
正确的做法如下:
CardList::CardList ( int number_of_cards ) {
this->card_list = std::vector<Flashcard>(number_of_cards);
}