0

I have a simple project:

method.h:

#pragma once
#ifdef _METHOD_
#define _METHOD_

#include <stdio.h>
#include <conio.h>

int plus(int a, int b);

#endif // _METHOD_

method.cpp:

#include "method.h"

int plus(int a, int b)
{
    return a+b;
}

Source.cpp:

#include <stdio.h>
#include <conio.h>

#include "method.h"

void main()
{
    int a = plus(4, 5);
    printf("%d",a);

    printf("\n");
    _getch();
}

but when I build project, an error occure: enter image description here

I'm a newbie in C programming. And so sorry about my grammar mistakes

2 Answers2

3

remove

#ifdef METHOD 
#define METHOD 

as #pragma once does the same and if you want to use guards it should be

#ifndef ....

#ifdef _METHOD_ will ignore the header file as you are never defining "_METHOD_"

Update #1

As per MSDN on #pragma once;

Specifies that the file will be included (opened) only once by the compiler when compiling a source code file.

Community
  • 1
  • 1
Oliver Ciappara
  • 668
  • 1
  • 9
  • 19
  • oh, sorry, ifndef instead of ifdef. Thank you very much ^_^ – Trần Hồng Phát Nov 25 '16 at 16:58
  • 2
    @TrầnHồngPhát Useful reading on what's going on there: http://stackoverflow.com/questions/8020113/c-include-guards and https://en.wikipedia.org/wiki/Include_guard . `#pragma once` is a type of include guard that is not covered by the standard so not all compilers support it. In fact, that's what `#pragma` means. When you see a `#pragma`, make sure your compiler supports it because if it does not, the compiler is allowed to silently ignore it and much debugging may ensue. [Be warned of a few problems with `#pragma once`](http://stackoverflow.com/a/34884735/4581301) that block standardization. – user4581301 Nov 25 '16 at 18:41
1

Firstly, change "#ifdef METHOD" in your header file to "#ifndef METHOD"

santiPipes
  • 51
  • 7