Skip to main content
GameDev.net gamedev.net
🔒 Locked

Function that Returns Custom Object

Started by samgj Apr 15, 2013 at 10:10 PM 12 replies 2k views
Original Post
samgj
samgj

I created a function that returns a custom object. When I compile it I get an error. Is there a way to get the same effect out of this without an error?

Jesse Dager
Jesse Dager

Also, make sure that the function is of your custom type:


CustomType* myfunction()
{
     //do stuff
     return value;
}

//instead of

void myfunction()
{
     //do stuff
     return value;
}
I develop to expand the universe. "Live long and code strong!" - Delta_Echo (dream.in.code)
0r0d
0r0d

I created a function that returns a custom object. When I compile it I get an error. Is there a way to get the same effect out of this without an error?

Yes there is.

Khatharr
Khatharr

An error without an error? That's zen, isn't it?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
SiCrane
SiCrane
When asking about an error, it's also useful to post the error message. In this case I would guess that the problem is that you didn't put a semi-colon at the end of your class definition.
rip-off
rip-off

I believe the precendence of the dereference operator and the member access (dot) operator won't allow this line:


*objecttype.init();

You can fix the precendence using parentheses:


(*objecttype).init();

But then again this is the reason the pointer member access (arrow) operator exists:


objecttype->init();

Finally, you might observe that this function is leaking memory. While observing this, another though occurs, dynamic allocation is in fact unnecessary! You could write:


 
mlnmobject create_instance(/* ... */)
{
    mlnmobject result;
    result.init();
    return *result;
}
 

You may also want to move the function definitions out of a header file if you want to link correctly when you start including them from multiple sources files.

As a side note, the type names you are using are very difficult to understand. Instead of class_loc_rot, consider calling it a "vector" (sometimes people use vec3 for short, to differentiate it between 2D vectors and std::vector). mlnmobject could be called GameObject, possibly placed in a namespace mlnm if necessary.

rip-off
rip-off

The errors that SiCrane and I mentioned are rather elementary C++ errors. If you cannot solve them yet, I would recommend you step back from trying to integrate Physfs and Python into a complex game for the time being. Instead, start with a simpler program, for example make some console programs until you are thoroughly familiar with the basic language.

Servant of the Lord
Servant of the Lord

When asking about an error, it's also useful to post the error message. In this case I would guess that the problem is that you didn't put a semi-colon at the end of your class definition.


Nice catch!

@samgj: To clarify, in C++, every class definition must end with a semicolon.

Example:


class MyClass
{
     //...
 
} ; <----- Semicolon must go here


Both your classes are missing a semicolon at the end, and that's a common syntax error that needs to be fixed. If, after fixing that, you still have a problem, you need to post your actual error message.
Error messages are the compiler's way of telling you what (and where) the problem is.


Another problem is your member variables are all private. Class member-variables are private by default (unlike structs which are public by default), so you must go like this:


class MyClass
{
     public: //Everything that follows this is publicly accessible to the rest of your code.
     
     int thisIsPublic;
     
     private: //Everything that follows this is private, and not accessible to the rest of your code (except for MyClass's functions).
 
     int thisIsPrivate;
};

So, for example, when you call objecttype's init() inside of create_instance(), that'd be an error, because init() is private inside of mlnmobject by default.

Further, don't use 'new'. Just, for now, ignore that 'new' even exists. It's something you'll learn later, but right now you're misusing in ways that will hurt more than it'll help.

Just go like this:


mlnmobject create_instance(mlnmobject objecttype, /* <--- You accidentally left a comma here, btw. That's an error. */)
{
    mlnmobject objecttype;
    objecttype.init();
 
    return objecttype;
}
SiCrane
SiCrane

@samgj: To clarify, in C++, every class definition must end with a semicolon.

Actually, the problem is that you don't have to put a semicolon there. If you had to, compilers could give you much more useful error messages about the problem. It's just that the other options aren't very useful in general, so you almost always see a semicolon there.
Servant of the Lord
Servant of the Lord

Actually, the problem is that you don't have to put a semicolon there. If you had to, compilers could give you much more useful error messages about the problem. It's just that the other options aren't very useful in general, so you almost always see a semicolon there.

Are you referring to:
class MyClass
{

} A, B;
Or something else?
SiCrane
SiCrane
That's one option. You can also define pointers, arrays, references, arrays of pointers, etc.
samgj
samgj

I added semi-colons to the classes and removed new from create_instance. There are still errors but they are for unrelated problems and I understand how to fix them.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.