C++ C++;单独的实现和头文件

C++ C++;单独的实现和头文件,c++,C++,我被指派把一个程序分成不同的文件。分配为:每个文件应包含以下内容: 客户。h:应包含客户结构的定义和打印客户的声明。 customers.cpp:应包含打印客户的实现(或定义)。 练习1.5.cpp:应包括customers.h和主程序 这是我的密码: 客户。h #pragma once; void print_customers(customer &head); struct customer { string name; customer *next; }; 客户。cpp #

我被指派把一个程序分成不同的文件。分配为:每个文件应包含以下内容: 客户。h:应包含客户结构的定义和打印客户的声明。 customers.cpp:应包含打印客户的实现(或定义)。 练习1.5.cpp:应包括customers.h和主程序

这是我的密码:

客户。h

#pragma once;

void print_customers(customer &head);

struct customer
{
string name;
customer *next;

};
客户。cpp

#include <iostream>

using namespace std;

void print_customers(customer &head) 
{
customer *cur = &head;
while (cur != NULL)
{
    cout << cur->name << endl;
    cur = cur->next;

}

}
#include <iostream>
#include <string>
#include "customers.h"
using namespace std;

main()
{
    customer customer1, customer2, customer3;
    customer1.next = &customer2;
    customer2.next = &customer3;

    customer3.next = NULL;
    customer1.name = "Jack";
    customer2.name = "Jane";
    customer3.name = "Joe";
    print_customers(customer1);
    return 0;
}
有人能帮忙吗,我只是c的初学者++

void print_customers(customer &head);
C++编译器采用自顶向下的方法工作。因此,它在该点看到的每个类型、标识符都必须为它所知

问题是编译器不知道上面语句中的类型
customer
。在函数的转发声明之前尝试转发声明类型

struct customer;
或者在结构定义之后向前移动函数声明。

首先

#include "customers.h"  // in the "customers.cpp" file.
其次,
print\u customers
使用
customer
,但尚未声明此类型。有两种方法可以解决这个问题

  • 将函数声明放在结构声明之后
  • 在声明函数之前提交一个转发声明(
    struct customer;

  • 您需要在
    customers.h
    中进行许多更改。请参阅代码中的注释

    #pragma once;
    
    #include <string>        // including string as it is referenced in the struct
    
    struct customer
    {
        std::string name;    // using std qualifer in header
        customer *next;
    };
    
    // moved to below the struct, so that customer is known about
    void print_customers(customer &head);
    
    #pragma一次;
    #include//包含结构中引用的字符串
    结构客户
    {
    std::string name;//在标头中使用std限定符
    客户*next;
    };
    //移动到结构下方,以便了解客户
    无效打印客户(客户和负责人);
    
    然后,您必须在
    customers.cpp
    中包含“customers.h”

    请注意,我没有在头文件中使用命名空间std编写
    。因为这将把
    std
    名称空间导入到包含
    customer.h
    的任何内容中。有关更多详细信息,请参阅:

    #pragma once;
    
    #include <string>        // including string as it is referenced in the struct
    
    struct customer
    {
        std::string name;    // using std qualifer in header
        customer *next;
    };
    
    // moved to below the struct, so that customer is known about
    void print_customers(customer &head);