Cannot be overloaded?

Jul 4, 2011 at 4:09am
So I'm doing a little class assignment and I've ran into a little problem. My teacher in all of his source files declares all his method signatures all at once at the top of his class. However when I tried to do that I get a compiler error. Here's the source:
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
#include <iostream>
using namespace std;

const short DEFAULT_TYPE = 0, CLIENT_TYPE = 1,
            EMPLOYEE_TYPE=2, ADMIN_TYPE = 3;

class Account
{
    //Public members
    public:
        bool equals(Account);
        string toString();
        void print();
        string getName();
        void setName(string);
        short getAccountType();

    protected:
        string accountName;
        short accountType;

        Account()   {
            accountType = DEFAULT_TYPE;
            accountName = "Default";
        }

        Account(short typeIn, string nameIn)    {
            accountName = nameIn;
            accountType = typeIn;
        }
        
        bool equals(Account accountIn)  {
            int equality = accountIn.getName().compare(accountName);
            return(equality == 0)? true: false;
        }

};


When I try to compile that I get this error:

D:\Andy\My Dropbox\Dropbox\...Account.cpp|32|error: 'bool Account::equals(Account)' cannot be overloaded|
D:\Andy\My Dropbox\Dropbox\...Account.cpp|11|error: with 'bool Account::equals(Account)'|


I don't really know why this doesn't work. Any suggestions?
Jul 4, 2011 at 4:28am
closed account (1yR4jE8b)
The way you are doing it is declaring two equals functions with the same argument lists. You want to write a function prototype, then implement the function definition in a .cpp file.

Account.hpp
1
2
3
4
5
6
7
8
class Account
{
//your member functions go *inside* the class declaration
public:
  //...
  bool equals(Account) const;
  //...
};


Account.cpp
1
2
3
4
5
6
7
8
//then in a seperate .cpp file you write the definitions:
#include "Account.h"
//...
bool Account::equals(Account accountIn) const
{
  return (accountIn.getName().compare(accountName) == 0);
}
//... 



Jul 7, 2011 at 11:13pm
Ok I tried to do what was mentioned above, but I'm still having a little bit of trouble. Its a different class but the problem is the same.

Password.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
using namespace std;

#ifndef PASSWORD_H_INCLUDED
#define PASSWORD_H_INCLUDED

class Password  {
    private:
    string password;

    public:
    string getInput();
    bool changePassword();
    bool authenticate(string passIn);
    string toString();
    Password();
    Password(string passIn);
}

#endif // PASSWORD_H_INCLUDED 


Pasword.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <windows.h>
#include "Password.h"

using namespace std;

const string PASS_ERROR = "Incorrect Password";

bool Password::changePassword()  {
    cout << "Enter Previous Password: " << endl;
    string old = Password::getInput();
    if(Password::authenticate(old)) {
        cout << endl << "Enter New Password: ";
        Password::password = Password::getInput();
        return true;
    }
    else    {
        cout<<endl<<PASS_ERROR;
        return false;
    }
}//.... 


But Im still getting a compiler error heres what it says.
D:\Andy\...Password.cpp|5|error: expected unqualified-id before 'using'|

whats the problem here?
Jul 7, 2011 at 11:20pm
Missing semicolon on line 18 in password.h
Jul 8, 2011 at 12:30am
just like the sock monster in everyones laundry room who steals socks so you only have one matching sock there is also a semicolon goblin. he steals semicolons.

ok. maybe not...
Topic archived. No new replies allowed.