Header variables not working in .cpp

I'm starting to learn how to work with separate files for classes and this is the only issue I'm having right now but it makes no sense to me.

I'm getting an error telling me that my variables yesno, and personname aren't declared in this scope.

please help.


My header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef NAME_H
#define NAME_H
#include <string>

class Name {
public:
    void YourName(std::string yn, std::string pn, int wl) {
    }
    private:
    std::string yesno;
    std::string personname;
    int loop;
};

#endif // NAME_H 




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


void YourName(std::string yn, std::string pn, int wl)
{
    while (wl = 1) {
std::cout << "Please enter your name." << std::endl;
std::cin >> personname;
std::cout << "You entered\n" << personname << ".\nIs that correct?\nYes or no?" << std::endl;
std::cin >> yesno;
if (yesno == "YES" || yesno == "Yes" || yesno == "yes") {
    std::cout << "Thank you!" << std::endl;
    break;
}
else if (yesno == "NO" || yesno == "No" || yesno == "no") {
    std::cout << "Please enter your name again." << std::endl;
}
    }
}


I understand that the variables I've declared in my header are private but shouldn't it work in the .cpp if they belong to the same class?

I've also tried with a member initializer in my header like this
1
2
3
4
    void YourName(std::string yn, std::string pn, int wl)
    : yesno(yn), personname(pn)
{
}


But as you world expect I got an error because member initializers are only for constructors.
Last edited on
The compiler is no psychic. How shall it know that void YourName(std::string yn, std::string pn, int wl) belongs to the class Name? You need to tell the compiler by writing
void Name::YourName(std::string yn, std::string pn, int wl)

In the header file you need to remove the curly braces on line 7/8 and replace them with a ;
Thank you!!!

I'll try it out as soon as I get back home
Topic archived. No new replies allowed.