-1

I'm trying to separate my c++ code to one header and one cpp file, but the interpreter showing some errors.

Here is my code:

Password.h:

#ifndef PASSWORD_H
#define PASSWORD_H

class Password {
private:
    string aliasName_;
    int hashOfPassword_;
public:
    void setAliasName(string aliasName);
    void setHashOfPassword(int hashOfPassword);
    string getAliasName() { return aliasName_; }
    int getHashOfPassword() { return hashOfPassword_; }
};

#endif

Password.cpp:

#include <string>
#include "Password.h"

using std::string;

void Password::setAliasName(string aliasName) {
    aliasName_ = aliasName;
}
void Password::setHashOfPassword(int hashOfPassword) {
    hashOfPassword_ = hashOfPassword;
}

Errors:

Error   C2065   'aliasName_': undeclared identifier X\password.cpp  7
Error   C2511   'void Password::setAliasName(std::string)': overloaded member function not found in 'Password'  X\password.cpp  6
Error   C3646   'aliasName_': unknown override specifier    X\password.h    6
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    X\password.h    6
Error   C2061   syntax error: identifier 'string'   X\password.h    9
Error   C3646   'getAliasName': unknown override specifier  X\password.h    11
Error   C2059   syntax error: '('   X\password.h    11
Error   C2334   unexpected token(s) preceding '{'; skipping apparent function body  X\password.h    11

Anyone have any ideas ?

werdf02
  • 95
  • 1
  • 11

3 Answers3

3

You need to move using std::string before the declaration of your class:

#ifndef PASSWORD_H
#define PASSWORD_H

#include <string>

using std::string;

class Password {
…

and remove it from your .cpp file.

Also, you might want to use #pragma once instead of the traditional #ifndef/#define/#endif and finally you might want to make your argument and methods const when need be.

zmo
  • 24,463
  • 4
  • 54
  • 90
  • Thank you Zmo to your fast answer :) – werdf02 Mar 05 '16 at 19:12
  • no problem, if it helped you, don't hesitate to +1 and accept it ;-) – zmo Mar 05 '16 at 19:17
  • I thought the use of `using` in header files would be bad practice. But I think it is not really importent if it is not a library. Another thing: I didn't know that `#pragma once` is supported by GCC. The last time I searched for it, I did only see that it is an extension of VC. But [this thread](http://stackoverflow.com/q/1143936/4967497) says it is supported by all common compilers. O.o – JojOatXGME Mar 05 '16 at 20:36
  • it's relatively recent, I believe it's been added with support of C++11, so within the last 2 or 3 versions of GCC. About `using`, you need to have access to the type, so you need to do the `#include` in your header. Though it's indeed bad practice to do `using` in the header, because if the user of your library (considering it becomes a lib) might include your header in his `namespace` and pollute the namespace with a symbol from `std`. So it'd be a good idea to do the `#include`, and within the header file call the full "path" to `std::string` in your prototypes. – zmo Mar 05 '16 at 22:03
1

You need to move

#include <string>
using std::string;

To inside Password.h, you can then remove these two lines from Password.cpp.

058 094 041
  • 495
  • 3
  • 6
0

Just add std:: to every string in your header file and remember, you should never use using or using namespace there! You should also include all the headers to the file in which you use them.

ly000
  • 343
  • 4
  • 12