Thread: Problem with vectors and classes

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    1

    Problem with vectors and classes

    Hi guys. I'm having a problem with getting a vector to fill with my location class here is the code:
    Code:
    #include<iostream>
    #include<vector>
    using namespace std;
    class location
    {
        public:
        int x,y;
        static int id;
        char fill;
        location(int x2, int y2, char a)
        {
    	x  = x2;
    	y = y2;
    	fill = a;
    	id++;
        }
    };
    class board
    {
        public:
        board()
        {
    	vector<location>board(361, new location(0,0,'+'));
        }
    };
    void makeBoard()
    {
        
    }
    int main(int argc, char **argv)
    {
        makeBoard();
        return 0;
    }
    Could someone tell me how to fix this? I know the program doesn't really do anything but I get some compiling errors.

    axel

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you post compiler errors we can help you much more easily.
    Code:
    vector<location>board(361, new location(0,0,'+'));
    Perhaps you need a space there?
    Code:
    vector<location> board(361, new location(0,0,'+'));
    [edit]
    And you don't need new there.
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    id++;
    You can't do that, given id is a static variable and that statement is in a non-static function.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    BTW, you can call your constructor arguments the same as the class variables, and access the class variables using the this pointer:
    Code:
    class location
    {
        public:
        int x,y;
        static int id;
        char fill;
        location(int x2, int y2, char a)
        {
    	x  = x2;
    	y = y2;
    	fill = a;
    	id++;
        }
    };
    ->
    Code:
    class location
    {
        public:
        int x,y;
        static int id;
        char fill;
        location(int x, int y, char fill)
        {
    	this->x  = x;
    	this->y = y;
    	this->fill = fill;
    	id++;
        }
    };
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by dwks
    Code:
    id++;
    You can't do that, given id is a static variable and that statement is in a non-static function.
    That's incorrect:
    Code:
    class Location
    {
    public:
    	static int id;
    
    	int x,y;
    	char fill;
    
    	Location(int x2, int y2, char a)  //non-static function
    	{
    		x  = x2;
    		y = y2;
    		fill = a;
    		id++;  //static member
    	}
    };
    
    int Location::id = 1;
    
    int main()
    {
    	Location myL(10, 20, 'x');
    	cout<<myL.id<<endl;
    
    	return 0;
    }
    Last edited by 7stud; 04-08-2006 at 01:08 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> vector<location>board(361, new location(0,0,'+'));

    You don't want the new there. You may be confused by Java (or not). Since the vector holds location objects, you just need to pass a location object for it to use to initialize its elements.

    The problem you will have is that id will be incorrect because you only increment it in the regular constructor but not the copy constructor (which is compiler generated in this case). When you create three instances in the vector with that code, all three will be copy constructed and so id will not get incremented. The solution depends on what you really want to do with id.

    Also note that your class is called board, so you don't want the vector variable to be called board as well.

  7. #7
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    vector<location>board(361, new location(0,0,'+'));
    Also, i don't see this variable declared in the class declaration so this is nothing but a local variable there i presume, which in this case is doing nothing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of vectors containing classes
    By larne in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2009, 07:19 AM
  2. classes and vectors
    By izuael in forum C++ Programming
    Replies: 10
    Last Post: 11-27-2006, 04:19 PM
  3. Vectors and custom classes
    By cunnus88 in forum C++ Programming
    Replies: 16
    Last Post: 05-12-2006, 05:11 AM
  4. vectors and classes
    By jimothygu in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 07:53 PM
  5. How To use vectors for custom classes
    By johnnyd in forum C++ Programming
    Replies: 14
    Last Post: 03-25-2003, 10:04 PM