Thread: Error i don't understand :(

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    Error i don't understand :(

    dereferencing pointer to incomplete type.
    I get that when trying to compile my code.
    Code:
    struct matrix *newmat(int row, int column)
    {
    	...
    	struct matrix *mat;
    	mat->rows = row;                   (this and the following two lines give me the error)
            mat->columns = column;
            mat->elements = &anarray
    	...
    the definition for matrix is
    Code:
    struct matrix()
    {
         int rows;
         int columns;
         int *elements;
    }

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    struct matrix()
    {
    int rows;
    int columns;
    int *elements;
    }
    Did you add a semicolon after your struct ?
    Also, why does your struct have a ().

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Surely that's not your definition, is it? Definitions of a struct don't have parentheses in them.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    opps my bad should be
    Code:
    struct matrix
    {
    int rows;
    int columns;
    int *elements;
    };
    sorry that what i current have, come out with that error.

  5. #5
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    sorry that what i current have, come out with that error.
    What do you mean ? do you still have an error or is it fixed ?

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    I still have the error.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post a small self-contained example of what you are doing, and what compiler errors you get. Not just the structure, but a very small working program to look at.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    struct matrix *newmat(int row, int column)
    {
    ...
    struct matrix *mat;
    Well, I assume your trying to allocate memory dynamically and that is why you are getting the error.

    In that case you would have to do..
    struct matrix *mat = (struct matrix*) malloc(sizeof(struct matrix));
    Post some more code, so we know what your trying to do.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The problem is that even though you have defined "struct matrix," this definition is not visible at the point in the code where you get this error. Probably, you have forward-declared "struct matrix" but have failed to include the header file which actually defines the type.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by brewbuck View Post
    The problem is that even though you have defined "struct matrix," this definition is not visible at the point in the code where you get this error. Probably, you have forward-declared "struct matrix" but have failed to include the header file which actually defines the type.
    .... or the definition of struct matrix is after the code that gives an error before using it.

    The point of both brewbuck's and my comments is that the definition of a struct has to be seen by the compiler before code that uses it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by jadehippo View Post
    dereferencing pointer to incomplete type.
    I get that when trying to compile my code.
    Code:
    struct matrix *newmat(int row, int column)
    {
    	...
    	struct matrix *mat;
    	mat->rows = row;                   (this and the following two lines give me the error)
            mat->columns = column;
            mat->elements = &anarray
    	...
    the definition for matrix is
    Code:
    struct matrix()
    {
         int rows;
         int columns;
         int *elements;
    }
    What's "anarray" in your code?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I could not understand this question
    By enggabhinandan in forum C Programming
    Replies: 3
    Last Post: 10-22-2006, 05:17 AM
  2. Replies: 21
    Last Post: 10-14-2006, 04:38 AM
  3. Need software to help to understand C source code
    By Kincider in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2006, 09:44 PM
  4. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  5. Need help to understand x["AC"] in the following code
    By jcourcel in forum C Programming
    Replies: 11
    Last Post: 06-06-2006, 01:13 AM