0
votes

I am using visual studio 2012 to create a windows forms application. The problem i am having is that when i was programming the main bulk of the program and only had one form i wanted 2 vectors to be global across that form. however now that the main bulk of programming is done i want to focus on the system flow and as such introduced a second form to act as the Main screen where the user will press a button and go through to the form i've been working on until now. The problem is the vectors that i defined as follows:

namespace Project1 {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

    vector<vector<Hexagon>>Grid; // 2d vector to hold grid of hexagon
    vector<Hexagon>Selected; // 1d vector to hold selected hexagons
/// <summary>
/// Summary for MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form{
....

I get the following errors when i compile the program:

1) Error 47 error LNK2005: "class std::vector >,class std::allocator > > > Project1::Grid" (?Grid@Project1@@3V?$vector@V?$vector@VHexagon@@V?$allocator@VHexagon@@@std@@@std@@V?$allocator@V?$vector@VHexagon@@V?$allocator@VHexagon@@@std@@@std@@@2@@std@@A) already defined in MainScreen.obj C:\Users\Ed\Documents\Visual Studio 2012\Projects\Project1\Project1\Myform.obj

2)Error 48 error LNK2005: "class std::vector > Project1::Selected" (?Selected@Project1@@3V?$vector@VHexagon@@V?$allocator@VHexagon@@@std@@@std@@A) already defined in MainScreen.obj C:\Users\Ed\Documents\Visual Studio 2012\Projects\Project1\Project1\Myform.obj

Just to clarify MainScreen is the form i added, it is called first.

MyForm is the form accessed by pressing a button in MainScreen

I hope i've provided enough information. I think it might be something to do with declaring both vectors in project1 in MyForm.h as shown above, i would make them local to the MyForm class but i cant because it is a managed class. Thanks

1
Do you know C++? You defined global variables in h-file. Now, if you include such h-file to more than one .cpp file, you have linker errors/ - Alex F
The solution is: define both variables as extern in h-file, and create them in one .cpp file. - Alex F
Derp!! of course! thank you should have realised that earlier - JabbaWook
interestingly it now works if i put namespace{} around the vector definitions, is that bad practise? - JabbaWook
h and cpp file should match. If you decided to place these vatiables in namespace in h-file, do the sane in cpp file. - Alex F

1 Answers

0
votes

Standard C++ solution to prevent such linker error is:

// MyForm.h file:
extern vector<vector<Hexagon>>Grid; // 2d vector to hold grid of hexagon
extern vector<Hexagon>Selected; // 1d vector to hold selected hexagons

// MyForm.cpp file:
namespace Project1 {
vector<vector<Hexagon>>Grid; // 2d vector to hold grid of hexagon
vector<Hexagon>Selected; // 1d vector to hold selected hexagons