首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++对象-私有int返回奇怪的值

C++对象-私有int返回奇怪的值
EN

Stack Overflow用户
提问于 2010-11-24 16:06:48
回答 4查看 2.5K关注 0票数 6

我一直在自学C++,并开始创建一个列表管理器来处理指针的概念。

我定义了一个名为List的类,它有三个属性:

代码语言:javascript
复制
int itemTotal;
Item* firstItem;
Item* lastItem;

构造函数将它们的值设置为:

代码语言:javascript
复制
itemTotal = 0;
firstItem = NULL;
lastItem = NULL;

我构建了一个函数来返回itemTotal的值:

代码语言:javascript
复制
int List::getItemTotal()
{
    return itemTotal;
}

在我的驱动程序中构建对象后,itemTotal立即开始表现得很滑稽,并返回非常大的数字(每次返回-858993460),即使列表上没有完成任何工作,程序中也没有发生任何事情。我向构造函数添加了一个cout,只是为了查看那里发生了什么,它回显了0值,但是一旦构造函数完成,值就会立即改变。

我一直试图用我的书来解决这个问题,但我似乎解决不了这个问题,所以我想我应该找一个更有经验的人。Main如下:

代码语言:javascript
复制
int main()
{
    List grocery;
    cout << "itemTotal is now: " << grocery.getItemTotal() << endl; // Returns wrong value...


    system("Pause");
    return 0;
}

输出如下所示:

代码语言:javascript
复制
grocery List is built!
itemTotal inside of the constructor is 0!
itemTotal is now: -858993460

有什么想法吗?=/

编辑:每个请求,整个类(抱歉,格式很难看,我不想全部重做):

代码语言:javascript
复制
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;

}

和构造函数:

代码语言:javascript
复制
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;
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-11-24 16:18:56

哈哈!您正在构造函数中初始化局部变量,而不是在成员中!!编写this->itemTotal = 0;或仅使用itemTotal = 0,甚至使用构造函数初始化列表,而不是int itemTotal = 0;,如下所示

代码语言:javascript
复制
List::list()
   :itemTotal(0),
    firstNode(NULL),
    lastNode(NULL)   
{
   cout << "List ctor Called"
}
票数 7
EN

Stack Overflow用户

发布于 2010-11-24 16:18:22

您在构造函数中声明了与成员同名的局部值。它们隐藏了成员的值,所以当您设置itemTotal = 0;时,实际上只是设置了一个局部变量的值。你的构造函数应该看起来像这样:

代码语言:javascript
复制
List::List()
    :itemTotal(0), firstNode(NULL), lastNode(NULL)
{
    cout << "item total should start at 0, it is " << itemTotal << " inside of the constructor." << endl;

}
票数 4
EN

Stack Overflow用户

发布于 2010-11-24 16:19:15

您正在初始化局部变量,而不是类成员。

替换:

代码语言:javascript
复制
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;
}

出自:

代码语言:javascript
复制
List::List()
{
  firstNode = NULL;
  lastNode = NULL;
  itemTotal = 0;
  cout << "item total should start at 0, it is " << nodeTotal << " inside of the constructor." << endl;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4264614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档