string class errors

This is a discussion on string class errors within the C++ Programming forums, part of the General Programming Boards category; I'm getting errors for using string's '==' operator and for displaying one with cout. Is doing that wrong, or do ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891

    string class errors

    I'm getting errors for using string's '==' operator and for displaying one with cout. Is doing that wrong, or do these error mean something else?

    error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<ch
    ar> >' (or there is no acceptable conversion)

    error C2784: 'bool __cdecl std::operator ==(const class std::allocator<_Ty> &,const class std::allocator<_U> &)' : could not deduce template argument for 'const class std
    ::allocator<_Ty> &' from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'

    error C2784: 'bool __cdecl std::operator ==(const class std::istreambuf_iterator<_E,_Tr> &,const class std::istreambuf_iterator<_E,_Tr> &)' : could not deduce template ar
    gument for 'const class std::istreambuf_iterator<_E,_Tr> &' from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'

    error C2784: 'bool __cdecl std::operator ==(const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not
    deduce template argument for 'const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &' from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'

    error C2784: 'bool __cdecl std::operator ==(const struct std::pair<_T1,_T2> &,const struct std::pair<_T1,_T2> &)' : could not deduce template argument for 'const struct s
    td::pair<_T1,_T2> &' from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'

    error C2676: binary '==' : 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator or a conversion to a ty
    pe acceptable to the predefined operator
    Error executing cl.exe.

    Balance.exe - 6 error(s), 0 warning(s)

    for '==' I'm comparing two strings and for cout I have a string in the out stream.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code please.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    string GetName()
    string search

    Code:
    if(search == Accounts[x].GetName())
    			{
    				AmountFound++;
    				ReturnValue[AmountFound] = Accounts[x].GetIDNumber();
    			}
    Code:
    cout << MyAccount.GetName() << endl;
    *edit* You want complete code, do ya? Sure...
    Last edited by CodeMonkey; 07-17-2003 at 08:08 PM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Code:
    #include "classes.h"
    
    char* CutToTwo(char* ThyNumber)
    {
    	int ReturnValue = 0;
    	bool looping = true;
    	unsigned int x = 0;
    	
    	while(looping)
    	{
    		if(ThyNumber[x] == '.')
    		{
    			ThyNumber[x + 3] = '\0';
    			looping = false;
    			break;
    		}
    		else if(ThyNumber[x] == '\0')
    		{
    			looping = false;
    			break;
    			ReturnValue = -1;
    		}
    		else
    		{
    			
    		}
    		x++;
    	}
    
    	return ThyNumber;
    }
    
    ACCNT::ACCNT()
    {
    	labeled = false;
    	balance = 0.00;
    	name = new char[100];
    }
    
    ACCNT::~ACCNT()
    {
    	//delete []name;
    }
    
    int ACCNT::GetAccountNum()
    {
    	return AccountNum;
    }
    
    
    double ACCNT::deposit(double amount, char *Reason, bool UsedInCreation)
    {
    	double TempBalance = balance;
    
    	if(amount == -1)
    		balance = balance * 2;
    	else if(amount > 0)
    		TempBalance = TempBalance + amount;
    
    	if(TempBalance > 0 && TempBalance > balance)
    			balance = TempBalance;
    
    	else
    		return -1;
    
    	return balance;
    }
    
    double ACCNT::withdraw(double amount, const char *Reason, bool UsedInCreation)
    {
    	double TempBalance = balance;
    		
    	if(amount == -1)
    		balance = 0.00;
    	else if(amount != -1 && amount < 0)
    		return 0;
    	else if(amount > 0)
    		TempBalance = TempBalance - amount;
    
    	if(TempBalance < 0)
    		return -2;
    	else
    	{
    		balance = TempBalance;
    	}
    		
    	return balance;
    }
    
    int ACCNT::operator =(ACCNT other)
    {
    	balance = other.GetBalance();
    	label(other.GetName());
    	SetIDNumber(other.GetIDNumber());
    	SetAccountNum(other.GetAccountNum());
    	
    	return 1;
    }
    
    int ACCNT::label(string InputName)
    {
    	name = InputName;
    	if(!labeled)
    		labeled = true;
    
    	return 0;
    }
    
    int ACCNT::SetIDNumber(int Number)
    {
    	IDNumber = Number;
    	return 0;
    }
    
    int ACCNT::SetAccountNum(int Number)
    {
    	AccountNum = Number;
    	return 0;
    }
    
    string ACCNT::GetName()
    {
    	return name;
    }
    
    double ACCNT::GetBalance()
    {
    	return balance;
    }
    
    int ACCNT::GetIDNumber()
    {
    	return IDNumber;
    }
    
    BANK::BANK()
    {
    	AccountPointer = 0;
    
    	full = false;
    	
    	for(int x = 0; x < MAX_NUM_ACCOUNTS; x++)
    	{
    		AccountsMap[x] = 0;
    	}
    
    }
    
    BANK::~BANK()
    {
    	//delete []name;
    	//delete []AccountsMap;
    }
    
    int BANK::label(string InputName)
    {
    	name = InputName;
    	
    	return 0;
    }
    
    int BANK::ReadInAccounts(const char* FileName)
    {
    	SCRIPT_POSITION read_state = ACC_NUM;
    	char buffer[55];
    	ifstream Reader(FileName);
    	bool active = true;
    	ACCNT CurrentAccount;
    
    	if(Reader.fail())
    		return -1;
    
    	while(active)
    	{
    		switch(read_state)
    		{
    		case ACC_NUM:
    			{
    				Reader.getline(buffer,25);
    				if(Reader.eof())
    				{
    					active = false;
    					break;
    				}
    
    				CurrentAccount = CreateAccount(atoi(buffer),NULL,NULL);
    				read_state = NAME;
    			}break;
    		case NAME:
    			{
    				Reader.getline(buffer,50);
    				if(Reader.eof())
    				{
    					active = false;
    					break;
    				}
    				CurrentAccount.label(buffer);
    				//At this point, CurrentAccount has the correct name value... it goes
    				//wrong elsewhere... DEBUGGING
    				SaveAccount(CurrentAccount);
    				read_state = MONEY;
    			}break;
    		case MONEY:
    			{
    				Reader.getline(buffer,25);
    				if(Reader.eof())
    				{
    					active = false;
    					break;
    				}
    				CurrentAccount.deposit(atof(buffer),"Creation",true);
    				SaveAccount(CurrentAccount);
    				read_state = ACC_NUM;
    			}break;
    		default:
    			{
    			}break;
    		}
    	}
    
    
    	return 0;
    }
    
    ACCNT BANK::CreateAccount(int AccountNum,const double StartingBalance,string AccountName)
    {
    	int CreatedID;
    	ACCNT account;
    
    	if(full)
    	{
    		account.label("FULL");
    		account.deposit(77.77,"FULL",true);
    		account.SetIDNumber(0);
    		return account;
    	}
    	account.label(AccountName);
    	account.deposit(StartingBalance,"Creation",true);
    	account.SetIDNumber(AccountPointer);
    	account.SetAccountNum(AccountNum);
    
    	Accounts[AccountPointer] = account;
    	AccountsMap[AccountPointer] = 1;
    	CreatedID = AccountPointer;
    
    	UpdateAccountPointer();
    
    	return Accounts[CreatedID];
    }
    
    int BANK::DeleteAccount(int ID)
    {
    	if(ID < 51 && ID > -1)
    	{
    		Accounts[ID].label("*VOID*");
    		AccountsMap[ID] = 0;
    		UpdateAccountPointer();
    		return 0;
    	}
    	else
    		return -1;
    
    	return -1;
    }
    
    string BANK::GetName()
    {
    	return name;
    }
    
    array <int> BANK::SearchByBalance(COMPARISON param, double SearchBalance)
    {
    	array <int> ReturnValue(MAX_NUM_ACCOUNTS);
    	int AmountFound = 0;
    
    	for(int x = 0; x < MAX_NUM_ACCOUNTS; x++)
    	{
    		if(AccountsMap[x] == 1)
    		{
    			switch(param)
    			{
    				case EQUAL_TO:
    				{
    					if(Accounts[x].GetBalance() == SearchBalance)
    					{
    						AmountFound++;
    						ReturnValue[AmountFound] = Accounts[x].GetIDNumber();
    						ReturnValue[0] = AmountFound;
    					}
    				}break;
    				case LESS_THAN:
    				{
    					if(Accounts[x].GetBalance() < SearchBalance)
    					{
    						AmountFound++;
    						ReturnValue[AmountFound] = Accounts[x].GetIDNumber();
    						ReturnValue[0] = AmountFound;
    					}
    				}break;
    				case GREATER_THAN:
    				{
    					if(Accounts[x].GetBalance() > SearchBalance)
    					{
    						AmountFound++;
    						ReturnValue[AmountFound] = Accounts[x].GetIDNumber();
    						ReturnValue[0] = AmountFound;
    					}
    				}break;
    				case LESS_THAN_OR_EQUAL_TO:
    				{
    					if(Accounts[x].GetBalance() <= SearchBalance)
    					{
    						AmountFound++;
    						ReturnValue[AmountFound] = Accounts[x].GetIDNumber();
    						ReturnValue[0] = AmountFound;
    					}
    				}break;
    				case GREATER_THAN_OR_EQUAL_TO:
    				{
    					if(Accounts[x].GetBalance() >= SearchBalance)
    					{
    						AmountFound++;
    						ReturnValue[AmountFound] = Accounts[x].GetIDNumber();
    						ReturnValue[0] = AmountFound;
    					}
    				}break;
    			}
    		}
    		else
    		{
    		}
    	}
    			
    	return ReturnValue;
    }
    
    bool BANK::isfull()
    {
    	return full;
    }
    
    array <int> BANK::SearchByAcctNum(int SearchParam)
    {
    	array <int> ReturnValue(MAX_NUM_ACCOUNTS);
    	int AmountFound = 0;
    	bool GetAll = false;
    
    	if(SearchParam == ALL)
    	{
    		GetAll = true;
    	}
    
    	for(int x = 0; x < MAX_NUM_ACCOUNTS; x++)
    	{
    		if(AccountsMap[x] == 1)
    		{
    			if(GetAll)
    			{
    				AmountFound++;
    				ReturnValue[AmountFound] = Accounts[x].GetIDNumber();
    			}
    			else if(Accounts[x].GetIDNumber() == SearchParam)
    			{
    				AmountFound++;
    				ReturnValue[AmountFound] = Accounts[x].GetIDNumber();
    			}
    			ReturnValue[0] = AmountFound;
    		}
    		else
    		{
    		}
    	}
    
    	return ReturnValue;
    }
    
    array <int> BANK::SearchByName(string search)
    {
    	array <int> ReturnValue(MAX_NUM_ACCOUNTS);
    	int AmountFound = 0;
    
    	for(int x = 0; x < MAX_NUM_ACCOUNTS; x++)
    	{
    		if(AccountsMap[x] == 1)
    		{
    			//!strcmpi(Accounts[x].GetNmae(),search)
    			if(search == Accounts[x].GetName())
    			{
    				AmountFound++;
    				ReturnValue[AmountFound] = Accounts[x].GetIDNumber();
    			}
    			ReturnValue[0] = AmountFound;
    		}
    		else
    		{
    		}
    	}
    	return ReturnValue;
    }
    
    int BANK::UpdateAccountPointer()
    {
    	for(int x = 0; x < MAX_NUM_ACCOUNTS; x++)
    	{
    		if(AccountsMap[x] == 1)
    		{
    		}
    		else if(AccountsMap[x] == 0)
    		{
    			AccountPointer = x;
    			return x;
    		}
    		else
    		{
    			return -1;
    		}
    	}
    
    	full = true;
    	return -2;
    }
    
    int BANK::SaveAccount(ACCNT InputAccount)
    {
    	if(AccountsMap[InputAccount.GetIDNumber()] == 0)
    		return -1;
    	else if(AccountsMap[InputAccount.GetIDNumber()] == 1)
    		Accounts[InputAccount.GetIDNumber()] = InputAccount;
    	else
    		return -1;
    
    	return 0;
    }
    
    ACCNT BANK::OpenAccount(int ID)
    {
    	if(AccountsMap[ID] != 1)
    	{
    		ACCNT Temp;
    		Temp.label("~!ERROR OPENING!~");
    		Temp.SetIDNumber(-1);
    		return Temp;
    		Temp.~ACCNT();
    	}
    	return Accounts[ID];
    }
    
    bool ACCNT::isLabeled()
    {
    	return labeled;
    
    }
    and...
    Code:
    int main(int argc, char* argv[])
    {
    	BANK MyBank;
    	ACCNT MyAccount;
    	//array <int> Search(MAX_NUM_ACCOUNTS); 
    	MyBank.ReadInAccounts("test.txt");
    	string mine;
    	if(mine.empty())
    		cout << "empty " << endl;
    
    	MyAccount = MyBank.OpenAccount(1);
    
    	cout << MyAccount.GetName() << endl;
    
    	//Search = MyBank.SearchByBalance(GREATER_THAN,-1);	
    		
    	return 0;
    }
    and... in test.txt....
    000001
    David's Mula
    632.03
    000002
    Ryan's chicken Fund
    0.05
    000003
    Tyler's Birth Control Fund
    6.95
    000004
    Mark's Mole Removal Fund
    236695.00
    000005
    Toilet Bowl Account
    234.55
    000006
    Renee's Identification Beauro
    122665.00
    000007
    Dad's Money
    100.01
    000008
    Mom's Squeegy Trading Business
    456.99
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Did you include the <string> header?

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    yes.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Fixed problem... I was using cstring and iostream- I needed to use string and iostream (C++ versions)
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Oh great- now that the code compiles, it's riddled with bugs- or, one bug. Upon running the program, it crashes. I can't seem to pinpoint where. Whatever I comment out, it still crashes.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    How far does it get? What debug information do you get? What is the error message when it crashes?

  10. #10
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    It's an unhandled exception error. I'm having trouble finding where it dies, because the debugger won't tell me. I'm not at my comp right now, when I get to it (it being my computer) I'll play around and then give more precise info.
    Last edited by CodeMonkey; 07-18-2003 at 12:05 PM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  11. #11
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Put each function's body in a try..catch and in the catch, output a message telling you which function an exception happened in, then exit.

  12. #12
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    I looked up try and catch, and have a loose understanding of them. Could somebody do an example with one of my functions for me so I can get a firm idea.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  13. #13
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    try{
    
    	string s = Accounts[ x ].GetName( );
    
    }
    
    catch( ... ) {
    
    	cout<<"Error occured in Accounts[ x ].GetName( ) Line:"<<__LINE__<<endl;
    
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  14. #14
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Thanks yous. I thought it was imperitive to have an object in the catch parameters, but it isn't.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  3. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21