gpt4 book ai didi

c++ - 将函数分离到 C 中的另一个源文件时出错

转载 作者:行者123 更新时间:2023-11-27 22:48:14 25 4
gpt4 key购买 nike

我有一个简单的项目:

方法.h:

#pragma once
#ifdef _METHOD_
#define _METHOD_

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

int plus(int a, int b);

#endif // _METHOD_

方法.cpp:

#include "method.h"

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

源.cpp:

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

#include "method.h"

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

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

但是当我构建项目时,发生错误: enter image description here

我是 C 编程的新手。非常抱歉我的语法错误

最佳答案

移除

#ifdef METHOD 
#define METHOD

因为 #pragma once 做同样的事情,如果你想使用守卫,它应该是

#ifndef ....

#ifdef _METHOD_ 将忽略头文件,因为您从未定义“_METHOD_

更新#1

根据 MSDN#pragma once;

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

关于c++ - 将函数分离到 C 中的另一个源文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40809526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com