Thread: Vector Trouble - Dev-C++

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    26

    Vector Trouble - Dev-C++

    I've been working on my text adventure, and in storing all the rooms I've come across a problem. How to store them. I want to keep them all in a vector, so they can be accessed easily, but when I do, I get an error.

    Code:
    #ifndef ROOM_LIST //Prevents repeat definitions.
    #define ROOM_LIST
    
    #include "Room.h"
    #include <vector>
    
    const unsigned short int roomCount=2;
    std::vector<Room> roomList;
    
    Room test1("coc_testhall", "You are standing in a large, open room.  Doors line every wall, with barely a foot between them.");
    roomList.push_back(test1);
    
    Room test2("coc_armorroom", "You are standing in a square room, lined with tables; each holding multiple suits of similar armor.");
    roomList.push_back(test2);
    
    #endif
    The error given is:
    Quote Originally Posted by Dev-CPP Error
    14 C:\Documents and Settings\Timothy Sassone\Text Adventure\source.cpp In file included from source.cpp
    11 C:\Documents and Settings\Timothy Sassone\Text Adventure\roomListing.h expected constructor, destructor, or type conversion before '.' token
    11 C:\Documents and Settings\Timothy Sassone\Text Adventure\roomListing.h expected `,' or `;' before '.' token
    14 C:\Documents and Settings\Timothy Sassone\Text Adventure\roomListing.h expected constructor, destructor, or type conversion before '.' token
    14 C:\Documents and Settings\Timothy Sassone\Text Adventure\roomListing.h expected `,' or `;' before '.' token
    C:\Documents and Settings\Timothy Sassone\Text Adventure\Makefile.win [Build Error] [source.o] Error 1
    Thank you for your time,
    Timothy

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like the code should be in a function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    26
    Quote Originally Posted by laserlight
    It looks like the code should be in a function.
    What part of it?

    The part designated in the errors are the lines 11 and 14, the ones using the push_back function.

    From what I can tell, I am doing it exactly as shown here.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by timmeh View Post
    What part of it?

    The part designated in the errors are the lines 11 and 14, the ones using the push_back function.

    From what I can tell, I am doing it exactly as shown here.
    No you're not. You're doing it outside of any function. Why do you need those to be global variables?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    26
    Quote Originally Posted by cpjust View Post
    No you're not. You're doing it outside of any function. Why do you need those to be global variables?
    Oh! I understand now. Sorry about that laserlight, I completely missed what you were saying...

    I moved it into the main loop and it works perfectly. Thanks, both of you!

    Just for future reference, is there an example/tutorial I could look at that covers why/what I can'/can't use outside functions?

    Thanks!

    Tim

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You can initialize objects in the global namespace to things that boil down to compile time constants.

    For example if Foo initializes to 0, ok. If Foo does RAII, no.

    Nothing else occurs before main().
    Last edited by whiteflags; 02-16-2009 at 02:14 AM.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    26
    Cool, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Whats better, C-Free or Dev C++??
    By i_can_do_this in forum Tech Board
    Replies: 10
    Last Post: 07-07-2006, 04:34 AM
  2. New to Dev C++/<windows.h>...
    By Cilius in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2005, 01:05 AM
  3. Glut and Dev C++, Programs not Quitting?
    By Zeusbwr in forum Game Programming
    Replies: 13
    Last Post: 11-29-2004, 08:43 PM
  4. DEV C++ Limitations?
    By Kirdra in forum Game Programming
    Replies: 3
    Last Post: 09-09-2002, 09:40 PM
  5. Tutorial about Bloodshed Dev!
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2001, 07:42 PM