Thread: Function call problem

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Function call problem

    Hi there,

    Having difficulty with how to construct a function.

    I'm storing pointers to my objects in a vector.

    In the function I'm trying to pass a reference as I've learned that this is more efficient.

    But since I'm storing pointers and not the actual objects I'm confused on how create the function.

    I've prepared some code as an example of where I'm at:

    base.h :
    Code:
    using namespace std;
    #include <string>
    
    class Base
    {
    public:
    
    	Base(string mainname);
    
    private:
    
    protected:
    
    	string m_Name;
    
    };
    
    Base::Base(string mainname)
    {
    	m_Name = mainname;
    }
    
    
    void adddoublequotes(Base& obj)//PROBLEM FUNCTION
    {
    	Base::m_Name = '\"' + Base::m_Name + '\"';//adds double quotes to the datamember
    
    }
    main.cpp :
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include "base.h"
    
    using namespace std;
    
    int main ()
    {
    	vector<Base *>vectorname;
    
    	string mainname;
    	cout << "Enter the name " << endl;
    	getline (cin, mainname);
    
    	Base *ptr = new Base(mainname);
    
    	adddoublequotes(&ptr);//PROBLEM FUNCTION
    
    	vectorname.push_back( ptr );
    
    	system("pause");
    	return 0;
    }
    I hope there is sufficient code there to explain my problem.

    Visual Studio reports:

    error C2597: illegal reference to non-static member 'Base::m_Name'
    error C3867: 'Base::m_Name': function call missing argument list; use '&Base::m_Name' to create a pointer to member
    error C2568: '+' : unable to resolve function overload
    If anyone can help I'd really appreciate it.

    Thanks very much!
    Last edited by Swerve; 11-17-2009 at 06:28 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    To get a reference from a pointer you need to dereference the pointer. Use * to dereference a pointer, not &.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    void adddoublequotes(Base& obj)//PROBLEM FUNCTION
    {
    	Base::m_Name = '\"' + Base::m_Name + '\"';//adds double quotes to the datamember
    
    }
    The compilers telling you that the class "Base" has no static member/field called "m_Name"--which is correct. In your code above, you probably want to refer to the given instance's field "m_Name". So you probably want to do something like this "obj->m_Name" instead of trying to access a field that belongs to the class that doesnt exist "Base::m_Name".

    Another problem is that "adddoublequotes" does not have access to the protected variable "m_Name", even if you fixed the previous error.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks chaps!

    Here is what I've done, is it as it should be?

    base.h :

    Code:
    using namespace std;
    #include <string>
    
    class Base
    {
    public:
    
    	Base(string mainname);
    
    private:
    
    protected:
    
    	string m_Name;
    
    };
    
    Base::Base(string mainname)
    {
    	m_Name = mainname;
    }
    
    void adddoublequotes(Base& ptr)//PROBLEM FUNCTION
    {
    	//Base->m_Name = '\"' + Base->m_Name + '\"';//adds double quotes to the datamember
    	cout << "Test cout" << endl;
    }
    main.cpp :

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include "base.h"
    
    using namespace std;
    
    int main ()
    {
    	vector<Base *>vectorname;
    
    	string mainname;
    	cout << "Enter the name " << endl;
    	getline (cin, mainname);
    
    	Base *ptr = new Base(mainname);
    
    	adddoublequotes(*ptr);//PROBLEM FUNCTION
    
    	vectorname.push_back( ptr );
    
    	system("pause");
    	return 0;
    }
    Is this correct?

    The compilers telling you that the class "Base" has no static member/field called "m_Name"--which is correct. In your code above, you probably want to refer to the given instance's field "m_Name". So you probably want to do something like this "obj->m_Name" instead of trying to access a field that belongs to the class that doesnt exist "Base::m_Name".

    Another problem is that "adddoublequotes" does not have access to the protected variable "m_Name", even if you fixed the previous error.
    Thanks for posting this, subject to the code above being correct, yes, that is the problem I'm having now.

    This is what I've come up with:

    base.h :

    Code:
    using namespace std;
    #include <string>
    
    class Base
    {
    public:
    
    	Base(string mainname);
    	void adddoublequotes(Base& ptr);
    private:
    
    protected:
    
    	string m_Name;
    
    };
    
    Base::Base(string mainname)
    {
    	m_Name = mainname;
    }
    
    void Base::adddoublequotes(Base& ptr)
    {
    	m_Name = '\"' + m_Name + '\"';   //adds double quotes to the datamember
    }
    main.cpp :

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include "diary.h"
    
    using namespace std;
    
    int main ()
    {
    	vector<Base *>vectorname;
    
    	string mainname;
    	cout << "Enter the name " << endl;
    	getline (cin, mainname);
    
    	Base *ptr = new Base(mainname);
    
    	ptr->adddoublequotes(*ptr);   //PROBLEM FUNCTION
    
    	vectorname.push_back( ptr );
    
    	system("pause");
    	return 0;
    }
    I would most welcome any feedback on what I've done. I seems to work OK, but I may of screwed up somewhere.

    Appreciate the help!
    Last edited by Swerve; 11-17-2009 at 07:57 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I would make adddoublequotes take a string instead of a Base object. Then I would call it on mainname before creating the object inside main.

    It's hard to tell with this contrived example, but I'd imagine adding double quotes doesn't have anything to do with the Base class logically.

    If it does, then you could also make adddoublequotes a member function, and possibly even call it from the constructor instead of requiring that it be called in main.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Your absolutely right.

    On my actual program, I am saving the objects to a .csv file, and when I was adding the double quotes in the constructor, when I saved the objects, and then re-loaded them again, the double quotes were again being added, so each value ended up like ""value"".

    This is why I'm trying to remove the adddoublequotes function from the constructor and make it separate .



    EDIT - An yep, I'm using strings in the function parameters to.

    Thanks Daved, your always helping me out!
    Last edited by Swerve; 11-17-2009 at 08:11 PM.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    ptr->adddoublequotes(*ptr)
    You probably dont need to pass any argument, as the only information that the function ("adddoublequotes") needs is already associated with its instance ("ptr"). You could just do "ptr->adddoublequotes()", and this function is already modifying that instance's "m_Name" variable, so the function doesnt need to be changed--of course besides removing the argument.

    And as mentioned, the current structure is somewhat confusing because its not very intuitive. A few options:
    - as mentioned, add quotes to the string with some function that has nothing to do with a "Base", and then call the constructor on that string
    - change the "Base" constructor to automatically add the double quotes on the given string--you could make it "smart" enough to check if the first and last characters are double quotes then you dont need to add more double quotes
    - remove the argument from the "adddoublequotes" function as I mentioned above

    The first option seems to make the most sense. The other options add more complexity to the class, when it might not be necessary for that class to even know or care at all about double quotes. Of course, if your class is some CSV thing, then it might actually make sense to wrap everything in that class as you are doing.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks nadroj!

    I've attempted using the function both with and without parameters on my actual program, and it compiles fine and runs, but for some reason it makes no change the datamembers, they remain without the quotes, so I think I'll go with your first recommendation.

    Appreciate your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. how to get function call stack
    By George2 in forum C Programming
    Replies: 18
    Last Post: 11-11-2006, 07:51 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM