473,660 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with linking errors in C++

nabh4u
62 New Member
i have 3 files namely acme.cpp, match.cpp and match.h
i am getting these errors :

Error1 error LNK2005: "int n" (?n@@3HA) already defined in acme.obj match.obj

Error2 error LNK2005: "int m" (?m@@3HA) already defined in acme.obj match.obj

following are my files:
/----------------------------------------------------------------------/
Header file match.h

#include <iostream>
#include<vector >

using namespace std;

/*------Declarations------*/

int n; // No. of companies.
int m; // No. of persons.
void read(); // Reads the input from user.

// class Data Structure for Company.
class company
{
public:
int openPos; // No. of open positions in a company.
int perAccept; // No. of persons acceptable by company.
vector <int> compPref; // Vector with preferences of companies.
vector <int> cId; // Vector containing Id's of companies.
};

// Class Data Structure for Person.
class person
{
public:
int comAccept; // No. of companies acceptable by person.
vector <int> pId; // Vector containing Id's of persons.
vector<int> perPref; // Vector with preferences of persons.
};
/----------------------------------------------------------------------------------------------------/
acme.cpp

# include"match.h "

using namespace std;

int main()
{
cout<<"xxxxx\n" ;
read(); // reads the values from the user.
return 0;
}
/----------------------------------------------------------------------------------------------------/
match.cpp

# include"match.h "

using namespace std;

void read()
{
cout<<"Enter the number of companies : ";
cin>>n;
cout<<"Enter the number of persons : ";
cin>>m;
}
/--------------------------------------------------------------------------------------------------/
Jan 25 '07 #1
4 1757
horace1
1,510 Recognized Expert Top Contributor
you define n and m in match.h and include it in both your .cpp files.
Declare the variables extern in the header file and define them in one of the .cpp files, e.g.
match.h
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5.  
  6. /*------Declarations------*/
  7.  
  8. extern int n; // No. of companies.
  9. extern int m; // No. of persons.
  10. void read(); // Reads the input from user.
  11.  
  12. // class Data Structure for Company.
  13. class company
  14. {
  15. public:
  16. int openPos; // No. of open positions in a company.
  17. int perAccept; // No. of persons acceptable by company.
  18. vector <int> compPref; // Vector with preferences of companies.
  19. vector <int> cId; // Vector containing Id's of companies.
  20. };
  21.  
and the .cpp files
Expand|Select|Wrap|Line Numbers
  1. # include"match.h"
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. cout<<"xxxxx\n";
  8. read(); // reads the values from the user.
  9. return 0;
  10. }
  11.  
Expand|Select|Wrap|Line Numbers
  1. # include"match.h"
  2.  
  3. using namespace std;
  4. int n; // No. of companies.
  5. int m; // No. of persons.
  6.  
  7. void read()
  8. {
  9. cout<<"Enter the number of companies : ";
  10. cin>>n;
  11. cout<<"Enter the number of persons : ";
  12. cin>>m;
  13. }
  14.  
then you won't get multiple definitions
Jan 25 '07 #2
Motoma
3,237 Recognized Expert Specialist
you define n and m in match.h and include it in both your .cpp files.
Declare the variables extern in the header file and define them in one of the .cpp files then you won't get multiple definitions
Correct me if I'm wrong, but this could also be solved by including a simple #ifndef directive:

Expand|Select|Wrap|Line Numbers
  1. #ifndef _MATCH_H_
  2. #define _MATCH_H_
  3. ...
  4. //rest of match.h
  5. ...
  6. #endif
  7.  
Jan 25 '07 #3
nabh4u
62 New Member
thank you...that works.



you define n and m in match.h and include it in both your .cpp files.
Declare the variables extern in the header file and define them in one of the .cpp files, e.g.
match.h
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5.  
  6. /*------Declarations------*/
  7.  
  8. extern int n; // No. of companies.
  9. extern int m; // No. of persons.
  10. void read(); // Reads the input from user.
  11.  
  12. // class Data Structure for Company.
  13. class company
  14. {
  15. public:
  16. int openPos; // No. of open positions in a company.
  17. int perAccept; // No. of persons acceptable by company.
  18. vector <int> compPref; // Vector with preferences of companies.
  19. vector <int> cId; // Vector containing Id's of companies.
  20. };
  21.  
and the .cpp files
Expand|Select|Wrap|Line Numbers
  1. # include"match.h"
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. cout<<"xxxxx\n";
  8. read(); // reads the values from the user.
  9. return 0;
  10. }
  11.  
Expand|Select|Wrap|Line Numbers
  1. # include"match.h"
  2.  
  3. using namespace std;
  4. int n; // No. of companies.
  5. int m; // No. of persons.
  6.  
  7. void read()
  8. {
  9. cout<<"Enter the number of companies : ";
  10. cin>>n;
  11. cout<<"Enter the number of persons : ";
  12. cin>>m;
  13. }
  14.  
