not declared in the scope

I created a header file called view.h

class View
{
public:
void showLoginScreen();
}

A cpp file called view.cpp

#include<iostream>
#include<string>
#include"view.h"
using namespace std;

void View::showLoginScreen()
{
string user;
char* pass;

cout<<"Enter Your Username:\t";
getline(cin,user);

pass=getpass("Enter Your Password:\t");
}


main.cpp file

#include<iostream>
#include"view.h"
using namespace std;

int main()
{
View obj;
obj.showLoginScreen();
return(0);
}


When I am compiling the main.cpp file I am getting an error message:

In function 'int main()'
View was not declared in this scope.
Expected ; before obj.
obj not declared in this scope.


What is the cause of this problem?


You need a semi-colon after the class definition.
Semi colon didn't solve the problem.
Did for me. The following compiles without any trouble.

view.h
1
2
3
4
5
class View
{
public:
void showLoginScreen();
};


view.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include<string>
#include"view.h"
using namespace std;

void View::showLoginScreen()
{
string user;
char* pass;

cout<<"Enter Your Username:\t";
getline(cin,user);

//pass=getpass("Enter Your Password:\t");
}


main.cpp
1
2
3
4
5
6
7
8
9
10
#include<iostream>
#include"view.h"
using namespace std;

int main()
{
View obj;
obj.showLoginScreen();
return(0);
}


Compiled with command:
g++ view.cpp main.cpp




The code is working fine when I am writing the entire code in a single file. But the compiler is throwing error when I am using 3 separate files.

Well for me, writing it in three separate files as in my post above and compiling it with the command line as above works fine. If you do the exact same thing and it doesn't work, the problem is not with the code.
Topic archived. No new replies allowed.