Nick Nick - 13 days ago 7
C++ Question

Can't access function from main file from header file C++

I don't know why I can't access the function clearConsole() from my .cpp file from the header files, I guess I'm calling it wrong? How do I target the main file from a header file? I try to call the clearConsole() function after the user input in the addCustomer() functinon in customer.h.

Main.cpp

// OTS.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

#include "customer.h"

// Clear function specific to Windows
// Cross platform alternatives are more convoluted to reach desired effect, so have not been included
void clearConsole()
{
#ifdef _WIN32
system("cls");
#endif
}


Customer.h

//customer.H
//The object class customer

class customer
{
//...
clearConsole();
}

Answer

If your files are linked together, a forward declaration of the functions should be enough.

Customer.h

//customer.H
//The object class customer

void clearConsole(); // <--- declare function

class customer
{

//....

};

But this structure looks wrong. I would declare the function in a different header, inside a namespace, and define it in a corresponding implementation file:

clearconsole.h

namespace ConsoleUtils
{
    void clearConsole();
}

clearconsole.cpp

namespace ConsoleUtils
{
    void clearConsole()
    {
    }
}