class vars checked b4 object needs them?

This is a discussion on class vars checked b4 object needs them? within the C++ Programming forums, part of the General Programming Boards category; Take this code for example Code: #define upleft buffer[a-1][b-1] #define upcent buffer[a-1][b] #define upright buffer[a-1][b+1] #define left buffer[a][b-1] #define right ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    class vars checked b4 object needs them?

    Take this code for example
    Code:
    #define upleft  buffer[a-1][b-1]
    #define upcent  buffer[a-1][b]
    #define upright buffer[a-1][b+1]
    #define left    buffer[a][b-1]
    #define right   buffer[a][b+1]
    #define loleft  buffer[a+1][b-1]
    #define locent  buffer[a+1][b]
    #define loright buffer[a+1][b+1]
    
    class intertia
    {
    public:
    	double a,b;
    	intertia() {a=b=0;}
    	void calculate()
    	{
    		a++;
    		if(upleft==1) {b-=.3;}
    		if(upright==1) {b+=.3;}
    		if(left==1) {b-=.3;}
    		if(right==1) {b+=.3;}
    		if(loleft==1) {b-=.3;}
    		if(loright==1) {b+=.3;}
    	}
    };
    
    int main()
    {
    	int buffer[70][70];
    	memset(buffer,0,sizeof(buffer));
    	intertia inert[70][70];
    
    //...
    The compiler complains that it doesn't know the identifier 'buffer'. Yet, the function that calls 'buffer' is not even called. Why would it complain if it doesn't need to use it yet? Buffer is declared by the time any object is constructed...

    I suppose the answer to my question is that yes, the class checks to see if it knows variables before any object is even constructed. But could anyone reassure me on that fact? Why is it implemented this way, making the code above have errors?

    Thanks a lot for the help

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,530
    Well
    if(upleft==1)

    is no different from
    if(buffer[a-1][b-1]==1)

    Simply look at what you've written to see that there is no buffer in scope at that point.

    Also
    1. Having a class rummage in external variables like that is a complete waste of the class concept.
    2. Don't use floats to index arrays.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Inventory tracking of dynamically allocated items
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 07-23-2006, 05:39 PM
  4. Replies: 3
    Last Post: 12-03-2001, 12:45 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21