functions

why is my functions not recognizing name and Bname.

main.cpp: In function ‘void setName(std::string)’:
main.cpp:38:5: error: ‘name’ was not declared in this scope
name=Bname;
^
main.cpp: In function ‘std::string getName()’:
main.cpp:42:8: error: ‘name’ was not declared in this scope
return name;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #include<iostream>
#include<string>
using namespace std;

class Bank_Account      //name of the class
{
    private:
    string name;        //attributes to the private so its protected
    int acct_num;
    int pin_num;
    double Balance;
    
    Bank_Account()
    {
        Balance=0;           //balance needed to intialized
    }
    
    Bank_Account  (double B)
    {
        Balance=B;
    }
    public:
    
void setName ();    //for name to store
double getName (string);   //for name to store and retrive name
void setAcctnum ();//account num to store 
double getAcctnum (double); //acct num for retrive acct number
double getBalance (double);//retrieve the balance 
bool setPIN  (double);  // to store number
double checkPIN(double); // receives a PIN number as parameter, returns TRUE if it matches the PIN in the object
void deposit(); // Receives amount of money to deposit.If this amount is valid (positive) then + to balance. Otherwise do nothing. No return value.
void  withdraw(); // Receives amount of money to withdraw. If this amount is valid(positive) then make sure there are funds to cover the withdrawl.
            //If so then subtract from balance. If not enough money then printthe message “Insufficent funds” and make no changes. No return value
   
};
void setName (string Bname)
{
    name=Bname;
}
string getName ()
{
return name;
}

int main()
{
   
}
(1.) The signature of void setName (); //for name to store
does not match the definition,
void setName (string Bname)

Change the signature to void setName(string);

Do the opposite for getName -- remove the (string) as a parameter in line 25.

(2.) You need to add ClassName::function_name so that we know we're defining a function that belongs to that particular class.

Change line 36 to void Bank_Account::setName (string Bname);

Change line 40 to string Bank_Account::getName()
Last edited on
1
2
3
4
void setName (string Bname)
{
    name=Bname;
}

For the compiler it looks like a normal, free function.
No way of knowing it that it belongs Bank_Account class as I can guess.
You need to add Bank_Account:: in front of the function name.
thank you guys!
Topic archived. No new replies allowed.