我已经被赋予了将一个程序分成不同文件的任务。分配是:每个文件应包含以下内容: customers.h:应包含客户结构的定义和打印客户的声明。 customers.cpp:应包含打印客户的实现(或定义)。 练习1 5.cpp:应包含customers.h和主程序。C++单独实现和头文件
这里是我的代码:
customers.h
#pragma once;
void print_customers(customer &head);
struct customer
{
string name;
customer *next;
};
customers.cpp
#include <iostream>
using namespace std;
void print_customers(customer &head)
{
customer *cur = &head;
while (cur != NULL)
{
cout << cur->name << endl;
cur = cur->next;
}
}
exercise_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++
非常有见识和明确的支持。谢谢! – user1816464
@ user1816464谢谢:) – Steve