Thread: Function call problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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.

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