繁体   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