• Advertisement
Sign in to follow this  

Question about passing struct's in c++

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by meatbeef
Take 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 this post


Link to post
Share on other sites
Quote:
Original post by Name_Unknown
Quote:
Original post by meatbeef
Take 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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement