Discussion:
How to call this method inside a global function?
(too old to reply)
Jack
2009-10-09 08:38:43 UTC
Permalink
Dear everybody,
class CMesh
{
HRESULT LoadMesh(...);
};

cInternalParser::Parse(...)
{
CMesh::LoadMesh(...);
}

How can I call CMesh::LoadMesh from a global function? The code above is
reported with an error
"Can't call non-static member function" or something...
And then I tried to make the class member static, and the compiler still
complains. I am sorry I can't remember what the whole message says because
I have changed the methods to global to cheat.
If I want to wrap them back into a class, what should I do?
Thanks
Jack
Jack
2009-10-09 08:42:28 UTC
Permalink
Sorry, wrong speech...
I should have said "from" other classes
as the post indicates...
Thanks
Jack
Guido Franzke
2009-10-09 09:01:44 UTC
Permalink
Hello Jack,
you must have a variable and call your function:

CMesh theMesh;
theMesh.LoadMesh(...); // now the Load function loads data into theMesh

With only the small code snippet you give us, I woould say, you should not
make LoadMesh static.
Ok, if you want to do, do this:

class CMesh
{
static HRESULT LoadMesh(...);
}
Then you can call CMesh::LoadMesh the way you do in Parse().

Regards,
Guido
Post by Jack
Dear everybody,
class CMesh
{
HRESULT LoadMesh(...);
};
cInternalParser::Parse(...)
{
CMesh::LoadMesh(...);
}
How can I call CMesh::LoadMesh from a global function? The code above is
reported with an error
"Can't call non-static member function" or something...
And then I tried to make the class member static, and the compiler still
complains. I am sorry I can't remember what the whole message says because
I have changed the methods to global to cheat.
If I want to wrap them back into a class, what should I do?
Thanks
Jack
Ulrich Eckhardt
2009-10-09 10:09:57 UTC
Permalink
Post by Jack
class CMesh
{
HRESULT LoadMesh(...);
};
cInternalParser::Parse(...)
{
CMesh::LoadMesh(...);
}
How can I call CMesh::LoadMesh from a global function? The code above is
reported with an error "Can't call non-static member function" or
something...
Right, in order to call a memberfunction, you need an object. Take a look at
the C++ FAQ, it explains the difference between a (free) function and a
memberfunction.
Post by Jack
And then I tried to make the class member static, and the compiler still
complains.
Well, a class-static function is a normal function but in the scope of a
class. As a normal function, it doesn't need an object but also doesn't
have access to the object's non-static members. I guess that's the error
you get.
Post by Jack
I am sorry I can't remember what the whole message says because
I have changed the methods to global to cheat.
Use a version control system, that way you could easily turn back the
changes. I'd suggest Subversion.
Post by Jack
If I want to wrap them back into a class, what should I do?
Don't force things into classes that don't belong there. OOP is not a goal,
it is a tool to develop software. Sometimes it is not the right tool.

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Ben Voigt [C++ MVP]
2009-10-09 16:31:24 UTC
Permalink
Post by Jack
Dear everybody,
class CMesh
{
HRESULT LoadMesh(...);
};
cInternalParser::Parse(...)
{
CMesh::LoadMesh(...);
}
How can I call CMesh::LoadMesh from a global function? The code above is
reported with an error
"Can't call non-static member function" or something...
And then I tried to make the class member static, and the compiler still
complains. I am sorry I can't remember what the whole message says because
I have changed the methods to global to cheat.
If I want to wrap them back into a class, what should I do?
If your purpose of wrapping is to keep related things together, not allow
there to be more than one copy, then I'd suggest a namespace. It keeps the
rest of your program uncluttered, but it still functions much like globals.
Post by Jack
Thanks
Jack
Loading...