Function from different .cpp

closed account (jLNv0pDG)
I am trying to use a function defined in one .cpp file in another. If I'm reading this right:
1. you define the function prototype in a header file,
2. #include that header file in both .cpp files; and
3. define the function in message.cpp
I think cout / <iostream> might be what is messing me up here.

1
2
3
4
5
6
7
8
9
10
11
//main.cpp
#include <iostream>
#include "message.h"

int main()
{
message();

std::cout << "Goodbye World!";

}


1
2
//message.h
void message(void);


1
2
3
4
5
6
7
8
9
//message.cpp -- "Hello World!"

#include <iostream>
#include "message.h"

void message(void)
{
std::cout << "Hello World!";
}


edit: typo corrected.
Last edited on
closed account (1yR4jE8b)
Your prototype in your header file is missing a semi-colon, don't know if that's your mistake or just a typo. Otherwise, that's how it works. You should also put include guards in the header file to guard against multiple inclusion.

You don't need to specify 'void' for the function parameter if there is no parameters, just leave it blank.
Don't forget to 'return 0' from main.
what errors are you getting
closed account (jLNv0pDG)
*facepalm*

The amount of silly mistakes I'm making.

In main.cpp I was doing:

cout << message()

Yet I managed to write it correctly here. ^.^
Topic archived. No new replies allowed.