我一直在自学C++,并开始创建一个列表管理器来处理指针的概念。
我定义了一个名为List的类,它有三个属性:
int itemTotal;
Item* firstItem;
Item* lastItem;构造函数将它们的值设置为:
itemTotal = 0;
firstItem = NULL;
lastItem = NULL;我构建了一个函数来返回itemTotal的值:
int List::getItemTotal()
{
return itemTotal;
}在我的驱动程序中构建对象后,itemTotal立即开始表现得很滑稽,并返回非常大的数字(每次返回-858993460),即使列表上没有完成任何工作,程序中也没有发生任何事情。我向构造函数添加了一个cout,只是为了查看那里发生了什么,它回显了0值,但是一旦构造函数完成,值就会立即改变。
我一直试图用我的书来解决这个问题,但我似乎解决不了这个问题,所以我想我应该找一个更有经验的人。Main如下:
int main()
{
List grocery;
cout << "itemTotal is now: " << grocery.getItemTotal() << endl; // Returns wrong value...
system("Pause");
return 0;
}输出如下所示:
grocery List is built!
itemTotal inside of the constructor is 0!
itemTotal is now: -858993460有什么想法吗?=/
编辑:每个请求,整个类(抱歉,格式很难看,我不想全部重做):
class List
{
public:
// Constructor
// Purpose: Builds object.
// Returns: Nothing.
// Pre-Conditions: None.
// Post-Conditions: Initializes null.
List();
// push_back function
// Purpose: Adds Item to end of List.
// Returns: None.
// Pre-Conditions: Must pass a declared Item object.
// Post-Conditions: None.
void push_back(Node*);
// push_front function
// Purpose: Adds Item to beginning of List.
// Returns: None.
// Pre-Conditions: Must pass a declared Item object.
// Post-Conditions: None.
void push_front(Node*);
// pop_back function
// Purpose: Removes last Item from List. Item is NOT deleted.
// Returns: Pointer to removed Item.
// Pre-Conditions: None.
// Post-Conditions: None.
Node* pop_back();
// pop_front function
// Purpose: Removes first Item from List. Item is NOT deleted.
// Returns: Pointer to removed Item.
// Pre-Conditions: None.
// Post-Conditions: None.
Node* pop_front();
// getFirst function
// Purpose: Returns pointer to first Item in List.
// Returns: Pointer.
// Pre-Conditions: List must have a Item object.
// Post-Conditions: None.
Node* getFirst();
// getItemTotal function
// Purpose: Returns the itemTotal
// Returns: Int
// Pre-Conditions: None.
// Post-Conditions: None.
int getItemTotal();
private:
Item* firstitem;
Item* lastitem;
int itemTotal;}
和构造函数:
List::List()
{
Item* firstNode = NULL;
Item* lastNode = NULL;
int itemTotal = 0;
cout << "item total should start at 0, it is " << nodeTotal << " inside of the constructor." << endl;
}发布于 2010-11-24 16:18:56
哈哈!您正在构造函数中初始化局部变量,而不是在成员中!!编写this->itemTotal = 0;或仅使用itemTotal = 0,甚至使用构造函数初始化列表,而不是int itemTotal = 0;,如下所示
List::list()
:itemTotal(0),
firstNode(NULL),
lastNode(NULL)
{
cout << "List ctor Called"
}发布于 2010-11-24 16:18:22
您在构造函数中声明了与成员同名的局部值。它们隐藏了成员的值,所以当您设置itemTotal = 0;时,实际上只是设置了一个局部变量的值。你的构造函数应该看起来像这样:
List::List()
:itemTotal(0), firstNode(NULL), lastNode(NULL)
{
cout << "item total should start at 0, it is " << itemTotal << " inside of the constructor." << endl;
}发布于 2010-11-24 16:19:15
您正在初始化局部变量,而不是类成员。
替换:
List::List()
{
Item* firstNode = NULL;
Item* lastNode = NULL;
int itemTotal = 0;
cout << "item total should start at 0, it is " << nodeTotal << " inside of the constructor." << endl;
}出自:
List::List()
{
firstNode = NULL;
lastNode = NULL;
itemTotal = 0;
cout << "item total should start at 0, it is " << nodeTotal << " inside of the constructor." << endl;
}https://stackoverflow.com/questions/4264614
复制相似问题