-
March 24th, 2011, 10:52 AM
#1
Problem using a function from other file
hello Every 1
visual studio 2010.............
c++
i have a problem , i need to use a function named getentityposition which is in some file for example named AgentActioncomponent ,when i want to call this function i am available with AgentActioncomponent and even getentityposition...i call it in this way
******************************************************************
Vec3f currentPos = AgentActionComponent::getEntityPosition(m_characterID2);
******************************************************************
but it doesnt recognize AgentActionComponent .. when i take my cursor to AgentActionComponent it says non static member reference
must be relative to a specific object ....
does any body have any idea
Regards Imran Habib
-
March 24th, 2011, 11:00 AM
#2
Re: Problem using a function from other file
does any body have any idea
Exactly what the error says. The function you are calling is a non-static function, so you can only use this function when it's part of a already created object.
Code:
AgentActionComponent myObject;
Vec3f currentPos = myObject.getEntityPosition (m_characterID2);
-
March 24th, 2011, 11:47 AM
#3
Re: Problem using a function from other file
Originally Posted by Skizmo
Exactly what the error says. The function you are calling is a non-static function, so you can only use this function when it's part of a already created object.
Code:
AgentActionComponent myObject;
Vec3f currentPos = myObject.getEntityPosition (m_characterID2);
1. Well ! i have tried the way u told me but when i m creating object it doesnt recognize object
To make my question more clear .........
2. the AgentActionComponent is a headerfile which has another .cpp file with the same name
the header file contains declaration about function getentityposition and .cpp file contains
definition ....
i don't know but i guess there is some connection missing some where....
Kindly help
Regards Imran Habib
-
March 24th, 2011, 12:06 PM
#4
Re: Problem using a function from other file
Originally Posted by imranhabib1
1. Well ! i have tried the way u told me but when i m creating object it doesnt recognize object
To make my question more clear .........
2. the AgentActionComponent is a headerfile which has another .cpp file with the same name
the header file contains declaration about function getentityposition and .cpp file contains
definition ....
i don't know but i guess there is some connection missing some where....
Kindly help
Regards Imran Habib
Your question is clear, your understanding of the problem isn't.
You're trying to call a non-static member function of a class. To do that, you need an instantiation of the class - an object. You're trying to call the function as if it were static and it's clearly not defined that way. It's not a header/cpp issue, it's a not calling the function properly issue.
Can you post the code where you tried to create an object?
-
March 24th, 2011, 12:59 PM
#5
Re: Problem using a function from other file
Originally Posted by GCDEF
Your question is clear, your understanding of the problem isn't.
You're trying to call a non-static member function of a class. To do that, you need an instantiation of the class - an object. You're trying to call the function as if it were static and it's clearly not defined that way. It's not a header/cpp issue, it's a not calling the function properly issue.
Can you post the code where you tried to create an object?
here we go
*****************************************
AgentActionComponent Obj;
Obj.getEntityPosition(m_characterID2);
*****************************************
it says no default constructor .............. on taking cursor to Obj .. i tried delcaring default constructor
but it doesnt work yet ...........
any suggestions
-
March 24th, 2011, 01:09 PM
#6
Re: Problem using a function from other file
Originally Posted by imranhabib1
here we go
*****************************************
AgentActionComponent Obj;
Obj.getEntityPosition(m_characterID2);
*****************************************
it says no default constructor .............. on taking cursor to Obj .. i tried delcaring default constructor
but it doesnt work yet ...........
any suggestions
Stop telling us you tried stuff and it didn't work without showing the code.
The error messages mean something. Obviously you need to create a default constructor. Show us what you did and we'll tell you why it didn't work.
-
March 26th, 2011, 10:16 AM
#7
Re: Problem using a function from other file
Originally Posted by GCDEF
Stop telling us you tried stuff and it didn't work without showing the code.
The error messages mean something. Obviously you need to create a default constructor. Show us what you did and we'll tell you why it didn't work.
AgentActioncomponent* Obj;
Obj->getentityposition;
now i dun have error but i have another problem in it and it is linking problem....
actually as far as i know if we need to access a member function in from header file we need to
1. add header .h as an header in .cpp
2. we have to call constructor of the class , (the same class whose member function we are calling) in our .cpp
3. adding refernce to .h file
I did 1 and 3 .. but i am confused about doing 2 ... because it has no default constructor I mean constructor with out parameters and its other constructor eg AgentActioncomponent(GameEntity* ABC)
can be used or not in my .cpp
Lastly :::: I CREATED my default constructor in that header file but i dun get when i call that default constructor ....
if u can help plz kindly let me know , how should i solve this prob
Directions will b appreciated
Regards Imran Habib
-
March 26th, 2011, 10:36 AM
#8
Re: Problem using a function from other file
Originally Posted by imranhabib1
2. we have to call constructor of the class , (the same class whose member function we are calling) in our .cpp
This is not true.
3. adding refernce to .h file
What do you mean by "reference to .h file"?
I did 1 and 3 .. but i am confused about doing 2 ... because it has no default constructor I mean constructor with out parameters and its other constructor eg AgentActioncomponent(GameEntity* ABC)
can be used or not in my .cpp
None of what you stated makes sense.
Here is an example of code where 2) is not necessary:
Code:
#include <string>
size_t foo(std::string& s)
{
return s.length();
}
Where do you see a default constructor being called for s? That code will compile just fine. As for 3) in your description, I have no idea what you're referring to.
Regards,
Paul McKenzie
-
March 26th, 2011, 02:21 PM
#9
Re: Problem using a function from other file
Originally Posted by imranhabib1;2005702
AgentActioncomponent* Obj;
Obj->getentityposition;
Obj is an unitialized pointer.
Obj->getentityposition is trying to dereference an unitialized pointer which will almost always cause your program to crash.
When you call a function, you need to put parens after the function name, and include applicable arguments between the parens.
There's been a rash of people here lately trying to program by guessing. That will never work. Get a good book and work through it.
-
March 26th, 2011, 02:23 PM
#10
Re: Problem using a function from other file
Originally Posted by imranhabib1
if u can help plz kindly let me know , how should i solve this prob
Directions will b appreciated
Regards Imran Habib
We speak in complete sentences using real words on this board.
-
March 26th, 2011, 02:52 PM
#11
Re: Problem using a function from other file
Originally Posted by GCDEF
There's been a rash of people here lately trying to program by guessing. That will never work.
I guess that they believe C++ can be learned by "feel" and maybe a cheat sheet listing some commands.
Regards,
Paul McKenzie
-
March 26th, 2011, 02:53 PM
#12
Re: Problem using a function from other file
Originally Posted by imranhabib1
AgentActioncomponent* Obj;
Obj->getentityposition;
now i dun have error but i have another problem in it and it is linking
problem....
Well, actually you managed to cheat the compiler this way, and you don't have a compilation error now, but you still do have error, and linking problem is the smallest part of it. Even if you manage somehow to link that, your code is 100% going to crash at runtime because of uninitialized pointer.
actually as far as i know if we need to access a member function in from header file we need to
1. add header .h as an header in .cpp
2. we have to call constructor of the class , (the same class whose member function we are calling) in our .cpp
3. adding refernce to .h file
I did 1 and 3 .. but i am confused about doing 2 ... because it has no default constructor I mean constructor with out parameters and its other constructor eg AgentActioncomponent(GameEntity* ABC)
can be used or not in my .cpp
Lastly :::: I CREATED my default constructor in that header file but i dun get when i call that default constructor ....
if u can help plz kindly let me know , how should i solve this prob
Sorry, no intention to offend anyhow. It's obvious you do not understand pointers, constructors, and something makes me believe your do-not-understand list does not end with those. And the best solution to your problem would be to go back and start learning C++ from the very beginning, get some fundamental reading on C++ and do not skip doing exercises disregarding how boring those would seem to you.
Last edited by Igor Vartanov; March 26th, 2011 at 03:01 PM.
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|