Burnie Burnie - 1 month ago 5
C++ Question

Calling methods from a header from another cc file?

How do you call a method from a header file that a .cpp file contains?

For example:

main.cpp

#include <iostream>
#include "test.h"
using namespace std;
int main() {
sayHelloWorld();
}


test.h

void sayHelloWorld();


test.cpp

#include "test.h"

void sayHelloWorld() {
printf("HELLO WORLD!\n");
}


Sorry if this seems like an easy question for some of you. :/

Pat Pat
Answer

Your example is the standard way of doing this. Remark: in test.cpp you'll need to #include <stdio.h> or better #include <cstdio> in order to use the printf function.

Comments