Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #13261
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: " *first = new string(*rhs.first);" cannot compile in MSVC |
| Date | 2012-01-12 18:04 +1300 |
| Message-ID | <9n77vbFvfbU4@mid.individual.net> (permalink) |
| References | <d29fdc08-5a93-47d6-a716-926803e54b36@k28g2000yqc.googlegroups.com> <9n6v0qFvfbU3@mid.individual.net> <dbd8d3b0-7de5-49f3-8fe4-ad7188e261ca@y12g2000yqc.googlegroups.com> |
On 01/12/12 04:29 PM, fl wrote:
> On Jan 11, 9:31 pm, Ian Collins<ian-n...@hotmail.com> 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<string>
>
> 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
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
" *first = new string(*rhs.first);" cannot compile in MSVC fl <rxjwg98@gmail.com> - 2012-01-11 18:22 -0800
Re: " *first = new string(*rhs.first);" cannot compile in MSVC Ian Collins <ian-news@hotmail.com> - 2012-01-12 15:31 +1300
Re: " *first = new string(*rhs.first);" cannot compile in MSVC fl <rxjwg98@gmail.com> - 2012-01-11 19:29 -0800
Re: " *first = new string(*rhs.first);" cannot compile in MSVC "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-01-12 04:42 +0100
Re: " *first = new string(*rhs.first);" cannot compile in MSVC Ian Collins <ian-news@hotmail.com> - 2012-01-12 18:04 +1300
Re: " *first = new string(*rhs.first);" cannot compile in MSVC BCFD36 <bcfd36@cruzio.com> - 2012-01-11 23:07 -0800
Re: " *first = new string(*rhs.first);" cannot compile in MSVC Juha Nieminen <nospam@thanks.invalid> - 2012-01-12 08:12 +0000
Re: " *first = new string(*rhs.first);" cannot compile in MSVC "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-01-12 09:50 +0100
Re: " *first = new string(*rhs.first);" cannot compile in MSVC Leigh Johnston <leigh@i42.co.uk> - 2012-01-12 12:36 +0000
Re: " *first = new string(*rhs.first);" cannot compile in MSVC "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-01-12 14:12 +0100
Re: " *first = new string(*rhs.first);" cannot compile in MSVC Leigh Johnston <leigh@i42.co.uk> - 2012-01-12 13:36 +0000
Re: " *first = new string(*rhs.first);" cannot compile in MSVC Juha Nieminen <nospam@thanks.invalid> - 2012-01-12 14:41 +0000
Re: " *first = new string(*rhs.first);" cannot compile in MSVC drew@furrfu.invalid (Drew Lawson) - 2012-01-12 14:50 +0000
Re: " *first = new string(*rhs.first);" cannot compile in MSVC Goran <goran.pusic@gmail.com> - 2012-01-11 23:12 -0800
csiph-web