我已經被賦予了將一個程序分成不同文件的任務。分配是:每個文件應包含以下內容: 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