static variables and includes?

This is a discussion on static variables and includes? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Devils Child i included engine into utility and utility into engine. thats what you told me. and ...

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Devils Child View Post
    i included engine into utility and utility into engine.
    thats what you told me. and it doesnt work either
    I didn't mean that you should actually do that.

    Try removing the static keyword.

  2. #17
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    ok.
    http://www.dev-ch.de/upload/files/De...lGL2_Demo2.zip (831.67 KB)
    here is the complete source code. you might not be able to compile and run it, but the principe is clear.

    the variables that are static are declared in matrix.cpp and want to be used in virtualGL.cpp
    i hope you understand the code as it is, it is very long you know...

    edit: removing the static keyword does not make a difference
    Last edited by Devils Child; 01-29-2008 at 01:43 PM.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want to use those variables in another source code file, you must not declare them static in matrix.cpp. Declare them normally in matrix.cpp, and use the "extern" keyword in virtualGL.cpp.

    Having said that, you really really really really really really want to make that matrix a structure, and maybe turn some other things into structures and classes as well. Doing so will (1) make all your computations much easier, and (2) allow you to have more than one matrix at any given time, and (3) make writing code much easier, since you can use somewhat-more-normal syntax. (For instance, look at vgliTriangleFill: instead of fifteen input parameters, you could pass in 3 Point objects, or better yet a vector/array of Points, and then all those nearly-identical lines of code could be done with a for-loop, and all those swaps could just be swap_Points, etc.)

  4. #19
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    i was thinking of putting it into a class later, but while we are at this right now: i tried once to build up a texture class, but that gave EXACTLY the same problem: i had problems with the declarations, so:
    1. (how) do i have to declare a class in the header file?
    2. when this is done, i should be able to access this class from _every_ cpp file that is included there.

    /* the thing with the triangle fill routine: hm i think it's a lot easyer and a bit of my style since in c++ you can wrap lines between tokens this is giving me the oppertunity to do more than 20 parameters to a function LOL */

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You'll need to put the class definition in an included header file, yes.

    Objects are just variables, but of a fancier type (instead of int i, you have Point i); so the same scoping rules apply: static variables are only visible in the file in which they are declared; extern variables are defined elsewhere and used in this file, etc.

  6. #21
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    yeh, i know how to use classes but i dont know how to declare them in the header files *shame*
    and when i write extern instead of static i get the same error

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    I don't see where the global variable is used nor defined.
    Can you explain where?

    Also, I suggest you avoid this:
    Code:
    void swap_int(int* x, int* y) {
    	int temp;
    	temp = *(int*)x;
    	*(int*)x = *(int*)y;
    	*(int*)y = temp;
    } void swap_int64(__int64* x, __int64* y) {
    Do it the right way:
    Code:
    void swap_int(int* x, int* y) {
    	int temp;
    	temp = *(int*)x;
    	*(int*)x = *(int*)y;
    	*(int*)y = temp;
    }
    
    void swap_int64(__int64* x, __int64* y) {
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Perhaps a stupid (as in does-nothing), yet illustrative, example is in order:

    point.h:
    Code:
    class Point {
        double x, y, z;
    }
    point1.cpp:
    Code:
    #include "point.h"
    Point a, b, c; //global variables -- bad practice, but we're illustrating
    static Point d, e, f; //complete opposite of global variables -- can never be seen in point2, no matter how hard you try
    point2.cpp:
    Code:
    #include "point.h"
    extern Point a, b, c; //same variables as in point1, through the magic of linking
    // if we don't link the object file point1.o, we're in trouble

  9. #24
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    tabstop: your explanation works! i just have to declare them not static in the main file and extern in all other files. also it is a lil bit messy to declare all those values as extern in the other files, but well...

    (elysia: i do that messy thingy only because these swap functions only differs by their datatypes)

    so now the only question left is, how to do the same thing wth classes.
    i will declare a class in the virtualGL.cpp and want to use it in other files, too,...

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Devils Child View Post
    tabstop: your explanation works! i just have to declare them not static in the main file and extern in all other files. also it is a lil bit messy to declare all those values as extern in the other files, but well...
    As messy as static?

    And if you have a lot of functions that differ only in their prototypes, then why don't you use templates?

    so now the only question left is, how to do the same thing wth classes.
    i will declare a class in the virtualGL.cpp and want to use it in other files, too,...
    Um ... that was a class? I don't know what you mean by "declare a class"; if you mean write out code or whatever, you need to have the class definition (the bit that that's in the header file here, plus function prototypes) in a header file, no matter what anyway. You can then write out the actual functions in a .cpp file.

  11. #26
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Code:
    void swap_int(int* x, int* y) {
    	int temp;
    	temp = *(int*)x;
    	*(int*)x = *(int*)y;
    	*(int*)y = temp;
    }
    What's with the casts?

    And why not just use what's already there?
    Code:
    using std::swap;
    swap(x, y);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #27
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    hm that swap command really exists :P
    i replaced my one with that one
    now to the classes: all i need to do is write the class declaration including its' functions into the header file and all other files are able to use that class? (cause that ain't working ..)

  13. #28
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    No, you define the class in the header and implement the functions in a .cpp file (unless it's a template function or template class).
    But this still probably leaves the problem that you're going to make global objects. I can just hope you won't do that.
    Then include the header and it will magically work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #29
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    well, it magically gives me a stack overflow
    Code:
    class vglTex {
    private:
    public:
    	int w, h, buf[513][513];
    	/*void LoadFromFile(char path[]) {
    		SDL_Surface* image = SDL_LoadBMP(path);
    		w = (*image).w;
    		h = (*image).h;
    		for (int x = 0; x <= w; x++) {
    			for (int y = 0; y <= h; y++) {
    				buf[x][y] = GetPixel(image, x, y);
    			}
    		}
    		SDL_FreeSurface(image);
    	}*/
    };

  15. #30
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    'Cause 512 x 513 x 4 = 1 052 676 (~1 MB). Stack overflow.
    Allocate buffer on heap.
    Sheesh.

    Code:
    w = (*image).w;
    This is not necessary.
    Code:
    w = image->w;
    Will do just fine.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes as member variables
    By Stonehambey in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2008, 07:01 PM
  2. Includes making me insane >.<
    By IceDane in forum C Programming
    Replies: 14
    Last Post: 04-14-2008, 10:24 AM
  3. global variables
    By shadovv in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2005, 02:21 PM
  4. MingW & ProperySheet
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 06-27-2002, 07:34 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

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