我被分配了将程序拆分为不同文件的任务。任务是: 每个文件都应包含以下内容: customers.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;
}
}
练习_1_5.cpp
#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;
}
它在单个程序中编译并运行良好,但是当我尝试将其拆分并编译时g++ -o customers.cpp
我收到此错误
customers.cpp:4:22: error: variable or field ‘print_customers’ declared void
customers.cpp:4:22: error: ‘customer’ was not declared in this scope
customers.cpp:4:32: error: ‘head’ was not declared in this scope
谁能帮忙,我只是c ++的初学者