Public Group

Question about passing struct's in c++

This topic is 4769 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm having a problem with passing structs located in one file to a function in a different file. I've tried using pointers and just passying the structure (ex. int something(struct test test1) or int something(struct test *test1). Well, the pointer one does work when compiling, but my problem comes up when trying to access the variables, I access them as you normally would ( -> )but no matter what I get "invalid use of undefined type const struct test'. With the second example i get 'variable has incomplete type'. I'm not sure what I'm doing wrong since if it's all in one file, it works fine either way. I dunno if it matters but I am using Dev C++. I appreciate any help givin :) Thanks! EsperKnight

Share on other sites
Put the struct definition in a header and make sure both .C files include it.

Share on other sites
EDIT: Ok never mind ignore this I'm on drugs :/

Share on other sites
Quote:
 Original post by meatbeefTake out the 'struct' keyword the compiler wants the type followed by the variable name so it should be something along the lines of void myfunc(myType *varname){ varname->member = x;}

That shouldn't be a problem with C++, as struct is a biproduct of C style and most compilers should be ok with it. It might be compiler dependent, MSVC 7.1 has no problems with that convention.

I think he doesn't have a proper definition of the struct in both files.

Share on other sites
Quote:
Original post by Name_Unknown
Quote:
 Original post by meatbeefTake out the 'struct' keyword the compiler wants the type followed by the variable name so it should be something along the lines of void myfunc(myType *varname){ varname->member = x;}`

That shouldn't be a problem with C++, as struct is a biproduct of C style and most compilers should be ok with it. It might be compiler dependent, MSVC 7.1 has no problems with that convention.

I think he doesn't have a proper definition of the struct in both files.

Yeh I noticed that. I edited my post just after you posted thanks for the heads up though

Share on other sites
0_0 I appreciate the advice name_unkown and meatbeef! I never say on any webpages you had to include the struct def in the header file of the function you're passing too. I really appreciate the help! Another question, if i was going to use extern struct, is this done in the same way? (include the def and all). Thanks!

EsperKnight

1. 1
2. 2
JoeJ
18
3. 3
4. 4
frob
12
5. 5

• 13
• 16
• 13
• 20
• 13
• Forum Statistics

• Total Topics
632188
• Total Posts
3004650

×