Confusing compilation error

Hi everyone.

I have a strange error that comes up when I compile my code:
"void Account::setType(std::string)' member function declared in class"
I am new to this site; pardon me if I am unable to get the correct formatting for this post. (I would be happy to accept any suggestions on how to improve formatting.)

Header file:

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
#include <iostream>
using namespace std;
#ifndef _Account_h_
#define _Account_h_

class Account{
private:
string first;
string last;
string address;
int SSN;
string type;
float interest;
float balance;

public:
void setFirst(string);
void setLast(string);
void setAddress(string);
void setSSN(int);
void setInterest(float);
void setBalance(float);
void setType(string);

string getFirst();
string getLast();
string getAddress();
int getSSN();
float getInterest();
float getBalance();

void Deposit(float);
void Withdrawal(float);
void CalculateTotalBalance();

void Print();
};

#endif 



Function Definitions (they are mainly access functions so I omitted many of them to avoid repetition):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Account.h"
#include <iomanip>
#include <iostream>

void Account::setBalance(float b) {
balance = b;
}

void Account::setType(string t) {
type = t;
}

string Account::getFirst() {
return first;
}

string Account::getLast() {
return last;
}

string Account::getAddress() {
return address;
}



The strange part is, I had many other set functions like it (with string parameters and void return types) before I added setType but I didn't get any errors about those functions. I added the setType a few days after I had written everything else.
Any help would be much appreciated.
Last edited on
Don't you need to include <string> before using string?
On some implementations, <iostream> includes <string>. It's not something you should rely on - it's better to explicitly include <string> in the file that requires it - but it would explain why it was compiling.
I tried including "#include <string>" but I continued to get the error.
I will paste the rest of the function definitions, maybe there is something there that is related to the error.

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "Account.h"
#include <iomanip>
#include <iostream>
#include <string>

void Account::setFirst(string f) {
        first = f;
}

void Account::setLast(string l) {
        last = l;
}

void Account::setAddress(string a) {
        address = a;
}

void Account::setSSN(int ssn) {
        SSN = ssn;
}

void Account::setInterest(float i) {
        interest = i;
}

void Account::setBalance(float b) {
        balance = b;
}

void Account::setType(string t) {
        type = t;
}

string Account::getFirst() {
        return first;
}

string Account::getLast() {
        return last;
}

string Account::getAddress() {
        return address;
}

int Account::getSSN() {
        return SSN;
}

void Account::Deposit(float d) {
        balance = balance + d;
}

void Account::Withdrawal(float w) {
        balance = balance - w;
}

void Account::CalculateTotalBalance() {
        balance = balance + balance*interest;
}
 
void Account::Print() {
        cout << first << " " << last << setw(5) << address << setw(5) << SSN << setw(5) << accounttype << setw(5) << interest << "%" << setw(5) << balance << endl;
}
Last edited on
closed account (Dy7SLyTq)
you also need to put in using std::string;
No - the OP already has using namespace std; in the header.

Having said that, it's not a good idea to put that in the header, because it then forces every file that includes the header to bring the entirety of std:: into the global namespace. It's much better to put using statements into the .cpp file instead.

I must admit, I'm puzzled by the OP's problem.
Last edited on
What exactly do you mean by that?

What does who mean by what?
Does that mean that I should remove "using namespace std;" from the header, but include it in the function definitions file?
Oh sorry, I was referring to DTSCode's post when I said "What exactly do you mean by that." I wasn't sure how to use std::string.
Does that mean that I should remove "using namespace std;" from the header, but include it in the function definitions file?

Yes.
OK, thank you.
I tried including <string> in the header but not in the .cpp file, and including it in the .cpp but not in the header, and including it in both. Nothing has worked so far.
Should I add std::string as well to both files?
closed account (Dy7SLyTq)
yes it wont work without "using std::string;"
How exactly do I use that code? Like
using std:: string;
at the top of the file with #include <iostream>?
Or like
1
2
3
void Account::setType(using std::string t) {
        type = t;
}


Also, do I have to include it (along with #include <string>) in both the header and the .cpp or just one?
closed account (Dy7SLyTq)
you need to #include <string> in both files and then using std::string; directly underneath
Hmm, the error is refusing to go away. This seems like it would be such a simple error to fix.

error: no 'void Account::setType(std::string)' member function declared in class 'Account'
Last edited on
In your header file, you'll need to simply put std::string instead of string throughout the file.

In your source code file, you can put using std::string; which means that every time you have string, the compiler will understand that you really mean std::string.

I don't think any of this has anything to do with your original problem, though. This is just about learning good practice with using statements.

I don't have a clue what your problem is, even now that you've told us the error message in full rather than omitting the important first word.
Sorry about that, I was really tired and couldn't read properly.

Thank you for trying anyway, and thanks for the tips on the using statements. If you suddenly hit a eureka moment, please don't hesitate to let me know!
Topic archived. No new replies allowed.