C++/CLI - Zmienne globalne i 2 windows forms

0

Witam,

Walczę cały dzień z VS nie mogąc uzyskać dostępu do zmiennych globalnych z 2 różnych Form.

Plik Form1.h (główna) - fragment
.#include <vector>
.#include "toDoForm.h"
.#include "globalVar.h"

Plik globalVar.h
.#ifndef NAMESPACE_FOO
.#define NAMESPACE_FOO

.#include <vector>
using namespace std;

namespace Foo
{
vector <short int="int"> vA;
}

.#endif

Do tej pory wszystko działa.

Kiedy do pliku toDoForm.h (2 formatka) dodam:
.#include "globalVar.h"

mam taki efekt:
toDoForm.obj : error LNK2005: "class std::vector<short,class std::allocator<short> > Foo::vA" (?vA@Foo@@$$Q3V?$vector@FV?$allocator@F@std@@@std@@A) already defined in Task.obj
Visual Studio 2010\Projects\Task\Debug\Task.exe : fatal error LNK1169: one or more multiply defined symbols found

Kiedy w globalVar.h dodam extern:
extern vector <short int="int"> vA;

Mam:
Task.obj : error LNK2020: unresolved token (0A000047) "class std::vector<short,class std::allocator<short> > Foo::vA" (?vA@Foo@@$$Q3V?$vector@FV?$allocator@F@std@@@std@@A)
1>Task.obj : error LNK2001: unresolved external symbol "class std::vector<short,class std::allocator<short> > Foo::vA" (?vA@Foo@@$$Q3V?$vector@FV?$allocator@F@std@@@std@@A)
1>Visual Studio 2010\Projects\Task\Debug\Task.exe : fatal error LNK1120: 2 unresolved externals

Czy ktoś wie czemu tak się dzieje, googlowałem, ale nic nie rozwiązało moich problemów. Nadal nie wiem gdzie robię błąd.

0

Też kiedyś miałem taki problem niestety nie pamiętam jak go rozwiązałem.

0

przenieś #include <vector> do pliku stdafx.h i wyrzuć z obu form.

0

Przeniosłem #include <vector> i wyrzuciłem z globalVar.h oraz Form1.h

globalVar.h:

.#ifndef NAMESPACE_FOO
.#define NAMESPACE_FOO

using namespace std;

namespace Foo
{
extern vector <short int="int"> vA;
}

.#endif

Efekt:
1>\documents\visual studio 2010\projects\task\task\form1.h(1434): warning C4715: 'Task::userAndToDo' : not all control paths return a value
1>toDoForm.obj : error LNK2020: unresolved token (0A000023) "class std::vector<short,class std::allocator<short> > Foo::vA" (?vA@Foo@@$$Q3V?$vector@FV?$allocator@F@std@@@std@@A)
1>Task.obj : error LNK2020: unresolved token (0A000090) "class std::vector<short,class std::allocator<short> > Foo::vA" (?vA@Foo@@$$Q3V?$vector@FV?$allocator@F@std@@@std@@A)
1>Task.obj : error LNK2001: unresolved external symbol "class std::vector<short,class std::allocator<short> > Foo::vA" (?vA@Foo@@$$Q3V?$vector@FV?$allocator@F@std@@@std@@A)
1>toDoForm.obj : error LNK2001: unresolved external symbol "class std::vector<short,class std::allocator<short> > Foo::vA" (?vA@Foo@@$$Q3V?$vector@FV?$allocator@F@std@@@std@@A)
1>\Documents\Visual Studio 2010\Projects\Task\Debug\Task.exe : fatal error LNK1120: 3 unresolved externals

lub globalVar.h (bez extern):

.#ifndef NAMESPACE_FOO
.#define NAMESPACE_FOO

using namespace std;

namespace Foo
{
vector <short int="int"> vA;
}

.#endif

Efekt:

1>toDoForm.obj : error LNK2005: "class std::vector<short,class std::allocator<short> > Foo::vA" (?vA@Foo@@$$Q3V?$vector@FV?$allocator@F@std@@@std@@A) already defined in Task.obj
1>\Documents\Visual Studio 2010\Projects\Task\Debug\Task.exe : fatal error LNK1169: one or more multiply defined symbols found

Dodam jeszcze, że problem pojawia się kiedy #include globalVar.h pojawi się w 2-ugiej formatce. Jeśli ta dyrektywa zostanie umieszczona w zwykłym pliku xyz.h to nie ma problemu

np xyz.h:
.#include "globalVar.h"

class bar
{
public:
bar() { Foo::aV.push_back(0);}
};

0

przerzuć #include "globalVar.h" do stdafx.h.
<vector> jak się okazuje nie musisz, ale też warto.

Ale właściwie to nie wiem co jest dokładnie przyczyną.

1 użytkowników online, w tym zalogowanych: 0, gości: 1