Hi DW, I'm having an issue here, I have 3 forms namely start.h,home,Form1 in their order of execution, the problem starts when I include Form 1 to any of these forms then I get LNK2005 'unsigned short MyprojectName::m_hservice' (?m_hservice@MyprojectName@@$$Q3GA) Already defined in home.obj

NB these forms are completely different, the first one which is start.h only let the user to click a button then show home which home is a password form when a user keys the correct credentials then it should now show Form1 but when I #include 'Form1.h' to home.hI get this error but hoeme.h doesn't have m_hservice at all being defined by myself so I don't know where it's taking it from because I never did that and it's not there as well.

Anyone who knows how I can solve this problem?

I think you're hitting a common linker issue. That LNK2005 error usually pops up when a variable is defined in more than one place. Check if m_hservice is being defined in multiple files. It might help to declare it as extern in your headers and then just define it in one source file. Hope that helps!

Anyone who knows how I can solve this problem?

The short answer is don't put code in header files.

Declarations go in header files, definitions go in source files.

You get away with it in small projects where you only have one source file, and each header is included exactly once.

But as soon as it gets a bit more complicated, the idea falls apart.

@WilliamOG This variable is not declared anywhere else except on Form1 where it is giving me problems.
The issue seems to be with

DWORD m_dwRVersion;
HRESULT m_hr;
WFSVERSION m_version;
HSERVICE m_hservice;
HWND m_handleWind;
LPWFSRESULT m_result;

@SaleM the code itself doesn't have error because I can execute Form1 without having it being called or started by another Form. The problem starts when I want Form1 to be started by another form.

To clear a picture for you, this problem shows even if you create a new form and only include form1 so that you will be able to Form1 ^ form1 = gcnew Form1; form1->Show();
But once you include form1 this error will show and remember this is a new form with no such code or declaration but as soon as you include form1 it will produce this error.

It seems to me you need to put the definitions in form1.h into a form1.cpp file.

Header files contain declarations.

Source files contain definitions.

If you put definitions in a header file, and include that file twice, you end up with something defined twice.

then I get LNK2005 'unsigned short MyprojectName::m_hservice' (?m_hservice@MyprojectName@@$$Q3GA) Already defined in home.obj

This is what the "Already defined" error message means.

I did try moving

DWORD m_dwRVersion;
HRESULT m_hr;
WFSVERSION m_version;
HSERVICE m_hservice;
HWND m_handleWind;
LPWFSRESULT m_result;

From Form1.h to Form1.cpp but when I do that I get this error error C2146: syntax error : missing ';' before identifier 'm_dwRVersion'

Well you still need to include Form1.h in Form1.cpp

Also, Form1.h will need changing as follows

extern DWORD m_dwRVersion;
extern HRESULT m_hr;
extern WFSVERSION m_version;
extern HSERVICE m_hservice;
extern HWND m_handleWind;
extern LPWFSRESULT m_result;

Thank you so much, I would not have figured that out. You are a champ.

Oops when I uncomment the code as I've commented it out while I was having that error so that I can continue with the other development on the Form now I get
error LNK2020: unresolved token (0A0000DF) "unsigned short MyprojectName::m_hservice"

error LNK2020: unresolved token (0A0000DF) "unsigned short MyprojectName::m_hservice"

Did you add Form1.cpp to the project?

commented: How? +8

The file is there and it also includes Form1.h

No one know a fix or a solution to this problem?

It's hard to visualise what you're doing wrong.

You started with
'unsigned short MyprojectName::m_hservice' (?m_hservice@MyprojectName@@$$Q3GA) Already defined in home.obj
meaning you have more than one instance of m_hservice.

You now have
error LNK2020: unresolved token (0A0000DF) "unsigned short MyprojectName::m_hservice"
meaning you have zero instances of m_hservice.

You claim to have moved a few things into Form1.cpp, but did you add that to the project?

In your IDE, do the following 2 steps.

  1. Build->Clean
  2. Build->Rebuild all

Then paste the output of the build log.
You can censor your windows username if it appears in any file paths it prints if you want.
The important bit will be the list of files it compiles, and the resulting error message(s).

Here's my build log

