Thread: syntax error when defining vectors

  1. #1
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21

    syntax error when defining vectors

    Folks,

    I'm in a scenario whereby I will often be inputing 10 elements but sometimes it might be more that 10 elements.

    So i decided to use vectors, so that if I needed to read in more than 10 elements, then the vector will grow automatically without too much work from myself.

    Now the element that I'm read in, is for a class called CHARACTER. So when defining the vector of my class CHARACTER, I wish to define it initially with as a vector of size 10. This is what I attempted below.

    Code:
    vector<CHARACTER>		char_list(10);
    However, I keep getting a syntax error as below:-

    c:\autochar\turn_data.h(29) : error C2059: syntax error : 'constant'
    main.cpp
    c:\autochar\turn_data.h(29) : error C2059: syntax error : 'constant'

    Any ideas of what I'm doing wrong ? (sorry for the basic question but I'm still very new to vectors and the other classes in the STL.

    Cheers
    Starkhorn

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What is: "CHARACTER"?

  3. #3
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21
    CHARACTER is a class that I've created.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You'll have to post more code than that.

  5. #5
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21
    Sorry, here's the code surrounding my CHARACTER class.

    Here is the character.h

    Code:
    #ifndef CHARACTER_H
    #define CHARACTER_H
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class CHARACTER
    {
    	public:
    	
    		CHARACTER(); //Default constructor
    
    		//Overloaded constructor with parameters
    		CHARACTER(string new_name, int new_location, int new_nation, int new_comm_rank, int new_age_rank, int new_mage_rank, int new_emi_rank, int new_doubled, bool new_dragon);
    
    		//Copy constructor
    		CHARACTER(const CHARACTER &character);
    
    		void set_char_name(string name);
    		//Sets the name af a character to the string szWord.
    
    		void set_char_loc(int location);
    		//Sets the location af a character to the int location.
    
    		void set_char_nation(int nation);
    		//Sets the nation af a character  to the int nation.
    
    		void set_char_double(int nation);
    		//Sets the nation af a character to the int nation.
    
    		void set_char_com(int comm_rank);
    		//Sets the comm_rank af a character to the passed-in int.
    
    		void set_char_age(int age_rank);
    		//Sets the age_rank af a character to the passed-in int.
    
    		void set_char_mage(int mage_rank);
    		//Sets the mage_rank af a character to the passed-in int.
    
    		void set_char_emi(int emi_rank);
    		//Sets the emi_rank af a character to the passed-in int.
    
    		void set_dragon(bool dragon);
    		//set to false or true whether the character is a dragon
    		
    
    	private:
    
    			string name;
    			int location, nation, comm_rank, agent_rank, mage_rank, emi_rank, doubled;
    			bool dragon;
    
    };
    
    #endif
    Here is the character.cpp

    Code:
    #include "character.h"
    
    // The default constructor
    CHARACTER::CHARACTER()
    {
    	set_char_name("");
    	set_char_loc(0);
    	set_char_nation(0);
    	set_char_double(0);
    	set_char_com(0);
    	set_char_age(0);
    	set_char_mage(0);
    	set_char_emi(0);
    	set_dragon(false);
    }
    
    CHARACTER::CHARACTER(string new_name, int new_location, int new_nation, int new_comm_rank, int new_age_rank, int new_mage_rank, int new_emi_rank, int new_doubled, bool new_dragon)
    {
    	set_char_name(new_name);
    	set_char_loc(new_location);
    	set_char_nation(new_nation);
    	set_char_double(new_doubled);
    	set_char_com(new_comm_rank);
    	set_char_age(new_age_rank);
    	set_char_mage(new_mage_rank);
    	set_char_emi(new_emi_rank);
    	set_dragon(new_dragon);
    }
    
    CHARACTER::CHARACTER(const CHARACTER &character)
    {
    	set_char_name(character.name);
    	set_char_loc(character.location);
    	set_char_nation(character.nation);
    	set_char_double(character.doubled);
    	set_char_com(character.comm_rank);
    	set_char_age(character.agent_rank);
    	set_char_mage(character.mage_rank);
    	set_char_emi(character.emi_rank);
    	set_dragon(character.dragon);
    }
    
    void CHARACTER::set_char_name(string new_name)
    //Sets the name af a character to the string szWord.
    {
    	name = new_name;
    
    }
    
    void CHARACTER::set_char_loc(int new_location)
    //Sets the location af a character to the int location.
    {
    	location = new_location;
    }
    
    void CHARACTER::set_char_nation(int new_nation)
    //Sets the nation af a character  to the int nation.
    {
    	nation = new_nation;
    }
    
    void CHARACTER::set_char_double(int new_doubled)
    //Sets the nation af a character to the int nation.
    {
    	doubled = new_doubled;
    }
    
    void CHARACTER::set_char_com(int new_comm_rank)
    //Sets the comm_rank af a character to the passed-in int.
    {
    	comm_rank = new_comm_rank;
    }
    
    void CHARACTER::set_char_age(int new_age_rank)
    //Sets the age_rank af a character to the passed-in int.
    {
    	agent_rank = new_age_rank;
    }
    
    void CHARACTER::set_char_mage(int new_mage_rank)
    //Sets the mage_rank af a character to the passed-in int.
    {
    	mage_rank = new_mage_rank;
    }
    
    void CHARACTER::set_char_emi(int new_emi_rank)
    //Sets the emi_rank af a character to the passed-in int.
    {
    	emi_rank = new_emi_rank;
    }
    
    void CHARACTER::set_dragon(bool new_dragon)
    //set to false or true whether the character is a dragon
    {
    	dragon = new_dragon;
    }

    As you can see nothing very complicated at all. Now what I want to do in my main.cpp is to create a vector of my character CLASS that initially has a size of 10.

    Here is my attempt which keeps throwing back the syntax error that I've specified in my first post. as you can see there is nothing really here....I'm simply trying to declare this vector at present, so that the vector has 10 instances of the CHARACTER class, all of which have been created using my default constructor.

    Sorry for not posting this earlier.

    Cheers
    Starkhorn

    Code:
    #include "character.h"
    
    int main(int argc, char *argv[])
    {
    
                 vector<CHARACTER>		char_list(10);	
                 return 1;
    }

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Everything compiles when I add
    #include <vector>

    Compiler: VS .NET 2003
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM