What is wrong with this code?

This is a discussion on What is wrong with this code? within the C++ Programming forums, part of the General Programming Boards category; ********************Start of The code****************** Code: #include<iostream> #include<string> #include<fstream> using namespace std; class Person { string name; public: Person() {name = ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Question What is wrong with this code?

    ********************Start of The code******************
    Code:
    #include<iostream>
    #include<string>
    #include<fstream>
    using namespace std;
    
    class Person
    {
    	string name;
    public:
    	Person() {name = "Roger";}
    	Person(string theName);
    	string getName() { return name; }
    	void setName(string theName) { name = theName;}
    	Person& operator=(const Person& rtSide);
    	friend istream &operator>>(istream& inStream, Person& personObject);
    	friend ostream &operator<<(ostream& outStream, const Person& personObject);
    };
    
    Person& Person::operator=(const Person &rtSide)
    {
    	name = rtSide.name;
    	return *this;
    }
    
    istream &operator>>(istream& inStream, Person& personObject)
    {
    	cout << "PLEASE ENTER NAME: ";
    	inStream >> personObject.name;
    	return inStream;
    }
    
    ostream &operator<<(ostream& outStream, const Person& personObject)
    {
    	outStream << personObject.name << '\n';
    	return outStream;
    }
    
    int main()
    {
    	Person rt("Romano"), rt1("Roger");
    	cout << rt;
    	return 0;
    }
    *********************End of Code********************
    Can anyone help to figure it our what is wrong with my coding? this are the errors generated by the compiler thanks


    --------------------Configuration: assigment3 - Win32 Debug--------------------
    Compiling...
    as3q3.cpp
    C:\Files\assigment3\as3q3.cpp(28) : error C2248: 'name' : cannot access private member declared in class 'Person'
    C:\Files\assigment3\as3q3.cpp(8) : see declaration of 'name'
    C:\Files\assigment3\as3q3.cpp(34) : error C2248: 'name' : cannot access private member declared in class 'Person'
    C:\Files\assigment3\as3q3.cpp(8) : see declaration of 'name'
    C:\Files\assigment3\as3q3.cpp(41) : error C2593: 'operator <<' is ambiguous
    Error executing cl.exe.

    as3q3.obj - 3 error(s), 0 warning(s)



    &#91;code]&#91;/code]tagged by Salem

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Call setName instead of accessing the string object name directly.

    Kuphryn

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Put your class in its own namespace with the operators that act on objects of your class in the same namespace. See what happens. You also need to implement your constructor and it would be a good idea to make it explicit to stop automatic implicit conversions of a string to a person.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    You forgot to define your Overloaded Constructor!

    Code:
    Person::Person(string theName){
    	this->name = "Roger";
    }
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    here like this....
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    namespace{
    
    class Person
    {
    string name;
    public:
    Person() {name = "Roger";}
    explicit Person(string theName):name(theName){}
    string getName() { return name; }
    void setName(string theName) { name = theName;}
    Person& operator=(const Person& rtSide);
    friend istream &operator>>(istream& inStream,Person& personObject);
    friend ostream &operator<<(ostream& outStream, const Person& personObject);
    };
    
    Person& Person::operator=(const Person &rtSide)
    {
    name = rtSide.name;
    return *this;
    }
    
    istream &operator>>(istream& inStream,Person& personObject)
    {
    cout << "PLEASE ENTER NAME: ";
    inStream >> personObject.name;
    return inStream;
    }
    
    ostream &operator<<(ostream& outStream, const Person& personObject)
    {
    outStream << personObject.name << '\n';
    return outStream;
    }
    
    }
    
    int main()
    {
    Person rt("Romano"), rt1("Roger");
    cout << rt;
    return 0;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    The line 28 error is in an overloaded operator for istream or something, right? But since name is a private member of person, nothing outside of the person class can access it, thus generating the error - an istream (or is it even an istream? It sort of looks like a global function to me) is trying to access a private member of person, which it can't. The same goes for the line 34 error.
    Last edited by Hunter2; 11-19-2002 at 12:26 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    but the function is declared as a friend and so can access all of persons private data without any problem. The problem was due to some sort of clash within the global namespace. Solution. Put the class in its own namespace with its operators. Now no ambiguity. If you dont like an anonymous namespace then give it a name.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    NO, the problem was simple - I just added a definition for the Overloaded Person() constructor and everything worked without change.

    He just forgot to define his overloaded fxn, that's what caused the errors.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    class Person
    {
    private:
    	string name;
    public:
    	Person() {this->name = "Roger";}
    	Person(string theName);
    	string getName() { return this->name; }
    	void setName(string theName) { this->name = theName;}
    	Person& operator=(const Person& rtSide);
    	friend istream &operator>>(istream& inStream, Person& personObject);
    	friend ostream &operator<<(ostream& outStream, const Person& personObject);
    };
    
    Person::Person(string theName){ //ADDED
    	this->name = "Roger";
    }
    
    Person& Person::operator=(const Person &rtSide)
    {
    	this->name = rtSide.name;
    	return *this;
    }
    
    istream &operator>>(istream& inStream, Person& personObject)
    {
    	cout << "PLEASE ENTER NAME: ";
    	inStream >> personObject.name;
    	return inStream;
    }
    
    ostream &operator<<(ostream& outStream, const Person& personObject)
    {
    	outStream << personObject.name << '\n';
    	return outStream;
    }
    
    int main()
    {
    	Person rt("Romano"), rt1("Roger");
    	cout << rt;
    	return 0;
    }
    Last edited by OneStiffRod; 11-19-2002 at 12:59 PM.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Okay, I'll just butt out
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Nah, "butt back in". You've got good stuff! Right, or not, an idea is an idea. (Heck, I didn't have any! )

    P.S. I learn a lot more from being wrong than I ever do from being right.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 05:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 01:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21