Error when separating function into another source file in C

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

Upvotes: 0

Views: 69

Answers (2)

Ol1v3r
Ol1v3r

Reputation: 788

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.

Upvotes: 3

santiPipes
santiPipes

Reputation: 61

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

Upvotes: 1

Related Questions