Thread: Instantiation question

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    121

    Instantiation question

    Can someone tell me why I get non-object errors if I don't instantiate the objects with 3 strings? I provided default arguments for this object, so what is the problem? Sample error received when objects not instantiated (receive them for all functions):

    c:\Documents and Settings\Owner.Mynotebook\My Documents\Visual Studio Projects\helloWorld\helloWorld.cpp(52): error C2228: left of '.inputVals' must have class/struct/union type

    Thanks, and Happy Easter!


    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class TelBook
    {
    protected:
    	string last_;
    	string first_;
    	string tel_;
    public:
    	TelBook(string, string, string);
    	void inputVals();
    	void dispVals();
    	void assignVals(TelBook*);
    };
    
    TelBook::TelBook(string f = "", string l = "", string t = "")
    {
    	first_ = f;
    	last_ = l;
    	tel_ = t;
    }
    
    void TelBook::inputVals()
    {
    	cout << "Enter the last name: ";
    	getline(cin, last_, '\n');
    	cout << "\nEnter the first name: ";
    	getline(cin, first_, '\n');
    	cout << "\nNow enter the telephone number: ";
    	getline(cin, first_, '\n');
    }
    
    void TelBook::dispVals()
    {
    	cout << "Last name: " << last_ << ", first name: " << first_ 
    		 << ", telephone number: " << tel_ << endl;
    }
    
    void TelBook::assignVals(TelBook *myTelEntry)
    {
        myTelEntry->last_ = last_;
    	myTelEntry->first_ = first_;
    	myTelEntry->tel_ = tel_;
    }
    
    int main()
    {
    	
    	TelBook myTelBookObject();
    	myTelBookObject.inputVals();
    	myTelBookObject.dispVals();
    	TelBook mySecondTelBookObject();
    	mySecondTelBookObject.inputVals();
    	mySecondTelBookObject.assignVals(&myTelBookObject);
    	myTelBookObject.dispVals();
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    41
    Drop the parentheses.

    Code:
    TelBook myTelBookObject; //creates a TelBook instance, whereas 
    // TelBook myTelBookObject(); // is a declaration of a function returning a TelBook

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Quote Originally Posted by Oysterman View Post
    Drop the parentheses.

    Code:
    TelBook myTelBookObject; //creates a TelBook instance, whereas 
    // TelBook myTelBookObject(); // is a declaration of a function returning a TelBook
    Thanks, I overlooked that!

    -Patrick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-12-2007, 01:02 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  4. question about .net to 6.0 change causing errors
    By jverkoey in forum C++ Programming
    Replies: 17
    Last Post: 03-23-2004, 10:45 AM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM