简体   繁体   English

意外的#ifndef,没有匹配的函数调用(链接列表C ++)

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

Hi i am currently working on a linked list project but am receiving a few errors while doing so that I can't seem to solve. 嗨,我目前正在开发一个链表项目,但是在执行操作时收到一些错误,因此我似乎无法解决。 The first error i am getting is an undetermined #ifndef . 我遇到的第一个错误是#ifndef不确定 What im confused about is that I didnt #include a source file in the header. 令我感到困惑的是,我没有#header中包含源文件。

My second error I am getting is in my main.cpp, where I am getting an error saying " no matching function call to 'List::AddNode(double, double, double,etc) ". 我遇到的第二个错误是在main.cpp中,该错误是“ 没有匹配的函数调用'List :: AddNode(double,double,double,etc) ”。

I have posted all three of my files in hopes of someone helping me figure out these errors I am receiving, Thank You. 我已经发布了所有三个文件,希望有人能帮助我弄清楚我收到的这些错误,谢谢。

EDIT: Thank you for those who helped me, that has solved my problem but now I am receiving new errors saying 编辑:谢谢那些为我提供帮助的人,这已经解决了我的问题,但是现在我收到新的错误提示
"undefined reference to List::List()', “对List :: List()的未定义引用,

undefined reference to List::AddNode(double, double, double, double, double, double, double, double)', 未定义引用List :: AddNode(double,double,double,double,double,double,double,double,double,double)',

undefined reference to `List::PrintList()'". 未定义对“ List :: PrintList()”的引用”。

Main 主要

#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();
}

Header File 头文件

#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 File 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;

    }
}

You misunderstood the way header guards are used: you need a conditional compile statement to guard the entire content of the header, not just the #define . 您误解了使用标头保护的方式:您需要一个条件编译语句来保护标头的整个内容,而不仅仅是#define Move #endif to the end of the file, and remove or comment out LIST_H_INCLUDED at the end: #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 */

The main error you have is with your header guards. 您遇到的主要错误是与标题保护有关。 Currently, you do this: 当前,您可以执行以下操作:

#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#endif LIST_H_INCLUDED

before you begin your code, which really has no effect, since the header guards don't actually guard anything. 开始代码之前 ,这实际上没有任何作用,因为标题保护实际上并没有保护任何内容。 Thus when you include "List.h" twice, the compiler complains. 因此,当您两次包含“ List.h”时,编译器会抱怨。 The proper way to do it would be 正确的方法是

#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

//... List code ...

#endif //Endif goes at the end of file

Thus, if the file "List.h" is already included, you won't accidently include it again. 因此,如果文件“ List.h”已经被包含,您将不会意外地再次包含它。 Also, if you want to make it easier for yourself and are compiling on a compiler that supports the operation, you can use the following: 另外,如果您想使自己更轻松并且在支持该操作的编译器上进行编译,则可以使用以下命令:

#pragma once

//... List code ...

The #pragma once does the same thing as the include guards, although it is nonstandard but largely supported by most if not all new compilers. #pragma once与include防护功能相同,尽管它是非标准的,但大多数(如果不是全部)新编译器都支持它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM