Error importing .h

About Monkey 2 Forums Monkey 2 Programming Help Error importing .h

This topic contains 7 replies, has 2 voices, and was last updated by  abakobo 2 years, 2 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #7125

    Hezkore
    Participant

    I’ve created a test.h file with nothing but

    And If I try to import it whit this Monkey 2 code

    I get lots of errors.
    But if I remote

    It works fine!

    What am I doing wrong?

    #7135

    abakobo
    Participant

    That’s strange!
    Probably an mx2 duplicate def bug.
    Or we are always supposed to use .h for definition and .cpp for implementation when using mojo/gui or use static.
    With console apps I never had the problem.
    Tested with static before the function in .h and works ok
    Tested with def in .h and implementation in .cpp/.c and works ok

    If you want to see some little example I made to learn externs: https://github.com/abakobo/learn_monkey2
    It’s messy and not commented but simple. Will later use these as a base for tutos/samples

    #7136

    Hezkore
    Participant

    I can’t figure out a solution to this…
    I’m stuck at this first step right now. :/

    #7139

    abakobo
    Participant

    Your problem here is that you give the implementation ({}) of your function in the .h file. It is ok to do that in c/c++ rules but it is considered BAD practice and nearly nobody does it. So I suppose Mark has not made mx2 compatible with it and sometimes (when using mojo for example) create duplicates of the same header for some internal mx2 reasons.

    What’s GOOD practise?
    -put static before your function if the implementation is in the .h file (ie: the{}). This will skip duplicates.

    [/crayon]

    Or
    -put your implementation in a .c/.cpp file Edit:could not make it work with a .c file!
    .h

    [/crayon]

    .cpp

    [/crayon]

    and import the .c/.cpp file along with the .h file in your .monkey2 file

    don’t forget to put the ; at end of lines. Sometimes it’s not mandatory but it’ll avoid you error messages.. (in the second case if you don’t put ; in the .h file it will fail to compile)

    for better practice use the #ifndef in your .h file

    [/crayon]

    but here of course test is a bit too simple for a def name!

    #7140

    Hezkore
    Participant

    Thanks abakobo. I’m still a bit confused though (Cause I’m no good at C nor C++)
    How would I structure this code into its own .h and .cpp files then?

    #7141

    abakobo
    Participant

    try this without splitting (much easier and there’s something I didn’t get with the split with a .c file!)

    [/crayon]
    #7142

    Hezkore
    Participant

    Sadly I get the same error using that code.

    Am I doing something wrong?

    #7143

    abakobo
    Participant

    whenever a declaration create duplicates try to add static before it…

    in this case I added 3 static keywords and no errors now (on W7)

    this doesn’t mean it’s the good solution! but it avoids you spilltin to a .cpp src

    [/crayon]
Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.