문제

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.

도움이 되었습니까?

해결책

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

다른 팁

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

Try this

Depending on what compiler you use, you could also use the following directive:

#pragma once

What this does is tell the compiler, "Hey, #include me only once", just like with the header guards. It may, however, be more effective in preventing redefinition linker errors.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top