Thread: cout<< and getline() in class definition

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    50

    cout<< and getline() in class definition

    Code:
    #include <iostream>
    #include<cstdlib>
    
    
    using namespace std;
    
    
    class ObPass {
    private:
       char name[40] = "Javaid Iqbal"; 
       int n = 33;                      
       char ch = 'A';
    public:
       	cout << name << n << ch;  
    	getline(cin, name);    	
            void disp(){
       	cout << name << "\n" <<n << "\n" << ch << endl;
    };
    int main() {
    	ObPass object;
      	object.disp();
      	system("pause"); 
       	return 0;
    	}
    Why we can't use cout << name << n << ch; and getline(cin, name); in class definition?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,254
    Maybe post something which compiles.

    You can't just write random lines of code inside the class. It has to be in a function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    50
    Quote Originally Posted by Salem View Post
    Maybe post something which compiles.

    You can't just write random lines of code inside the class. It has to be in a function.
    Code:
    void disp3(){                                      
          
    	  cout << "Enter Name: " << endl;  
          getline(cin,name);
    	  cout << "Enter Integer: " << endl;  
    	  cin >> n; 
          cout<<"Enter Character: "<<endl;
    	  cin >> ch;    
    	  cout << name << n  << ch << endl;
    	  cout <<"=======================================" << endl;
        }
    I'm trying getline() function, in the member function definition, but inside member function it is still giving error. cin is not able to enter multi words string and if getline() too is not working, then how to enter multi words sting inside the member function?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,885
    What's the error message?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,254
    There are two kinds of getline.

    getline (string) - C++ Reference
    This one operates exclusively on strings.

    istream::getline - C++ Reference
    This one works with char pointers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2019
    Posts
    50
    Quote Originally Posted by laserlight View Post
    What's the error message?
    Error is as following,

    In member function 'void ObPass::disp3()':
    [ERROR] no matching function for call to 'getline(std::istream&, char[40])'

  7. #7
    Registered User
    Join Date
    Mar 2019
    Posts
    50
    Well, I've gotten the answer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 08-06-2011, 11:58 AM
  2. Replies: 40
    Last Post: 06-02-2010, 10:08 AM
  3. Replies: 5
    Last Post: 04-17-2008, 02:31 AM
  4. class definition Q
    By scipitam in forum C++ Programming
    Replies: 8
    Last Post: 10-24-2006, 09:06 PM
  5. Replies: 7
    Last Post: 05-26-2005, 10:48 AM

Tags for this Thread