Jump to content

  • Log In with Google      Sign In   
  • Create Account


vector compile error?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 markrodgers11   Members   -  Reputation: 149

Like
0Likes
Like

Posted 17 October 2012 - 11:17 AM

I'm trying to build my 2nd text based RPG but this time I wanna make it more flexible and have the code split up and everything. I will post my source code below and my compile error:

Source Code:

init.cpp
#include <iostream>
#include "game.h"
#include "rooms.h"
void main() {
intro();
}

game.cpp
#include <iostream>
#include <string>
#include <vector>
#include "rooms.h"
using namespace std;
void intro() {
pRooms[9] = 10;
checkRoom();
system("pause");
}

game.h
#include <string>
#include <vector>
using namespace std;
// Prototypes //
void intro();

rooms.cpp
#include <iostream>
#include <vector>
#include "rooms.h"
using namespace std;
int checkRoom() {
int cRoom = 0;
int counter = 0;
int x = 0;
if (counter <= nRooms) {
  if (pRooms[x] = 1) {
   x = cRoom;
   cout << "cRoom = " << cRoom;
  }
  else {
   ++counter;
   ++x;
   cout << "Adding 1\nAdding 1 to x";
  }
}
return cRoom;
}

rooms.h
#include <iostream>
#include <vector>
using namespace std;
// Prototypes //
int checkRoom();
// Vectors //
vector<int> pRooms(10,0);
// Integers //
int nRooms = pRooms.size();

Build Log

1>------ Build started: Project: textRPG, Configuration: Debug Win32 ------
1>  game.cpp
1>  Generating Code...
1>  Skipping... (no relevant changes detected)
1>  rooms.cpp
1>  init.cpp
1>init.obj : error LNK2005: "class std::vector<int,class std::allocator<int> > pRooms" (?pRooms@@3V?$vector@HV?$allocator@H@std@@@std@@A) already defined in game.obj
1>init.obj : error LNK2005: "int nRooms" (?nRooms@@3HA) already defined in game.obj
1>rooms.obj : error LNK2005: "class std::vector<int,class std::allocator<int> > pRooms" (?pRooms@@3V?$vector@HV?$allocator@H@std@@@std@@A) already defined in game.obj
1>rooms.obj : error LNK2005: "int nRooms" (?nRooms@@3HA) already defined in game.obj
1>H:\C++	extRPG\Debug	extRPG.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Edited by markrodgers11, 17 October 2012 - 11:20 AM.


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9539

Like
1Likes
Like

Posted 17 October 2012 - 11:23 AM

If you want to use a global variable in multiple source file. Declare them as extern in a header, and then define them in one source file. I.e.: put extern int i; in a header and in one source file put int i = /* blah blah blah */;

#3 markrodgers11   Members   -  Reputation: 149

Like
0Likes
Like

Posted 17 October 2012 - 11:25 AM

extern vector<int> pRooms(10,0);

is that possible? Posted Image

I tried that ^^ and it still has a compile error.

1>------ Build started: Project: textRPG, Configuration: Debug Win32 ------
1>  game.cpp
1>  Generating Code...
1>  Compiling...
1>  rooms.cpp
1>  init.cpp
1>  Generating Code...
1>init.obj : error LNK2005: "class std::vector<int,class std::allocator<int> > pRooms" (?pRooms@@3V?$vector@HV?$allocator@H@std@@@std@@A) already defined in game.obj
1>init.obj : error LNK2005: "int nRooms" (?nRooms@@3HA) already defined in game.obj
1>rooms.obj : error LNK2005: "class std::vector<int,class std::allocator<int> > pRooms" (?pRooms@@3V?$vector@HV?$allocator@H@std@@@std@@A) already defined in game.obj
1>rooms.obj : error LNK2005: "int nRooms" (?nRooms@@3HA) already defined in game.obj
1>H:\C++	extRPG\Debug	extRPG.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Edited by markrodgers11, 17 October 2012 - 11:25 AM.


#4 SiCrane   Moderators   -  Reputation: 9539

Like
3Likes
Like

Posted 17 October 2012 - 11:27 AM

No, don't put the initializer as part of the extern declaration in the header. Put the initializers on the definitions in the source file.

#5 Daniel_RT   Members   -  Reputation: 478

Like
0Likes
Like

Posted 17 October 2012 - 05:19 PM

#pragma once

Put that at the top of every header file, this prevents things being included more that once during compilation.

#6 Brother Bob   Moderators   -  Reputation: 8009

Like
0Likes
Like

Posted 17 October 2012 - 05:39 PM

#pragma once

Put that at the top of every header file, this prevents things being included more that once during compilation.

Neither that, nor include guards, prevent multiple definition errors.

#7 Trienco   Crossbones+   -  Reputation: 2114

Like
0Likes
Like

Posted 17 October 2012 - 10:24 PM

Time to point out the important difference between declaration and definition. You can have as many declarations in your source files as you like, but only ONE definition.

void function();
Is a declaration. It just says "this thingy exists... somewhere".

void function() {...}
Is a definition, because it defines what the function actually is and does.

For variables it's slightly different.

int x;
int x = 10;
Both are definitions, though the first variable isn't initialized (and if I find code like that at work without a really good reason, the author will be verbally slapped).

extern int x;
Is a pure declaration, saying that somewhere this variable exists.

extern int x = 10;
Doesn't make sense, because you now try to declare it AND already define it.


For classes it's a bit more interesting. You generally implement (define) the member functions in a single source file, so everything is fine. Why does it work if you implement them right there in the header (inside the class)? Because it's automatically considered inline. Inline will "fix" your multiple definitions (for functions), but unless you actually want it to be inlined, that kind of thing also deserves a good slapping (especially on very huge and bloated functions, which you shouldn't be coding anyway).

There is also the most important declaration of all. The forward declaration.

class SomeClass;
Again, this simply declares "this class exists somewhere". Get in the habit of using this in your header files instead of including whole header files for other classes. As long as your header is only containing pointers or references to that class, nobody cares what it looks like and by using forward declarations, you reduce messy include avalanches and often reduce compile time. Basically: only use #include in files that actually use the class itself.

Edited by Trienco, 17 October 2012 - 10:26 PM.

f@dzhttp://festini.device-zero.de

#8 L. Spiro   Crossbones+   -  Reputation: 13164

Like
0Likes
Like

Posted 18 October 2012 - 12:07 AM

Frankly this is one of the reasons why I never have global variables in this form.
It isn’t even simple to explain to him how to fix this how it is.

I use one class per file and as a result this form of a global never exists in my projects.
Instead, they become class static members (read carefully; they are still globals, just in a different form/syntax).

There are several reasons I do this, but the main point here is that it eliminates this headache.

File.h:
class CMyClass {
public :
	static vector<int> pRooms;
};

File.cpp:
vector<int> CMyClass::pRooms = vector<int>( 10, 0 ); // Will this constructor actually work?

The headache of multiple-definitions and proper use extern is gone.


L. Spiro

Edited by L. Spiro, 18 October 2012 - 12:08 AM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS