0
votes

I'm trying to make a windows form application in Visual Studio C++, but I get this errors after compiling, for each function:

error LNK2005: function already defined in MyForm.obj

These are my files:

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
}

Header.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

So how can I fix errors?

1
Are you declaring and implementing the function in Header.h? - jpo38
That Source.cpp is not C++. You need to straighten out your language tags. - PaulMcKenzie
You are using C++ in a project that isn't C++. Windows Forms can only be used with C++/CLI. Hard to fathom why you, as the author, doesn't know, what programming language you are using. - IInspectable

1 Answers

0
votes

If you declare and implement a function in a header file (Header.h) and if this file gets included twice, then you'll most likely will get a function already defined error at some point.

This can be fixed by either:

  • Moving function implementation to a source (cpp) file and only keep it's declaration in the header (h) file

  • Make the function inline (if it is acceptable), that will remove the error