-
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.
-
-
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...
-
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
-
Did you include the <string> header?
-
-
Fixed problem... I was using cstring and iostream- I needed to use string and iostream (C++ versions)
-
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.
-
How far does it get? What debug information do you get? What is the error message when it crashes?
-
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.
-
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.
-
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.
-
Code:
try{
string s = Accounts[ x ].GetName( );
}
catch( ... ) {
cout<<"Error occured in Accounts[ x ].GetName( ) Line:"<<__LINE__<<endl;
}
-
Thanks yous. I thought it was imperitive to have an object in the catch parameters, but it isn't.