Thread: Unable to compare string with 'getter' returned string.

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

    Unable to compare string with 'getter' returned string.

    Hi,

    I am trying to add an option for a user to enter a name into my program, and if that name exists, then the program should delete the object which it matches.

    I'm using a vector, which holds base class pointers.

    So I'm entering the name entered by the user into a string variable, and then looping through the vector to search for a match, but my 'getter' function is not able to compare it's return with the string variable it seems.

    Here is my program:

    header file (diary.h) :
    Code:
    using namespace std;
    #include <string>
    
    class Diary
    {
    public:
    
    	Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails);
    
    	//Getters (Getters are used to access private or protected datamembers) :
    	string getName() const { return m_Name; }
    	string getAddress() const { return m_Address; }
    	string getPostCode() const { return m_PostCode; }
    	string getTelNo() const { return m_TelNo; }
    	string getDetails() const { return m_Details; }
    
    	//Pure Virtual getter functions for Leads class
    	virtual string getDate() = 0;
    	virtual string getTime() = 0;
    
    	//Virtul function for adding double quotes to strings for the .csv file:
    	virtual void addDoubleQuotes(string& m_Name,string& m_Address,string& m_PostCode,string& m_TelNo,string& m_Details)
    	{
    		m_Name = '\"' + m_Name + '\"';
    		m_Address = '\"' + m_Address + '\"';
    		m_PostCode = '\"' + m_PostCode + '\"';
    		m_TelNo = '\"' + m_TelNo + '\"';
    		m_Details = '\"' + m_Details + '\"';
    	}
    
    private:
    
    protected:
    
    	string m_Name;
    	string m_Address;
    	string m_PostCode;
    	string m_TelNo;
    	string m_Details;
    };
    
    Diary::Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails)
    {
    	m_Name = mainname;
    	m_Address = mainaddress;
    	m_PostCode = mainpostcode;
    	m_TelNo = maintelno;
    	m_Details = maindetails;
    
    	addDoubleQuotes(m_Name, m_Address, m_PostCode, m_TelNo, m_Details);
    }
    
    //----------------------------------------------------------------
    
    class Leads : public Diary
    {
    public:
    	Leads(string m_Name, string m_Address, string m_PostCode, string m_TelNo, string m_Details, string m_Date, string m_Time);
    
    	//Getters:
    	string getTime() { return m_Time; }
    	string getDate() { return m_Date; }
    
    	//Virtual function for adding double quotes to strings for the .csv file:
    	void addDoubleQuotes(string& m_Date, string& m_Time)
    	{
    		m_Date = '\"' + m_Date + '\"';
    		m_Time = '\"' + m_Time + '\"';
    	}
    
    private:
    
    protected:
    
    	string m_Date;
    	string m_Time;
    };
    
    Leads::Leads(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails, string maindate, string maintime) : Diary(mainname, mainaddress, mainpostcode, maintelno, maindetails)
    {
    	m_Date = maindate;
    	m_Time = maintime;
    
    	addDoubleQuotes(m_Date, m_Time);
    }
    main.cpp :
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <fstream>
    #include "diary.h"
    
    using namespace std;
    
    int main ()
    {
    	vector<Diary *>vectorname;
    
    	do{
    		int choice = 0;
    		cout << "Choose one of the options below:" << endl;
    		cout << "1) Enter a lead" << endl;
    		cout << "2) Delete a lead" << endl;
    		cout << "3) Exit the program" << endl;
    		cin >> choice;
    		cin.ignore();
    
    		switch(choice)
    		{
    		case 1:
    			{
    				cout << "You chose to enter a lead" << endl;
    				//---------------------
    				//Collect the leads details
    				//---------------------
    
    				string mainname;
    				cout << "Name? " << endl;
    				getline (cin, mainname);
    
    				string mainaddress;
    				cout << "Address? " << endl;
    				getline (cin, mainaddress);
    
    				string mainpostcode;
    				cout << "Post Code? " << endl;
    				getline (cin, mainpostcode);
    
    				string maintelno;
    				cout << "Telephone Number? " << endl;
    				getline (cin, maintelno);
    
    				string maindetails;
    				cout << "Details? " << endl;
    				getline (cin, maindetails);
    
    				string maindate;
    				cout << "Date ? DDMMYY " << endl;
    				getline (cin, maindate);
    
    				string maintime;
    				cout << "Time? 24hr HHMM " << endl;
    				getline (cin, maintime);
    
    				cout << "All questions asked" << endl;
    
    				try
    				{
    					Leads *ptr = new Leads(mainname, mainaddress, mainpostcode, maintelno, maindetails, maindate, maintime);
    					vectorname.push_back( ptr );
    				}
    				catch (bad_alloc& ba)
    				{
    					cerr << "bad_alloc caught: " << ba.what() << endl;
    				}
    
    				break;
    			}
    
    		case 2:
    
    			{
    				cout << "You chose to delete a lead" << endl;
    				//---------------------
    				//Delete a 'Lead'
    				//---------------------
    
    				string leadToDelete;
    				cout << "Enter the name of the lead to delete" << endl;
    				getline (cin, leadToDelete);
    
    				for(unsigned int a = 0; a<vectorname.size(); a++)
    				{
    					if(vectorname[a]->getName == leadToDelete)
    					{
    						cout <<"Found the lead you want to delete" << endl;
    					}
    				}
    				cout << "The lead has been deleted" << endl;
    			}
    
    		case 3:
    
    			{
    				cout << "You chose to exit the program" << endl;
    
    				ofstream streamname("database.csv");
    				if(!streamname)
    				{
    					cout << "Cannot open file" << endl;
    					return 1;
    				}
    
    				for(unsigned int a = 0; a<vectorname.size(); a++)
    				{
    					streamname << vectorname[a]->getName() << "," << vectorname[a]->getAddress() << "," << vectorname[a]->getPostCode() << "," << vectorname[a]->getTelNo() << "," << vectorname[a]->getDetails() << "," << vectorname[a]->getDate() << "," << vectorname[a]->getTime() << endl;
    				}
    
    				for(unsigned int a = 0; a<vectorname.size(); a++)
    				{
    					delete vectorname[a];
    				}
    
    				streamname.close();
    
    				exit(0);
    
    				break;
    			}
    		}
    	}
    	while(true);
    
    	system ("PAUSE");
    
    	return 0;
    }
    The compiler (Visual Studio 2005) gives the following error:

    error C2784: 'bool std:perator ==(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'overloaded-function'
    I thought this would work, as the 'getter' returns a string, but I guess not.

    If anyone could help explain my problem in a simple to understand way I would really appreciate it.

    Many thanks!

    Swerve

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Functions need () after their name.
    Code:
    if(vectorname[a]->getName() == leadToDelete)
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    embarrassment.jpg

    THANK YOU cpjust.

    I feel a total muppet now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. String Compare Using Dynamic Arrays
    By djwicks in forum C Programming
    Replies: 4
    Last Post: 03-31-2005, 08:01 PM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM