意外的 #ifndef,没有匹配的函数调用(链表 C++)
Unexpected #ifndef, No Matching Function Call (Linked List C++)
你好,我目前正在做一个链表项目,但在做的时候遇到了一些错误,我似乎无法解决。我遇到的第一个错误是 undetermined #ifndef。我感到困惑的是我没有在 header 中#include 源文件。
我遇到的第二个错误是在我的 main.cpp 中,我收到一条错误消息“没有匹配的函数调用 'List::AddNode(double, double, double等)".
我已经发布了所有三个文件,希望有人能帮助我找出我收到的这些错误,谢谢。
编辑:感谢那些帮助过我的人,这已经解决了我的问题,但现在我收到新的错误提示
“对 List::List() 的未定义引用”,
未定义引用 List::AddNode(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();
}
Header 文件
#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;
}
}
您遇到的 main 错误与头部防护有关。目前,您这样做:
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#endif LIST_H_INCLUDED
before 你开始你的代码,这实际上没有任何效果,因为 header guards 实际上没有 guard 任何东西。因此,当您包含 "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 guards 做同样的事情,尽管它是非标准的,但在很大程度上得到了大多数(如果不是全部)新编译器的支持。
您误解了 header guards 的使用方式:您需要一个条件编译语句来保护 header 的全部内容,而不仅仅是 #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 */
你好,我目前正在做一个链表项目,但在做的时候遇到了一些错误,我似乎无法解决。我遇到的第一个错误是 undetermined #ifndef。我感到困惑的是我没有在 header 中#include 源文件。
我遇到的第二个错误是在我的 main.cpp 中,我收到一条错误消息“没有匹配的函数调用 'List::AddNode(double, double, double等)".
我已经发布了所有三个文件,希望有人能帮助我找出我收到的这些错误,谢谢。
编辑:感谢那些帮助过我的人,这已经解决了我的问题,但现在我收到新的错误提示
“对 List::List() 的未定义引用”,
未定义引用 List::AddNode(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();
}
Header 文件
#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;
}
}
您遇到的 main 错误与头部防护有关。目前,您这样做:
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#endif LIST_H_INCLUDED
before 你开始你的代码,这实际上没有任何效果,因为 header guards 实际上没有 guard 任何东西。因此,当您包含 "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 guards 做同样的事情,尽管它是非标准的,但在很大程度上得到了大多数(如果不是全部)新编译器的支持。
您误解了 header guards 的使用方式:您需要一个条件编译语句来保护 header 的全部内容,而不仅仅是 #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 */