Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误LNK2005:函数已在MyForm.obj中定义_C++_Visual Studio_Visual C++ - Fatal编程技术网

C++ 错误LNK2005:函数已在MyForm.obj中定义

C++ 错误LNK2005:函数已在MyForm.obj中定义,c++,visual-studio,visual-c++,C++,Visual Studio,Visual C++,我试图在VisualStudioC++中制作Windows窗体应用程序,但编译后,我会得到这些错误,对于每个函数: 错误LNK2005:函数已在MyForm.obj中定义 这些是我的文件: Source.cpp #pragma once #include "MyForm.h" using namespace System; using namespace System::Windows::Forms; [STAThread]//leave this as is void main() {

我试图在VisualStudioC++中制作Windows窗体应用程序,但编译后,我会得到这些错误,对于每个函数:

错误LNK2005:函数已在MyForm.obj中定义

这些是我的文件:

Source.cpp

#pragma once
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;

[STAThread]//leave this as is
void main() {
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);
    Application::Run(gcnew Project1::MyForm);
}
MyForm.h

#pragma once
#include <iostream>
#include <string>
#include "Header.h"

namespace Project1 {
    //codes of te form
}
#pragma一次
#包括
#包括
#包括“Header.h”
命名空间项目1{
//te格式代码
}
标题.h

#pragma once
#include <iostream>
#include <string>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;
int n, m;
int size1, size2;
//My functions here
#pragma一次
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int n,m;
国际标准尺寸1,国际标准尺寸2;
//我在这里的职能

那么如何修复错误呢?

如果您在头文件(header.h)中声明并实现一个函数,并且如果该文件被包含两次,那么您很可能会在某个时候得到一个已定义的
函数

这可以通过以下方式解决:

  • 将函数实现移动到源(cpp)文件,并仅将其声明保留在头(h)文件中

  • 使函数
    内联(如果可以接受),这将删除错误


  • < /UL>是否声明和实现了Source.cpp?不是C++的Health.h中的函数。你需要整理你的语言标签。你在一个不是C++的项目中使用C++。Windows窗体只能与C++/CLI一起使用。很难理解为什么作为作者,你不知道自己使用的是什么编程语言。可能与@jpo38重复的是我的标题。h:“你的代码看起来更像C#而不是C++”-它是C++/CLI。C#不使用
    作为范围解析运算符,也没有
    gcnew
    关键字。@IInspectable:OK删除了我的注释。谢谢,这没什么变化。你在谈论C++特定的规则,但是OP不使用C++。这真的只是猜测。也许是对的,但你真的应该先写一条评论要求澄清。@jpo38非常感谢!通过使所有函数
    内联
    解决了我的问题。