1>------ Rebuild All started: Project: myprojectname, Configuration: Debug Win32 ------
1>  stdafx.cpp
1>  AssemblyInfo.cpp
1>  Form1.cpp
1>  Form2.cpp
1>  home1.cpp
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(13): warning C4005: 'EXIT_FAILURE' : macro redefinition
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\stdlib.h(46) : see previous definition of 'EXIT_FAILURE'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(711): warning C4101: 'WfsVersion' : unreferenced local variable
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(709): warning C4101: 'data' : unreferenced local variable
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(846): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(905): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(940): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(975): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(1010): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(1045): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(1080): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(1141): warning C4996: '_fcvt': This function or variable may be unsafe. Consider using _fcvt_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\stdlib.h(726) : see declaration of '_fcvt'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(1145): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\home1.h(755): warning C4101: 'e' : unreferenced local variable
1>  starter.cpp
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\starter.h(2): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(13): warning C4005: 'EXIT_FAILURE' : macro redefinition
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\stdlib.h(46) : see previous definition of 'EXIT_FAILURE'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(711): warning C4101: 'WfsVersion' : unreferenced local variable
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(709): warning C4101: 'data' : unreferenced local variable
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(846): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(905): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(940): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(975): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(1010): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(1045): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(1080): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(1141): warning C4996: '_fcvt': This function or variable may be unsafe. Consider using _fcvt_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\stdlib.h(726) : see declaration of '_fcvt'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(1145): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\home1.h(755): warning C4101: 'e' : unreferenced local variable
1>  myprojectname.cpp
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(13): warning C4005: 'EXIT_FAILURE' : macro redefinition
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\stdlib.h(46) : see previous definition of 'EXIT_FAILURE'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(711): warning C4101: 'WfsVersion' : unreferenced local variable
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(709): warning C4101: 'data' : unreferenced local variable
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(846): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(905): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(940): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(975): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(1010): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(1045): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(1080): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(1141): warning C4996: '_fcvt': This function or variable may be unsafe. Consider using _fcvt_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\stdlib.h(726) : see declaration of '_fcvt'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\Form1.h(1145): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\home1.h(755): warning C4101: 'e' : unreferenced local variable
1>myprojectname.cpp(6): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>c:\users\mypcname\documents\visual studio 2010\projects\myprojectname\myprojectname\starter.h(2): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>  Generating Code...
1>  .NETFramework,Version=v4.0.AssemblyAttributes.cpp
1>myprojectname.obj : error LNK2020: unresolved token (0A0000DF) "unsigned short MyProjectName::m_hservice" (?m_hservice@MyProjectName@@$$Q3GA)
1>starter.obj : error LNK2020: unresolved token (0A0000DB) "unsigned short MyProjectName::m_hservice" (?m_hservice@MyProjectName@@$$Q3GA)
1>home1.obj : error LNK2020: unresolved token (0A0000DB) "unsigned short MyProjectName::m_hservice" (?m_hservice@MyProjectName@@$$Q3GA)
1>myprojectname.obj : error LNK2020: unresolved token (0A0000E5) "struct _wfsversion MyProjectName::m_verSion" (?m_verSion@MyProjectName@@$$Q3U_wfsversion@@A)
1>starter.obj : error LNK2020: unresolved token (0A0000E1) "struct _wfsversion MyProjectName::m_verSion" (?m_verSion@MyProjectName@@$$Q3U_wfsversion@@A)
1>home1.obj : error LNK2020: unresolved token (0A0000E1) "struct _wfsversion MyProjectName::m_verSion" (?m_verSion@MyProjectName@@$$Q3U_wfsversion@@A)
1>myprojectname.obj : error LNK2020: unresolved token (0A0000E7) "long MyProjectName::m_hr" (?m_hr@MyProjectName@@$$Q3JA)
1>starter.obj : error LNK2020: unresolved token (0A0000E3) "long MyProjectName::m_hr" (?m_hr@MyProjectName@@$$Q3JA)
1>home1.obj : error LNK2020: unresolved token (0A0000E3) "long MyProjectName::m_hr" (?m_hr@MyProjectName@@$$Q3JA)
1>myprojectname.obj : error LNK2020: unresolved token (0A0000E8) "unsigned long MyProjectName::m_dwRVersion" (?m_dwRVersion@MyProjectName@@$$Q3KA)
1>starter.obj : error LNK2020: unresolved token (0A0000E4) "unsigned long MyProjectName::m_dwRVersion" (?m_dwRVersion@MyProjectName@@$$Q3KA)
1>home1.obj : error LNK2020: unresolved token (0A0000E4) "unsigned long MyProjectName::m_dwRVersion" (?m_dwRVersion@MyProjectName@@$$Q3KA)
1>home1.obj : error LNK2001: unresolved external symbol "unsigned short MyProjectName::m_hservice" (?m_hservice@MyProjectName@@$$Q3GA)
1>starter.obj : error LNK2001: unresolved external symbol "unsigned short MyProjectName::m_hservice" (?m_hservice@MyProjectName@@$$Q3GA)
1>myprojectname.obj : error LNK2001: unresolved external symbol "unsigned short MyProjectName::m_hservice" (?m_hservice@MyProjectName@@$$Q3GA)
1>home1.obj : error LNK2001: unresolved external symbol "long MyProjectName::m_hr" (?m_hr@MyProjectName@@$$Q3JA)
1>starter.obj : error LNK2001: unresolved external symbol "long MyProjectName::m_hr" (?m_hr@MyProjectName@@$$Q3JA)
1>myprojectname.obj : error LNK2001: unresolved external symbol "long MyProjectName::m_hr" (?m_hr@MyProjectName@@$$Q3JA)
1>home1.obj : error LNK2001: unresolved external symbol "struct _wfsversion MyProjectName::m_verSion" (?m_verSion@MyProjectName@@$$Q3U_wfsversion@@A)
1>starter.obj : error LNK2001: unresolved external symbol "struct _wfsversion MyProjectName::m_verSion" (?m_verSion@MyProjectName@@$$Q3U_wfsversion@@A)
1>myprojectname.obj : error LNK2001: unresolved external symbol "struct _wfsversion MyProjectName::m_verSion" (?m_verSion@MyProjectName@@$$Q3U_wfsversion@@A)
1>home1.obj : error LNK2001: unresolved external symbol "unsigned long MyProjectName::m_dwRVersion" (?m_dwRVersion@MyProjectName@@$$Q3KA)
1>starter.obj : error LNK2001: unresolved external symbol "unsigned long MyProjectName::m_dwRVersion" (?m_dwRVersion@MyProjectName@@$$Q3KA)
1>myprojectname.obj : error LNK2001: unresolved external symbol "unsigned long MyProjectName::m_dwRVersion" (?m_dwRVersion@MyProjectName@@$$Q3KA)
1>C:\Users\mypcname\Documents\Visual Studio 2010\Projects\myprojectname\Debug\myprojectname.exe : fatal error LNK1120: 16 unresolved externals
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Why is Form1.h over 1000 lines long?

Why are you using C-style strncpy all over the place, and not std::string (since this is C++).

Does Form1.cpp include Form1.h?
The lack of error messages when you compile Form1.cpp suggests not.

I tried to send a PM to you, but it's disabled.

Why is Form1.h over 1000 lines long?

This is because of the the functions as well as the controls (GUI)

Why are you using C-style strncpy all over the place, and not std::string (since this is C++).

The code originally is from C

Does Form1.cpp include Form1.h?

Yes it does

I tried to send a PM to you, but it's disabled.

I also tried and I see it's not available.

An update
I've moved
extern DWORD m_dwRVersion; extern HRESULT m_hr; extern WFSVERSION m_version; extern HSERVICE m_hservice; extern HWND m_handleWind; extern LPWFSRESULT m_result;
From top by using namespace System::Drawing; to inside of a function and now it's runs. The problem is that the m_hservice has to be global because it value has to be used by other functions as now if I just add it, it returns invalid service error which is error -22.

If I put back the m_hservice I get the error unsigned short.

@Salem here's my email informationupdatesa@gmail.com

information.updatesa@gmail.com

I've responded to your email address (FYI, gmail ignores dots).
You should edit your posts to remove your personal info.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.