2

I have been given an assignment to split up a program into different files. The assignment was: 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.

Here is my code:

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;
}

It compiles and runs fine in a single program but when i try to split it up and compile with g++ -o customers.cpp

I receive this error

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

Can anyone help, I am just a beginner with c++

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user1816464
  • 223
  • 1
  • 3
  • 9

3 Answers3

2
void print_customers(customer &head);

C++ compiler work in top to bottom approach. So, every type, identifier it sees at the point must be known to it.

The problem is that compiler doesn't know the type customer in the above statement. Try forward declaring the type before the function's forward declaration.

struct customer;

Or move the function forward declaration after the struct definition.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
2

First,

#include "customers.h"  // in the "customers.cpp" file.

Second, print_customers uses customer, but this type is not declared yet. You have two ways to fix the problem.

  1. Put declaration of the function after declaration of the struct.
  2. Put a forwarded declaration (struct customer;) before declaration of the function,
masoud
  • 55,379
  • 16
  • 141
  • 208
1

There are a number of changes that you need in customers.h. See the comments in the code.

#pragma once;

#include <string>        // including string as it is referenced in the struct

struct customer
{
    std::string name;    // using std qualifer in header
    customer *next;
};

// moved to below the struct, so that customer is known about
void print_customers(customer &head);

You must then #include "customers.h" in customers.cpp.

Note how I didn't write using namespace std in the header file. As this would import the std namespace into anything that included customer.h. For more details see: Why is including "using namespace" into a header file a bad idea in C++?

Community
  • 1
  • 1
Steve
  • 7,171
  • 2
  • 30
  • 52