簡體   English   中英

意外的#ifndef,沒有匹配的函數調用(鏈接列表C ++)

[英]Unexpected #ifndef, No Matching Function Call (Linked List C++)

嗨,我目前正在開發一個鏈表項目,但是在執行操作時收到一些錯誤,因此我似乎無法解決。 我遇到的第一個錯誤是#ifndef不確定 令我感到困惑的是,我沒有#header中包含源文件。

我遇到的第二個錯誤是在main.cpp中,該錯誤是“ 沒有匹配的函數調用'List :: AddNode(double,double,double,etc) ”。

我已經發布了所有三個文件,希望有人能幫助我弄清楚我收到的這些錯誤,謝謝。

編輯:謝謝那些為我提供幫助的人,這已經解決了我的問題,但是現在我收到新的錯誤提示
“對List :: List()的未定義引用,

未定義引用List :: AddNode(double,double,double,double,double,double,double,double,double,double)',

未定義對“ List :: PrintList()”的引用”。

主要

#include <iostream>
#include <cstdlib>
#include "List.h"

using namespace std;

int main()
{
    List Moe;

    Moe.AddNode(23.00, 7.00, 12.75, 7.65, 1.00,45.00, 0.18, 50.00);
    Moe.PrintList();
}

頭文件

#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#endif LIST_H_INCLUDED

using namespace std;
class List
{
private:

    struct node{
        double adults, children,costA,costC,dessert,room,tt,deposit;
        node* next, *link;

    };
    typedef struct node* nodePtr;

    nodePtr head;
    nodePtr curr;
    nodePtr temp;

public:
    List();
    void AddNode(double addAdults,double addChildren, double addCostA, double addCostC,double addDessert, double addRoom, double addTT, double addDeposit);
    void PrintList();
};

CPP文件

#include <cstdlib>
#include <iostream>
#include "List.h"

using namespace std;

List::List()
{
    head = NULL;
    curr = NULL;
    temp = NULL;
}
void List::AddNode(double addAdults,double addChildren, double addCostA, double addCostC,double addDessert, double addRoom, double addTT, double addDeposit)
{
    nodePtr n = new node;
    n->next = NULL;

    n->adults = addAdults;
    //cout >> "Number of Adults: " >> addAdults;
    n->children = addChildren;
    n->costA = addCostA;
    n->costC = addCostC;
    n->dessert = addDessert;
    n->room = addRoom;
    n->tt = addTT;
    n->deposit = addDeposit;

    if(head != NULL)
    {
        curr = head;
        while(curr -> next != NULL)
        {
            curr = curr->next;
        }
        curr->next = n;
    }
    else
    {
        head = n;
    }
}
void List::PrintList()
{
    curr = head;
    while(curr != NULL)
    {
        cout << "Total cost for adult meals:        $" << (curr -> adults) * (curr -> costA) << endl;
            cout << "Total cost for child meals:        $" << ((curr -> children) *(curr -> costA)) * 0.60 << endl;
            cout << "Total cost for dessert:            $" << ((curr -> adults) + (curr -> children)) * (curr -> dessert) << endl;
            cout << "Total food cost:                   $" <<(curr -> adults) * (curr -> costA) +
                                                           (curr -> children) *(curr -> costA) * 0.60 +
                                                           ((curr -> adults) + (curr -> children)) * (curr -> dessert)<< endl;
            cout << "Plus tips and tax:                 $" <<curr -> tt * ((curr -> adults) * (curr -> costA) +
                                                           (curr -> children) *(curr -> costA) * 0.60 +
                                                           ((curr -> adults) + (curr -> children)) * (curr -> dessert)) <<
                                                            " (Does NOT Include Room Fee)" << endl;
            cout << "Plus room fee:                     $" << (curr -> room) << endl;
            cout << "Less Deposit:                      $";
            cin >>curr -> deposit;
            cout << "" << endl;
            cout << "Balance Due:                      $" << /*FOOD COST*/((curr -> adults) * (curr -> costA) +
                                                           (curr -> children) *(curr -> costA) * 0.60 +
                                                           ((curr -> adults) + (curr -> children)) * (curr -> dessert)) +
                                                           /*TIPS & TAX*/ (curr -> tt * ((curr -> adults) * (curr -> costA) +
                                                           (curr -> children) *(curr -> costA) * 0.60 +
                                                           ((curr -> adults) + (curr -> children)) * (curr -> dessert))) +
                                                           /*ROOM FEE */ ((curr -> room)) - /*DEPOSIT*/ (curr -> deposit) << endl;
            cout << "" << endl;

            curr = curr->next;

    }
}

您誤解了使用標頭保護的方式:您需要一個條件編譯語句來保護標頭的整個內容,而不僅僅是#define #endif移到文件末尾,然后在末尾刪除LIST_H_INCLUDED或將其注釋掉:

#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

using namespace std;
class List
{
private:

    struct node{
        double adults, children,costA,costC,dessert,room,tt,deposit;
        node* next, *link;

    };
    typedef struct node* nodePtr;

    nodePtr head;
    nodePtr curr;
    nodePtr temp;

public:
    List();
    void AddNode(double addAdults,double addChildren, double addCostA, double addCostC,double addDessert, double addRoom, double addTT, double addDeposit);
    void PrintList();
};

#endif /* LIST_H_INCLUDED */

您遇到的主要錯誤是與標題保護有關。 當前,您可以執行以下操作:

#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#endif LIST_H_INCLUDED

開始代碼之前 ,這實際上沒有任何作用,因為標題保護實際上並沒有保護任何內容。 因此,當您兩次包含“ List.h”時,編譯器會抱怨。 正確的方法是

#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

//... List code ...

#endif //Endif goes at the end of file

因此,如果文件“ List.h”已經被包含,您將不會意外地再次包含它。 另外,如果您想使自己更輕松並且在支持該操作的編譯器上進行編譯,則可以使用以下命令:

#pragma once

//... List code ...

#pragma once與include防護功能相同,盡管它是非標准的,但大多數(如果不是全部)新編譯器都支持它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM