0

This blog Cubic suggested trying four things and then more or less asking for help

  • Rebuild, check
  • check Runtime Libraries, only have one project
  • check entry point, check SUBSYSTEM:CONSULE
  • check for force included .lib files, I do not see #pragma comment(lib, ...)
  • something about turning on /VERBOSE in the linker options, I do not see the option

I would post some code but this is a LNK error ; it does not give much information.

  • LNK2005: "class std::vector,class std::allocator >,class std::allocator,class std::allocator > > >,class std::allocator,class std::allocator >,class std::allocator,class std::allocator > > > > > list1" (?list1@@3V?$vector@V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@V?$allocator@V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@@2@@std@@A) already defined in msproject.obj
  • LNK2005: "class std::vector,class std::allocator >,class std::allocator,class std::allocator > > >,class std::allocator,class std::allocator >,class std::allocator,class std::allocator > > > > > list1" (?list1@@3V?$vector@V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@V?$allocator@V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@@2@@std@@A) already defined in msproject.obj

I see that it is something about vector already defined in msproject - I made sure the vectors have different names. Is this something with the header # include <vector>; I tried commenting out some of the #includes to check, but same.

forest.peterson
  • 755
  • 2
  • 13
  • 30

2 Answers2

2

Seems like you're defining list1 more than once. (btw, the name suggests it should be a std::list, but that's beyond the scope of the question)

Do you have a

std::vector<std::string> list1;

in a header file? Is that header included in multiple translation units?

If you want a global, you need to use extern in the header:

extern std::vector<std::string> list1;

and move the definition in a single implementation file.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • @Luchian_Grigore for std:: I declared 'using namespace std;' Yes, vector> list1; is declared in the header. What do you mean by 'single implementation file' and what is a 'translation unit', I am looking these terms up? trying the extern. – forest.peterson Jul 25 '12 at 21:08
  • @forest.peterson ok, perfect. That's your problem. A translation unit is, simply put, a `cpp` file with all its includes. An implementation file is a `cpp` file. Basically you have to copy the definition in the header to a `cpp` file and in the header, declare the variable with `extern`. – Luchian Grigore Jul 25 '12 at 21:12
  • @Luchian_Grigore LNK2005 is gone but now the vector is undeclared outside the main.cpp; I declared the vector as list1 and list2 in the main.cpp and a class to resolve this issue. – forest.peterson Jul 25 '12 at 21:31
  • @forest.peterson did you put the `extern` followed by the declaration in a header? If you want to use the vector in a different file, just include the header with the declaration and it should work. – Luchian Grigore Jul 25 '12 at 21:34
  • @Luchian_Grigore If I declare the vector in the class file instead of the main then the LNK2005 is back. – forest.peterson Jul 25 '12 at 21:39
  • @forest.peterson did you declare it with `extern`? And also define it in the cpp file? – Luchian Grigore Jul 25 '12 at 21:39
  • @Luchian_Grigore in the class .h file I declared 'extern vector> list;' and then #include the class .h into main.cpp I use vector list in a function call 'lex.buildVectorFromFile("ASCII.csv", list, 200, 2);' And there are the same two LNK2005 errors again. – forest.peterson Jul 25 '12 at 21:41
  • @forest.peterson so in the `.h` you have `extern vector<...> list` and in `.cpp` you have `vector<...> list` right? That's not ok. Post a different question with this issue. – Luchian Grigore Jul 25 '12 at 21:42
  • @Luchian_Grigore, wait - I moved the vector, I declared it in the .h file above the class declaration. New errors C2071 - I can start a new question for this if necessary. The vector is declared once in .h and in main.cpp it is used in the function example I gave. Have we solved the question posed here? – forest.peterson Jul 25 '12 at 21:44
  • 1
    @forest.peterson the problem in this question is a multiple definition, which you solve by declaring the variable `extern` and defining it only once. `C2071` is a compiler error, which is different. – Luchian Grigore Jul 25 '12 at 21:45
1

You've defined list1 multiple times. Probably by putting it in a header file, which you've then included in multiple cpp files. This is how you should do it

// in header file
extern std:vector<whatever> list1;

// in one cpp file
std:vector<whatever> list1;

The gap in your knowledge is that you don't know/understand how to declare and define global variables in programs that have more than one source file. Any decent introductory book on C++ should cover this.

jahhaj
  • 3,021
  • 1
  • 15
  • 8
  • 2
    @Luchian Grigore Well apologies, you may find this hard to believe but your post was not visible when I started my post. Obviously I must be a slow typist. Down voting people you believe to have plagiarised you is a bit sad, this isn't competition. – jahhaj Jul 25 '12 at 21:20
  • Not going to start a debate. I explained my downvote because I want to encourage you to stay on SO (since you're a new user) & this is the summer of love. But duplicating answers isn't encouraged. Also, if you're *that* slow of a typist (5 rows in 10 minutes), you can simply delete this answer and your rep won't be affected by the downvote. After all, this isn't a competition, right? – Luchian Grigore Jul 25 '12 at 21:26
  • New in this incarnation, not new to SO overall. I don't see any way to delete my post. Maybe I don't have sufficient privileges yet. – jahhaj Jul 25 '12 at 21:29
  • I upvoted since while mostly duplicate, I got on the right track with the bit about declaring once in header with extern and once in cpp - turns out, it is once as extern in cpp A and once in cpp B. – forest.peterson Jul 25 '12 at 23:04
  • That works, but it's generally considered better style to put the extern declaration in a header file. Suppose you needed to use that variable in more than one cpp file, you would have to repeat the extern declaration. Even worse suppose you repeated the extern declaration but then you decided you needed to change it, you would have to make the same change in several places. Even worse suppose you got it wrong, and you ended up with multiple incompatible declarations of the same global variable. As programs get bigger issues like this become more of a concern. – jahhaj Jul 26 '12 at 05:51