Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 链表C++;,显示功能中未显示输出 #包括 使用名称空间std; 阶级人格主义者 { 私人: char-aName[7]; 内阿布里贝; 个人列表*链接; 公众: void addNodes(); void display(); }; #包括 #包括 使用名称空间std; #包括“mylink.h” void PersonList::addNodes() { 个人主义者*2; PersonList*startPtr=新PersonList(); PersonList*当前=新PersonList(); PersonList*temp=new PersonList();//创建了列表上的第一个节点 couttemp->aName; couttemp->aBribe; temp->link=NULL;//当我得到最后一个节点时,link将指向NULL(我在列表中的什么位置?) if(startPtr==NULL) { startPtr=温度; 电流=startPtr; } 其他的 { temp2=startPtr; while(temp2->link!=NULL) temp2=temp2->link; temp2->link=temp; } //} } void PersonList::display() { 个人名单*临时; PersonList*startPtr=此; 温度=启动温度; while(temp!=NULL) { cout_C++ - Fatal编程技术网

C++ 链表C++;,显示功能中未显示输出 #包括 使用名称空间std; 阶级人格主义者 { 私人: char-aName[7]; 内阿布里贝; 个人列表*链接; 公众: void addNodes(); void display(); }; #包括 #包括 使用名称空间std; #包括“mylink.h” void PersonList::addNodes() { 个人主义者*2; PersonList*startPtr=新PersonList(); PersonList*当前=新PersonList(); PersonList*temp=new PersonList();//创建了列表上的第一个节点 couttemp->aName; couttemp->aBribe; temp->link=NULL;//当我得到最后一个节点时,link将指向NULL(我在列表中的什么位置?) if(startPtr==NULL) { startPtr=温度; 电流=startPtr; } 其他的 { temp2=startPtr; while(temp2->link!=NULL) temp2=temp2->link; temp2->link=temp; } //} } void PersonList::display() { 个人名单*临时; PersonList*startPtr=此; 温度=启动温度; while(temp!=NULL) { cout

C++ 链表C++;,显示功能中未显示输出 #包括 使用名称空间std; 阶级人格主义者 { 私人: char-aName[7]; 内阿布里贝; 个人列表*链接; 公众: void addNodes(); void display(); }; #包括 #包括 使用名称空间std; #包括“mylink.h” void PersonList::addNodes() { 个人主义者*2; PersonList*startPtr=新PersonList(); PersonList*当前=新PersonList(); PersonList*temp=new PersonList();//创建了列表上的第一个节点 couttemp->aName; couttemp->aBribe; temp->link=NULL;//当我得到最后一个节点时,link将指向NULL(我在列表中的什么位置?) if(startPtr==NULL) { startPtr=温度; 电流=startPtr; } 其他的 { temp2=startPtr; while(temp2->link!=NULL) temp2=temp2->link; temp2->link=temp; } //} } void PersonList::display() { 个人名单*临时; PersonList*startPtr=此; 温度=启动温度; while(temp!=NULL) { cout,c++,C++,您正在创建一个新列表: #include <string> using namespace std; class PersonList { private: char aName[7]; int aBribe; PersonList *link; public: void addNodes(); void display(); }; #include <iostream> #include <string>

您正在创建一个新列表:

#include <string>

using namespace std;

class PersonList
{

private:
    char aName[7];
    int aBribe;
    PersonList *link;

public:
    void addNodes();
    void display();

};

#include <iostream>
#include <string>

using namespace std;

#include "mylink.h"

void PersonList::addNodes()
{

    PersonList *temp2;

    PersonList* startPtr = new PersonList();
    PersonList* current = new PersonList();


        PersonList *temp = new PersonList();//created the first node on the list
        cout<<"Enter the person's name: ";
        cin>>temp->aName;
        cout<<"Enter the person's contribution: ";
        cin>>temp->aBribe;
        temp->link=NULL;//when i get last node, link will point to null(where am i in list?)

        if(startPtr==NULL)
        {
            startPtr = temp;
            current = startPtr;
        }

        else
        {
            temp2 = startPtr;

            while(temp2->link!=NULL)
                temp2 = temp2->link;
            temp2->link=temp;
        }
    //}
}

void PersonList::display()
{
    PersonList *temp;
    PersonList *startPtr = this;

    temp=startPtr;

    while(temp != NULL)
    {
        cout<<temp->aName<<"\t\t"<<temp->aBribe<<endl;
        temp = temp->link;
    }

}

#include <iostream>
#include "mylink.h"

using namespace std;

int displayMenu (void);
void processChoice(int, PersonList&);

int main()
{
int num;

PersonList myList;


do 
{
num = displayMenu();
if (num != 3)
processChoice(num, myList);
} while (num != 3);

return 0;
}

int displayMenu(void)
{
int choice;
cout << "\nMenu\n";
cout << "==============================\n\n";
cout << "1. Add student to waiting list\n";
cout << "2. View waiting list\n";
cout << "3. Exit program\n\n";
cout << "Please enter choice: ";
cin >> choice;

cin.ignore();
return choice;
}

void processChoice(int choice, PersonList& p)
{

switch(choice)
{
case 1: p.addNodes();
break;
case 2: p.display();
break;
}

}
然后显示出来。所以,它自然是空的


addNodes方法中也存在类似的问题。将节点添加到新列表中,然后将其丢弃,这实际上是内存泄漏。

构建新的
PersonList
时会发生什么?它是否调用
addNodes()
另外,addNodes似乎只在本地实例上工作,而不是在传入的变量或自身上工作,这是有意的吗?感谢您的快速响应。我现在把它放在PersonList*startPtr;它说它startPtr未经初始化就被使用。问题是什么?如果没有看到您的类定义,很难确定,但可能您需要PersonList*startPtr=this;这是我的类定义#include using namespace std;class PersonList{private:char aName[7];int aBribe;PersonList*votePtr;public:void addNodes();void display();};您需要在您的问题中包含它。我添加了类定义和test.cpp文件
PersonList *startPtr = new PersonList();