Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > " *first = new string(*rhs.first);" cannot compile in MSVC

Reply
Thread Tools

" *first = new string(*rhs.first);" cannot compile in MSVC

 
 
fl
Guest
Posts: n/a
 
      01-12-2012
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:perator=(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;
}
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-12-2012
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:perator=(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?).

--
Ian Collins
 
Reply With Quote
 
 
 
 
fl
Guest
Posts: n/a
 
      01-12-2012
On Jan 11, 9:31*pm, Ian Collins <(E-Mail Removed)> 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:perator=(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?).
>
> --
> Ian Collins- Hide quoted text -
>
> - Show quoted text -


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;

class Person{
public:
Person(const string& first, const string& last, int id);
Person:erson(const Person& rhs);
const Person& Person:perator=(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
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-12-2012
On 12.01.2012 04:29, fl wrote:
> On Jan 11, 9:31 pm, Ian Collins<(E-Mail Removed)> 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:perator=(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?).
>>
>> --
>> Ian Collins- Hide quoted text -
>>
>> - Show quoted text -

>
> 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;
>
> class Person{
> public:
> Person(const string& first, const string& last, int id);
> Person:erson(const Person& rhs);
> const Person& Person:perator=(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;
> };


The code is not just technically wrong, but also totally wrong-headed.

So the question is, is it meant as an example of incorrect and
wrongheaded code, or is it meant to be OK?

Anyway, first, never place

using namespace std;

in the global namespace in a header. That's a novice error.

Then, iinstead of e.g.

string* first;

do

std::string first;

and fix all the code that refers to that.

Cheers & hth.,

- Alf
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-12-2012
On 01/12/12 04:29 PM, fl wrote:
> On Jan 11, 9:31 pm, Ian Collins<(E-Mail Removed)> 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:perator=(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:erson(const Person& rhs);
> const Person& Person:perator=(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
 
Reply With Quote
 
BCFD36
Guest
Posts: n/a
 
      01-12-2012
On 1/11/12 9:04 PM, Ian Collins wrote:
> On 01/12/12 04:29 PM, fl wrote:

[stuff deleted

>> #define PERSON_H
>>
>> #include<string>
>>
>> using namespace std;

>
> Don't ever put this in a header!
>


It would be very helpful to the self stated rookie if you stated WHY
this should not be done



>
> As Alf suggests, don't use pointers.
>


See above comment. If you have been doing this awhile, the answer is
obvious. If you are an FNG, maybe not so obvious.

--
Dave Scruggs
Sr. Software Engineer, Lockheed Martin, Sunnyvale
 
Reply With Quote
 
Goran
Guest
Posts: n/a
 
      01-12-2012
On Jan 12, 3:22*am, fl <(E-Mail Removed)> 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.


Looking at later given Person definition, it becomes clear that the
code indeed is totally wrong. In fact, it is staggeringly wrong. Not
only that, it's clear that the code has never been tried, as it can't
possibly compile.

Some of things that are wrong with it:

0. it can't compile. *first is a std::string, and new string(whatever)
is a std::string*. WTF were these people thinking!?
1. there's no good reason to use string* for first, last members. In
C, one would have used char*, true, but in C++, one would not have
used string*. string class' "raison d’ętre" is to avoid using pointers
and meddling manually with dynamic storage. So whoever wrote this
shows a staggering lack of understanding of both C and C++. As said
elsewhere, a plain string should have been used.
2. operator= is exception-unsafe. (EXTREMELY important programming
knowledge found e.g. here: http://en.wikipedia.org/wiki/Excepti...ception_safety).
"delete first" (or last) in the beginning leave the object in
undefined state: if exception is thrown by e.g. a new later on, first
and/or last point to a deleted object, and that invalidates Person
object for any later use. Correct thing to do (presuming "string*
first/last") is e.g (part of the function inside "if"):

// "can throw" zone here
auto_ptr<string> f(new string(rhs.first));
auto_ptr<string> l(new string(rhs.last));
// "can't throw" zone here
delete first;
delete last;
first = f.release();
last = l.release();

Finally, if first/last are a plain string (not string*), then this
becomes e.g.

// "can throw" zone here
string f(rhs.first);
string l(rhs.l);
// "can't throw" zone here
first.swap(f);
last.swap(l);

(Clarification: both snippets presume that Person:perator= needs so-
called "strong exception safety, which is pretty usual for operator=,
hence they make a copy of data in a throwing zone, then assign in a
non-throwing one).

Goran.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      01-12-2012
BCFD36 <(E-Mail Removed)> wrote:
> On 1/11/12 9:04 PM, Ian Collins wrote:
>> On 01/12/12 04:29 PM, fl wrote:

> [stuff deleted
>
>>> #define PERSON_H
>>>
>>> #include<string>
>>>
>>> using namespace std;

>>
>> Don't ever put this in a header!
>>

>
> It would be very helpful to the self stated rookie if you stated WHY
> this should not be done


One interesting question is where do newbies learn the magic line
"using namespace std;" from? Obviously they do not come up with it on
their own. They have to be *taught* it. Not only do they have to see it,
it must be so prominently used that they will start using it as a matter
of fact, without even thinking about it.

Who teaches newbies to use that dreadful line? Mostly pros who should
know better! They write books, they write online tutorials, they write
example code, all of which is used by newbies to learn the language.
Why are they doing this? They know better, yet they still do it.

The reason why that line should be avoided should become clear when
one thinks about it a bit: What is the purpose of namespaces? Why are
all the standard functions and types inside a namespace? (If it still
doesn't ring a bell, then: What happens when two types or functions have
the same name and parameters?)

In fact, "using namespace std;" and then avoiding the "std::" prefix
actually makes the code less readable. No longer can you spot usages of
the standard libraries with a quick visual glance and, more importantly,
usages of the more obscure standard library names becomes confusing
(because there's no visual way of distinguishing where that name is
coming from).

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-12-2012
On 12.01.2012 09:12, Juha Nieminen wrote:
> BCFD36<(E-Mail Removed)> wrote:
>> On 1/11/12 9:04 PM, Ian Collins wrote:
>>> On 01/12/12 04:29 PM, fl wrote:

>> [stuff deleted
>>
>>>> #define PERSON_H
>>>>
>>>> #include<string>
>>>>
>>>> using namespace std;
>>>
>>> Don't ever put this in a header!
>>>

>>
>> It would be very helpful to the self stated rookie if you stated WHY
>> this should not be done

>
> One interesting question is where do newbies learn the magic line
> "using namespace std;" from? Obviously they do not come up with it on
> their own. They have to be *taught* it. Not only do they have to see it,
> it must be so prominently used that they will start using it as a matter
> of fact, without even thinking about it.
>
> Who teaches newbies to use that dreadful line? Mostly pros who should
> know better! They write books, they write online tutorials, they write
> example code, all of which is used by newbies to learn the language.
> Why are they doing this? They know better, yet they still do it.
>
> The reason why that line should be avoided should become clear when
> one thinks about it a bit: What is the purpose of namespaces? Why are
> all the standard functions and types inside a namespace? (If it still
> doesn't ring a bell, then: What happens when two types or functions have
> the same name and parameters?)
>
> In fact, "using namespace std;" and then avoiding the "std::" prefix
> actually makes the code less readable. No longer can you spot usages of
> the standard libraries with a quick visual glance and, more importantly,
> usages of the more obscure standard library names becomes confusing
> (because there's no visual way of distinguishing where that name is
> coming from).


I have to disagree a bit, even though I agree with the main sentiment
about name collisions.

The C++ standard library has many names that can possibly conflict with
user code. Like `std::distance`. I think that that particular one only
bit me once, but there are plenty more.

But then, OTOH., adopting mechanical rules (and disregarding the notion
of possible intelligence on the part of the coder) in order to easily
see whether the code is using some less well known standard library
function or type, well I think that that is adding a lot of work and
pain for no real gain. Those double colons are not just more to write,
and they're not just noisy. They also cause a certain much used brain
dead editor to foul up the indentation of the currently edited text.

Anyway, I'd rather see `string` than `std::string`. Except for one
place, namely in the global namespace in a header. And that's because
when it is in the global namespace in a header, it can cause name
collisions with other code.

I guess, summing up, that I lean towards intelligence and understanding
rather than just following mechanical rules; that one should ideally
know *why* and not rely on rules designed to statistically accomplish
most of the why when followed mechanically. But when a novice is
struggling with something, simple "do like this" and "don't do that"
advice (to be followed mechanically, without understanding) can get the
person on track fast, and in a much better position to learn. So there's
a place for everything, including mechanical rule-following.

Cheers,

- Alf
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-12-2012
On 12.01.2012 13:36, Leigh Johnston wrote:
> The asymmetric nature of using 'std::' in
> header files but not in .cpp files is bogus even if name collisions are
> more likely in header files.


I think most people would agree with that, since (1) there is no general
advice to not use `std::` in .cpp files, and (2) although some people
who haven't thought about it advocate never using `using namespace std;`
in header files, competent people advocate something that's more
meaningful, namely to not use that at the global namespace level in a
header file.

In short, you're arguing against viewpoints that no-one has advocated.

One possible reason for such gross a misunderstanding could be that
those whose viewpoints you (inadvertently) misrepresented, possibly
including me, have not managed to explain things clearly enough for you?


Cheers & hth.,

- Alf
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compile error on WinXP/MSVC++E05 Jonas Schneider C Programming 1 11-30-2007 07:46 PM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
problem compile between Lapack++ & MSVC Visual.net =?iso-8859-1?B?RnLpZOlyaWMgRGVncmFldmU=?= C++ 2 10-02-2006 04:15 PM
Getting MSVC to compile proper unix stuff, like hash_map, fts.h, dirent.h collection60@hotmail.com C++ 9 05-10-2006 06:59 PM
Overloaded functions: Compile OK on MSVC++ but not on g++ user@domain.invalid C++ 4 10-21-2005 11:50 AM



Advertisments