Instance object of a class

Hey all,

I am on a early beginner level. Last week I did my first exercise to create a password validator which worked well. But the code was completely in the main (). Now I want to expand to have a choice between login and new password.
I want to keep it very simple, so I decided to create 2 classes, one of them is the class New_PW.
I started from scratch to build the structure first and have the problem that within main() I want to create an instance of the New_PW class (line 17 of main.cpp). I get the failure message "'New_PW' was not declared within this scope". I tried to put the line in front of the main (), but the result is the same.
Of course I also get a failure then in line 18 of main.cpp trying to access the New_PW_input function with the object because it does not exist. But I think this will resolve automatically as soon as I have the object existing.
All lessons I read state that it should work like this. New_PW Hay_PW; so Hay_PW object of New_PW class
I searched in forums like this and read more than 20 posts with a similar problem, but I could not find my specific one. Most posts state that a class have to be declared before using it. So I tried "class New_PW;" before main() in the main.cpp. The failure message then changes to "aggregate 'New_PW Hay_PW' has incomplete type and cannot be defined." So I guess that the problem is that my main.cpp does not know the class header and source. But I did not find any lesson or post which states which link I need to write in the main.cpp.


main.cpp
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
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

int main()
{
    string x;
    cout << "Do you need a new password? If so, please type Yes; if not No" << endl;
    getline(cin,x);

    if (x == "Yes") {
    }
    else if (x == "No") 
    {
    New_PW Hay_PW;
    Hay_PW.New_PW_input();
    }
    else {
    cout << "Error - Try again" <<endl;
    }
    return 0;
}


include\New_PW.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef NEW_PW_H
#define NEW_PW_H
#include <string>

using namespace std;

class New_PW
{
    public:
       void New_PW_input();
       string PW;

    protected:

    private:
};

#endif // NEW_PW_H 


src\New_PW.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "New_PW.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

void New_PW::New_PW_input()
        {
            cout << "Please type new password" << endl;
            getline(cin,PW);
        };
Last edited on
You need #include "New_PW.h" in your main.cpp as well. Wherever you need New_PW you also need to include the header.
thanks
Topic archived. No new replies allowed.