Search code examples
c++reusability

"Function was not declared" during multiple file compilation in C++


My source files look like the following

//functions.h
#ifdef FUNCTIONS
#define FUNCTIONS

bool isPrime(int num);

#endif

//function.cpp
include "functions.h"
bool isPrime(int num) {...}

//main.cpp
#include "functions.h"
#include <iostream>
int main()
{
    std::cout << isPrime(2);
    return 0;
}

I compiled them with g++ -o main main.cpp functions.cpp

It gave me the error message "isPrime" was not declared in this scope.


Solution

  • Your problem is so simple I didn't see it at first. Instead of #ifdef FUNCTIONS you need #ifndef FUNCTIONS. Note the n.