views:

311

answers:

1

This one has been driving me mad! I have a struct:

typedef struct{
  int a;
}myStruct;

Then I have:

myStruct tempStruct;

I am trying to pass the struct to a class method whose implementation is:

- (void) myFunc:(struct myStruct)oneOfMyStructs{};

I call the method like so:

[myClass myFunc:(struct myStruct)tempStruct];

The compiler complains about "Conversion to non-scalar type requested." All I want to do is pass a struct into a class method, but the syntax has me a bit confused. I'm new to Objective-C. Please let me know if you can see where I'm going wrong. I've not been able to pass by reference either, so if you could help me out with that, that would be great!

Thanks!

+3  A: 

Since you have typedef-ed it to myStruct, you don't need to (indeed mustn't) include the struct keyword in the function signature or the call:

typedef struct tag_myStruct
{
    int a;
}
myStruct;

- (void) myFunc:(myStruct) x {}

[myClass myFunc:tmpStruct];

should be sufficient. (Compare all those Cocoa functions that take things like NSRects.)

walkytalky
Thanks for the tip. I've formatted the code as you suggested but the compiler complains when I call the function: "Incompatible type for argument 1 of 'myFunc'"I'm reluctant to post the whole thing because it involves OpenGL and is made up of half a dozen files. I just can't help thinking I'm doing something fundamentally wrong here...Should it matter that the call to the function comes from within a function inside another class? I don't see why it should as I am trying to pass by value.Thanks again.
Striker
Without the actual code it's hard to say. Calling from one class to another should be fine provided the type definition is visible in both locations. It doesn't sound like you're trying to do something fundamentally wrong, just that some detail isn't matching up. Check that the method declaration in the interface matches the implementation, that the right headers are being included, etc.
walkytalky