I am trying to separate out particular sets of routines into a separate implentation and header file which can be compiled independently to the main program such that the program source consists of a file called customers.h, customers.cpp, and exercise_1_5.cpp
Each of the files should contain the following:
customers.h should contain the definition of the customer structure and the declaration of print_customers.
customers.cpp should contain the implementation (or definition) for print_customers.
exercise_1_5.cpp should contain an include of customers.h and the main program.
This is my original code from a single .cpp file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include<iostream>
#include<string>
using namespace std;
struct customer{
string name;
customer *next;
};
void print_customers(customer &head){
customer *cur = &head;
while (cur != NULL){
cout << cur->name << endl;
cur = cur->next;
}
}
int 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;
}
|
This is new header file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
//File: customers.h
#include<string>
#pragma once
struct customer{
std::string name;
customer *next;
};
void print_customers(customer &head);
This is customers.cpp file:
[code]
//File: customers.cpp
#include <iostream>
#include "customers.h"
using namespace std;
void print_customers(customer &head){
customer *cur = &head;
while (cur != NULL){
cout << cur->name << endl;
cur = cur->next;
}
}
|
And this is my re-edited exercise_1_5.cpp file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include<iostream>
#include<string>
#include"customers.h"
using namespace std;
void print_customers(customer &head);
int 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;
}
|
The error messages I am getting from the compiler on the customers.cpp file:
C:\Users\Ben\Documents\CS264\lab3\customers.cpp:5:22: error: variable or field 'print_customers' declared void
C:\Users\Ben\Documents\CS264\lab3\customers.cpp:5:22: error: 'customer' was not declared in this scope
C:\Users\Ben\Documents\CS264\lab3\customers.cpp:5:32: error: 'head' was not declared in this scope
Can anybody please give me some guidance or assistance so that I can get this to compile and run correctly?