then you won't get multiple definitions
Jan 25 '07 #4
horace1
1,510 Recognized Expert Top Contributor
Correct me if I'm wrong, but this could also be solved by including a simple #ifndef directive:

Expand|Select|Wrap|Line Numbers
  1. #ifndef _MATCH_H_
  2. #define _MATCH_H_
  3. ...
  4. //rest of match.h
  5. ...
  6. #endif
  7.  
you use #ifndef when a file may include the same header file more than once, e.g. you have a header file that includes other header files etc etc.- a file may include b.h and c.h both of which include a.h and you can end up compiling the same header file multiple times (which if it only contains declarations would not cause errors but is very inefficent)

In this case two seperate C++ files included the same header file which defined n and m, hence memory was allocated for n and m in both object files leading to multiple definition of the variables at link time

have a look at
http://www.cs.odu.edu/~zeil/cs361/Le...headerdef.html
Jan 25 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
4924
by: Joonas Paalasmaa | last post by:
Hi, When compiling Sketch's streamfilter C extension the errors below are raised during linking. What could cause the errors? (Python 2.3, MinGw 1.1 with GCC 2.95.3-6, Windows 98) Here are the occurrences of FilterType that may be relevant: ------- C:\sketch\sketch-0.7.12\Filter\filterobj.c: 949: PyTypeObject FilterType = {
0
1724
by: SM | last post by:
Hi, While linking orbix C++ application, we get a lot of linking errors. Few errors are pasted below. If someone could tell us which are the libraries that need to be added, it would be of help. Version: Orbix 3.3 C++ edition. Errors: CORBA::Request::decodeLongArray(long*&, unsigned long&) .../sunos6/POSS.o
7
5104
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting normal functions in header files results in multiple definition errors even when include guards are used. -- STH Hatton's Law: "There is only One inviolable Law" KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com Mozilla:...
3
14512
by: Saurabh Aggrawal | last post by:
Hi, I am porting an application for 64-bit AMD processor and while linking the application i am getting the following errors: Processing directory uidll... Linking DLL G:\NewUIPackers1\drivers\Unser\bin\WIN32\DEBUG\PPKAW1UI.dll .... LINK : warning LNK4224: /PDBTYPE is no longer supported; ignored UIDLL.def : error LNK2001: unresolved external symbol DllCanUnloadNow
0
1411
by: Chris | last post by:
Hi I have a project that was written in V6.0 (c/c++) and it works just fine, but when I run it in .NET 2003 (V7.0), I get a whole lot of linking errors. I have spent alot of time trying to solve it but to no avail....please help! the errors that show up are: Compiling...
2
4162
by: noe | last post by:
Hello, I have linked a '.c' file and I have obtained next fail: C:\prueba2\sfilter>build -cZ BUILD: Object root set to: ==> objchk BUILD: Adding /Y to COPYCMD so xcopy ops won't hang. BUILD: /i switch ignored BUILD: Compile and Link for i386 BUILD: Examining c:\prueba2\sfilter directory for files to compile.
14
4621
by: m | last post by:
all, i am trying to use the function round() which I found through google to be declared in math.h ( http://www.gnu.org/software/libc/manual/html_node/Rounding-Functions.html). this function does not work and i get an unreferenced symbol error. the function rint() works when math.h is included. is round() not found in all math.h files? if so what might be an alternative. i work on a machine that runs a sparc-sun-solaris operating system...
0
4881
by: Adam Clauss | last post by:
I have managed C++ library (is bridging between a Win32 .dll and a C# application). All was well when compiled under VS2003, but I am running into a series of linking errors when compiling against VS2005. They all/mostly seem to be within the STL. Any idea what might cause something like this? Linking errors follow. ---
3
4371
by: walkeraj | last post by:
I'm trying to compile an open source game called IVAN , and I'm able to compile it from a makefile, but not from an IDE. I have attempted to recreate the way the makefile compiles the project as closely as possible, yet I'm getting odd linking errors. I think I've traced it to some kind of problem with the SDL libraries it uses, yet I can't explain why it will compile from the makefile and not from the IDE. I have made sure that the ide...
6
2694
by: John | last post by:
I have 5 native static libraries that are being compiled in Visual Studio 2005 with the /MDd C Runtime option. I have 2 CLR DLLs (all managed code) in Visual Studio 2005 and the /MDd C Runtime setting. They are in the same solution, and compile/link without problems (Debug configuration). I also have 1 mixed-mode CLR DLL that consumes both the static libraries and managed DLLs. It is configured to use the /MDd C Runtime library. When...
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8539
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.