C ++中链表代码中未解决的外部错误

unresolved external error in linked-list code in c++

我在实现代码中遇到了问题,你能帮我 运行 这个代码正确吗?

代码必须插入一个新客户并显示所有客户..

我收到以下错误:

1>Source.obj : error LNK2019: unresolved external symbol "class waitinglist __cdecl mylist(void)" (?mylist@@YA?AVwaitinglist@@XZ) referenced in function _main 1>c:\users\mmm\documents\visual studio 2012\Projects\ConsoleApplication141\Debug\ConsoleApplication141.exe : fatal error LNK1120: 1 unresolved externals

这是Header.h:

#include<iostream>
#include<string>
using namespace std;

class customer
{
public:
    string name;
    int gsize;
    int status;
    customer* next;

    customer();
    customer(string,int,int);
};

class waitinglist
{
public:
    int tables; //number of occupied tables
    int chairnum;
    int totalcustomers;
    customer*head,*tail;
    waitinglist();
    waitinglist(int);
    void newcustomer(string,int,int);
    void displayall();
};

这是 Source.cppmain:

#include"Header.h"

customer::customer()
{
    name="";
    gsize=status=0;
    next=NULL;
}

customer::customer(string name1,int gsize1,int status1)
{
    name=name1;
    gsize=gsize1;
    status=status1;
    next=NULL;
}

waitinglist::waitinglist()
{
    chairnum=totalcustomers=tables=0;
    head=tail=NULL;
}

waitinglist::waitinglist(int val)
{
    chairnum=val;
    totalcustomers=0;
    tables=0;
    head=tail=NULL;
}

void waitinglist::newcustomer(string name1,int gsize1 ,int status1)
{
    customer*tmp= new customer;
    if (head==NULL)            // linkedlist is empty
    {
        head = tail = tmp;
        totalcustomers++;
    }
    else
    {
        tmp->next = head;
        head = tmp;
        totalcustomers++;
    }
}

void waitinglist::displayall()
{
    customer *tmp;
    tmp = head;
    while(tmp != NULL)
    {
        cout<<tmp->name <<" "<<tmp->gsize<<"-->"; 
        tmp = tmp->next;
    }
    cout << endl;
}

int main()
{
    int choice;
    string namevar="";
    int gsizevar=0;
    int statusvar=0;
    waitinglist mylist();
    do
    {
        cout<<"Note: 1 in status means{the customer not here and 2 means the customer is here.}\n";
        cout<<"Select your option.\n\n";
        cout<<"(1) Add a new Customer.\n";
        cout<<"(2) List all Names.\n";
        cout<<"(3) Quit.\n\n";
        cout<<"Enter your choice: -->";

        cin>>choice;

        if (1 <= choice && choice <= 2)
        {   
            switch (choice)
            {
            case 1:
                mylist().newcustomer(namevar,gsizevar,statusvar);
                break;
            case 2:
                mylist().displayall();
                break;
            default:
                cout << "Invalid choice.  Enter again.\n\n";
                break;
            }
        }   
    }
    while (choice != 3);
    return 0;
}
waitinglist mylist();

被解释为函数声明(由于 most vexing parse),你没有提供它的定义,后来你试图调用它。您收到链接器错误,因为找不到定义。

main 中每次出现 mylist 后删除括号:

waitinglist mylist;                             // default-construct mylist
...
mylist.newcustomer(namevar,gsizevar,statusvar); // use mylist object
...
mylist.displayall();