473,508 Members | 2,373 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 1750
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
4916
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...
0
1714
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....
7
5074
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...
3
14475
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...
0
1396
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...
2
4137
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:...
14
4604
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...
0
4875
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...
3
4358
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...
6
2683
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...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
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...
1
7036
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...
0
7489
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...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
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...
0
1547
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 ...
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.