1

I'm working on a function that works as part of a larger program. My C pointer skills are a bit rusty and so I need some help here.

Notice: I've omitted a lot of the program because it is irrelevant and works correctly, all you need to know is that I import a header file that declares the struct ImportedStruct, also, I cannot change the function header, so I need to pass the ImportedStruct as a pointer and the function as a pointer.

I get the following error message in VS code: pointer to incomplete class type is not allowed.

Any ideas?

#include "name.h"
long int *functionName(struct ImportedStruct *structName, int *irrelevant){
    int width = structName->width;
    int height = structName->height;

    // Remaining function ...
}

Header file:

struct ImportedStruct 
{ 
  int width, height;
};
9
  • 1
    Are you sure you included the header? Also, ImportedStruct -> structName. Commented Feb 23, 2020 at 19:02
  • My apologies, @Neil I had a typo. I just edited the code, thanks for the heads up! Commented Feb 23, 2020 at 19:05
  • Also, I'm sure I imported the header correctly, as otherwise I would have had other errors indicating that "ImportedStruct" is not a valid type. But it is in this case, the only problem is that it's not working when I use pointers. Commented Feb 23, 2020 at 19:09
  • That should work, now. Commented Feb 23, 2020 at 19:13
  • 1
    @Iyad: You actually wouldn't get an error that it's not a valid type. You'd get the error that you got, that the type is not defined. The compiler knows that struct ImportedStruct is a type, it has the keyword struct in front of it. Commented Feb 23, 2020 at 19:16

1 Answer 1

3

I just compiled the code you provide

struct ImportedStruct 
{ 
  int width, height;
};

long int *functionName(struct ImportedStruct *structName, int *irrelevant){
    int width = structName->width;
    int height = structName->height;

    // Remaining function ...
    return (long int *)(width + height);
}

and it compiled successfully.

Make sure struct definition is included. Also, you can put

struct ImportedStruct 
{ 
  int width, height;
};

right before your function definition. If compiler does not give you multiple definition error, then struct definition is not included by headers.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi Tarek, thank you for your answer. I've just edited my code segment. Can you take a look again?
Hi @lyad. Edited the answer. Please check if helps.
Hi Tarek, thank you so much for your help. You're right, when I compile it with your edits it does work properly. The problem is, I'm importing that struct from another header file. Which I believe I've imported properly (include "name.h") at the top. Do you have any idea what else it could be? Because I need to import the struct and not re-declare it.
Just track the headers you include that they actually include the header with ImportedStruct definition. It also could be that definition is protected by preprocessor defines.
The existence of the word 'class' in the original error messages suggests to me that you tried to compile it as C++
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.