Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Ian Collins Newsgroups: comp.lang.c++ Subject: Re: " *first = new string(*rhs.first);" cannot compile in MSVC Date: Thu, 12 Jan 2012 18:04:42 +1300 Lines: 80 Message-ID: <9n77vbFvfbU4@mid.individual.net> References: <9n6v0qFvfbU3@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net jgVtIYR/1yyWYFdP660ImQYBabS8b9El8ZMLsD9n8TEzZpnJPG Cancel-Lock: sha1:gh6JDZLIEBBTMIsDCM6fXqgrwJk= User-Agent: Mozilla/5.0 (X11; SunOS i86pc; rv:6.0.2) Gecko/20111003 Thunderbird/6.0.2 In-Reply-To: Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:13261 On 01/12/12 04:29 PM, fl wrote: > On Jan 11, 9:31 pm, Ian Collins wrote: >> On 01/12/12 03:22 PM, fl wrote: >> >> >> >> >> >>> Hi, >>> I am learning C++ with a web download small C++ routine. The member >>> function below the dot line cannot compile in MSVC express 2010. The >>> error message is: >> >>> 1>c:\users\jeff\documents\visual studio 2010\projects\ldpc\person >>> \person.cpp(27): error C2679: binary '=' : no operator found which >>> takes a right-hand operand of type 'std::string *' (or there is no >>> acceptable conversion) >> >>> Because the web site is a university C++ course, I guess it should not >>> be a totally wrong. I also feel that line 27 usage is really strange. >> >>> What is your opinion? Thanks a lot. >> >>> .................... >> >>> const Person& Person::operator=(const Person& rhs){ >>> if( this !=&rhs ){ >>> delete first; >>> delete last; >>> (line 27): *first = new string(*rhs.first); >>> *last = new string(*rhs.last); >>> id = rhs.id; >>> } >> >>> return *this; >>> } >> >> The definition of Person would help (what are first and last?). > > Thank you very much. Here is the Person definition file: > > //http://www.cs.middlebury.edu/~dkauchak/classes/s10/cs062-s10/ > problems/destructor > ........... > #ifndef PERSON_H > #define PERSON_H > > #include > > using namespace std; Don't ever put this in a header! > class Person{ > public: > Person(const string& first, const string& last, int id); > Person::Person(const Person& rhs); > const Person& Person::operator=(const Person& rhs); > string getFirst() const; > string getLast() const; > void setLast(const string& newLast); > int getId() const; > string toString() const; > > private: > string* first; > string* last; > int id; > }; > > #endif So your original code is attempting to assign a std::string* to a std::string. As Alf suggests, don't use pointers. -- Ian Collins