Below is code for main, Prac.h, Student.h, Student.cpp, Prac.cpp
main
Code:
#include <iostream>
#include <string>
#include <map>
#include<vector>
#include "Student.h"
#include "Prac.h"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
// Student h("kunal",89);
Prac p;
Student l;
l= p.test();
return 0;
}
Student.h
Code:
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
/** Default constructor */
Student();
Student(string t_name, int t_age);
protected:
private:
string name;
int age;
};
#endif // STUDENT_H
Student.cpp
Code:
#include "Student.h"
Student::Student()
{
//ctor
}
Student::Student(string t_name, int t_age){
name=t_name;
age=t_age;
}
Prac.h
Code:
#ifndef PRAC_H
#define PRAC_H
#include "Student.h"
class Prac
{
public:
/** Default constructor */
Prac();
Student& test();
protected:
private:
};
#endif // PRAC_H
Prac.cpp
Code:
#include "Prac.h"
Prac::Prac()
{
//ctor
}
Student& Prac::test(){
Student p("asdas",90);
cout<<&p;
return p;
}
When I compile program, I get error:
PATHcode/PlusPlusC/learn/src/Prac.cpp||In member function ‘Student& Prac::test()’:|
PATH/Documents/lab/code/PlusPlusC/learn/src/Prac.cpp|10|warning: reference to local variable ‘p’ returned [enabled by default]|
||=== Build finished: 0 errors, 1 warnings ===|
When I run program, I get an exception.
I am going blind in this phase, how to return an object of class.
when to use * and when to use & while passing and returning.
Any help shall be highly appreciated in this problem.
I am new to c++/pointers/reference world.