Separating routines into a separate implentation and header file

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?
Last edited on
You need to #include "customers.h" in customers.cpp.
Hi Stewbond, thanks for the speedy response. I tried that and now I am getting the following error:

In file included from C:\Users\Ben\Documents\CS264\lab3\customers.cpp:3:0:
C:\Users\Ben\Documents\CS264\lab3\customers.h:7:5: error: 'string' does not name a type
C:\Users\Ben\Documents\CS264\lab3\customers.cpp: In function 'void print_customers(customer&)':
C:\Users\Ben\Documents\CS264\lab3\customers.cpp:11:22: error: 'struct customer' has no member named 'name'

I have edited my code above to reflect the previous code change already.
Move line 2 from exercise_1_5.cpp to customers.h. And change the definition of name to std::string name;. The compiler doesn't know what a string is when it see the variable name.
Last edited on
Thanks doug4, I think I'm getting closer. I just have one remaining error:

C:\Users\Ben\Documents\CS264\lab3\exercise_1_5.o:exercise_1_5.cpp:(.text+0xd8): undefined reference to `print_customers(customer&)'
You need to link in customers.o
Thanks Doug4, got it sorted :)
Topic archived. No new replies allowed.