Closed. This question needs to be more focused。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                        
                        5年前关闭。
                                                                                            
                
        
#include <iostream>
#include <conio.h>
using namespace std;

class NumberBox
{
private:

    int number;

public:

    NumberBox *next_ptr;
    void setNumber(int number)
    {
        this->number = number;
    }


    int getNumber()
    {
        return number;
    }
};

int main()
{
    int number[5];
    int counter;
    cout << "Please give 5 numbers" << endl;
    for(counter = 4; counter > - 1; counter--)
    {
        cin >> number[counter];
    }


    NumberBox *numberBoxListHead_ptr = new NumberBox();
    NumberBox *numberBoxListBody1_ptr = new NumberBox();
    NumberBox *numberBoxListBody2_ptr = new NumberBox();
    NumberBox *numberBoxListBody3_ptr = new NumberBox();
    NumberBox *numberBoxListTail_ptr = new NumberBox();

    numberBoxListHead_ptr->setNumber(number[4]);
    numberBoxListHead_ptr->next_ptr = numberBoxListBody1_ptr;

    if(numberBoxListHead_ptr->getNumber() == '5')
    {
        cout << "Outcome should be..." << endl;
    }
    numberBoxListBody1_ptr->setNumber(number[3]);
    numberBoxListBody1_ptr->next_ptr = numberBoxListBody2_ptr;
    if(numberBoxListHead_ptr->next_ptr->getNumber() == '5')
    {
        cout << "Use this as" << endl;
    }

    numberBoxListBody2_ptr->setNumber(number[2]);
    numberBoxListBody2_ptr ->next_ptr = numberBoxListBody3_ptr;
    if(numberBoxListBody1_ptr->next_ptr->getNumber() == '5')
    {
        cout << "Work" << endl;
    }

    numberBoxListBody3_ptr->setNumber(number[1]);
    numberBoxListBody3_ptr->next_ptr = numberBoxListTail_ptr;
    if(numberBoxListBody2_ptr->next_ptr->getNumber() == '5')
    {
        cout << "Please work" << endl;
    }

    numberBoxListTail_ptr->setNumber(number[0]);
    if(numberBoxListBody3_ptr->next_ptr->getNumber() == '5')
    {
        cout << "Work???" << endl;
    };

    getch();

}


最后,如何使“ If语句在列表中起作用?因为给了我满足If语句的值”,但是它给出了空白。

如果我编辑节点以发送一个满足条件的数字,那行得通吗?

最佳答案

numberBoxListHead_ptr->getNumber(number[0]);这没有任何作用,您应该在此处使用setNumber。另外,您的getNumber函数不应带有任何参数,因为它不是必需的。并且,使getNumber为const成员函数。

关于c++ - 使用循环取数字,将其赋予列表(特殊数组),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9546463/

10-09 08:47