Problem using a function from other file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Problem using a function from other file

  1. #1
    Join Date
    Feb 2011
    Location
    sweden
    Posts
    16

    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

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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);

  3. #3
    Join Date
    Feb 2011
    Location
    sweden
    Posts
    16

    Re: Problem using a function from other file

    Quote Originally Posted by Skizmo View Post
    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

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,205

    Re: Problem using a function from other file

    Quote Originally Posted by imranhabib1 View Post
    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?

  5. #5
    Join Date
    Feb 2011
    Location
    sweden
    Posts
    16

    Re: Problem using a function from other file

    Quote Originally Posted by GCDEF View Post
    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

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,205

    Re: Problem using a function from other file

    Quote Originally Posted by imranhabib1 View Post
    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.

  7. #7
    Join Date
    Feb 2011
    Location
    sweden
    Posts
    16

    Re: Problem using a function from other file

    Quote Originally Posted by GCDEF View Post
    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

  8. #8
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Problem using a function from other file

    Quote Originally Posted by imranhabib1 View Post
    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

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,205

    Re: Problem using a function from other file

    Quote 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.

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,205

    Re: Problem using a function from other file

    Quote Originally Posted by imranhabib1 View Post
    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.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Problem using a function from other file

    Quote Originally Posted by GCDEF View Post
    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

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,051

    Re: Problem using a function from other file

    Quote Originally Posted by imranhabib1 View Post
    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&#37; 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 04: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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center