1>knightTour_Main.obj : error LNK2005: "int p" (?p@@3HA) already defined in Definitions.obj
1>knightTour_Main.obj : error LNK2005: "int q" (?q@@3HA) already defined in Definitions.obj
Error^
This is the code where I’ve defined it in a header file:
#include<iostream>
using namespace std;
int p,q;
int f( int, int,const int [][8],const int [][8], int [],int []);
I’ve tried
extern int p,q;
But I get this error after that
1>knightTour_Main.obj : error LNK2001: unresolved external symbol "int p" (?p@@3HA)
1>knightTour_Main.obj : error LNK2001: unresolved external symbol "int q" (?q@@3HA)
Do you recommend anything else? Thank you.
Replacing the
int p,q;withextern int p,q;was correct, but you also need to includeint p,q;in exactly one .cpp fileIf you have a definition (
int p,q;) in a header, then you’ll get one copy ofpandqallocated for each file that includes that header, and they conflict (the “already defined” error). If you replace it with a declaration (extern int p,q;), you don’t getporqallocated anywhere.