简体   繁体   中英

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 . What im confused about is that I didnt #include a source file in the 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) ".

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()',

undefined reference to List::AddNode(double, double, double, double, double, double, double, double)',

undefined reference to `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

#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 . Move #endif to the end of the file, and remove or comment out LIST_H_INCLUDED at the end:

#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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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