#include <string>
#include <iostream>
#ifndef NAME_H
#define NAME_H

class Name 
{
    private:
        std::string first_,last_ ;
    public:
    explicit Name(const std::string & first = "john",   const std::string & last = "doc"):first_(first),last_(last)
            {
                if(!isvalidlast(last_) || isvalidfirst(first_))
                    throw "Name :: Name(const string &,const string &):invalid Name";
            }

    static bool isvalidlast(const std::string & last){}
    static bool isvalidfirst(const std::string & first){}
    std::string getfirst() const 
        {
          return first_;
        }
    bool setfirst (const std::string & first)
        {
            if(!isvalidfirst(first)) return false;
                first_=first;
                return true;
        }
    friend std::istream & operator >> (std::istream &,Name &);
    friend std::ostream & operator <<(std::ostream &,Name &); 



}; //end of class defn

    inline std::ostream & operator << (std::ostream & os,const Name & n)
    {
        return os << n.first_ << ' ' << n.last_;
    }
    inline std::istream & operator >> (std::istream & is,Name & n)
    {
        return is >> n.first_ >> n.last_;
    }

#endif

.cpp

#include <set>
#include <iostream>
#include "Name.h"
//using namespace std;
int main()
{     Name n;
    std::set<Name> s;

    while(cin >> n)
        s.insert(n);

}

now it is giving error in

while(cin >> n)
        s.insert(n);


    cin is not defined in this context

cin is in the std namespace. If you don't have a using statement somewhere to expose it, you need to qualify the name: std::cin.

You commented out the using namespace std line. You need to either uncomment that or specify that you want the cin from the std namespace. Something like std::cin >> n

#ifndef NAME_H
#define NAME_H
#include <string>
#include <iostream>

class Name 
{
private:
        std::string first_,last_ ;
public:
    explicit Name(const std::string & first = "john",   const std::string & last = "doc"):first_(first),last_(last)
    {
        if(!isvalidlast(last_) || isvalidfirst(first_))
            throw "Name :: Name(const string &,const string &):invalid Name";
    }

    static bool isvalidlast(const std::string & last)
    {
    std::string::size_type i = 0;

    // If last name is empty
    if(last.length() == 0)
    {
        return false;
    }

    // Cannot contain whitespace, only alphabets
    for(i = 0; i < last.length(); i++)
    {
        if(!isalpha(last[i]))
        {
            return false;
        }
    }

    return true;

    }
    static bool isvalidfirst(const std::string & first)
    {
    std::string::size_type i = 0;

    // If last name is empty
    if(first.length() == 0)
    {
        return false;
    }

    // Cannot contain whitespace, only alphabets
    for(i = 0; i < first.length(); i++)
    {
        if(!isalpha(first[i]))
        {
            return false;
        }
    }

    return true;
    }
    std::string getfirst() const 
    {
      return first_;
    }
    std::string getlast() const 
    {
      return last_;
    }
    bool setfirst (const std::string & first)
    {
        if(!isvalidfirst(first)) 
            return false;
            first_= first;
            return true;
    }
    bool setlast (const std::string & last)
    {
        if(!isvalidlast(last)) 
            return false;
            last_= last;
            return true;
    }
    friend std::istream & operator >> (std::istream &,Name &);
    friend std::ostream & operator <<(std::ostream &,const Name &);   



}; //end of class defn

inline std::ostream & operator << (std::ostream & os, const Name & n)
{
    return os << n.getfirst() << ' ' << n.getlast();
    //return os << n.first_ << ' ' << n.last_;
}
inline std::istream & operator >> (std::istream & is,Name & n)
{
    return is >> n.first_ >> n.last_;
}

#endif

second file is

#ifndef STUDENT_H
#define STUDENT_H

#include "Name.h"
class Student
{
    private:
        std::string id_;
        Name name_;
        static bool isValidId(const std::string& id);

    public:
        friend std::ostream & operator <<(std::ostream &,const Student &);    
        friend std::istream & operator >>(std::istream &, Student &);
        explicit Student(const std::string& id , const std::string& first , const std::string& last ) : id_(id), name_(first, last) {}
        explicit Student() ;         

}; // end of class defn

inline  std::ostream& operator <<(std::ostream& os,const Student & s)
{
    os << s.id_ << s.name_.getfirst() << s.name_.getlast() << std::endl;
    //os << s.id_  << std::endl;
    return os;
}
inline  std::istream& operator >>(std::istream& is,Student & s)
{
    //is >> s.id_;
    is >> s.id_ >> s.name_ ;
    return is;
}

#endif          

third file is

#include <sstream>
#include <iomanip>
#include <map>
#include "student.h"

using namespace std;
//bool Name::isvalidfirst(const string & first)
//{



//}
//bool Name::isvalidlast(const string & last)
//{


//}  // end of isvalidlast
bool Student::isValidId(const std::string& id)
        {
            if(id.size() != 9)
                return false;
            if(id[0] != 'a')
                return false;
            for(string::size_type i = 1; i < 9; i++)
                if(!isdigit(id[i]))
                    return false;
        }

int main()
{
//Student s1;
Student s("a12345678", "Homer","Simpson");
cout << s << endl;
//map<Student ,int> m;
//Student s;

//int score;
//while(cin >> s >> score)
    //m[s]  += score;

}

Error is

Segmentation fault (core dumped)

Can someone explain?

This article has been dead for over six months. Start a new discussion instead.