Solved

warning C4270

Posted on 1997-06-02
3
196 Views
Last Modified: 2010-04-10
warning C4270: 'initializing' : do not initialize a non-const 'class ::String __near &' with a non-lvalue 'class ::String ' function return

This is my first test question.
I would like to know if this is a legitimate warning of if it is a bug in the compiler(MS C++ 1.52).

in book Teach Yourself C++ in 21 days ( Perry)
   there is a reference that it is a bug.
   (sorry i dont know the page)

here is the statement that gives the warning.    
    *m_pName = setName();  : warning C4270 etc.

the relevant declarations are
    String * m_pName;
    String setName();
 
the definition is

String Column::setName()
{
   String ans="dummy";
   return ans;
}

looking at the statement
   *m_pName = setName();

The String pointed to by *m_pName calls it's  operator= and copies in the contents of the String in setName(). This is precisely what I want it to do and cannot  see why the compiler has any justification for complaining.
0
Comment
Question by:cgardner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 11

Expert Comment

by:mikeblas
ID: 1163794
To get an accurate answer, you'll need to show the definition and declaration of operator=() in the String class you wrote.

.B ekiM
0
 
LVL 5

Accepted Solution

by:
yonat earned 50 total points
ID: 1163795
The declaration
String * m_pName;
will definetily cause problems, since you don't set memory space
for the String pointed by m_pName. In other words, m_pName does
not point to a valid String. Instead, use
String * m_pName = new String;
or just plain old
String m_Name;
If you use the latter, then m_Name = setName() should work.
But, like mikeblass said, it depends on the definition of
String::operator=(const String&). Form the warning, I suspect
that it is wrongly declared String::operator=(String&) - without
the const.
0
 

Author Comment

by:cgardner
ID: 1163796
Thanks, your right.. I forgot the const in operator =
which caused the C4270

also I forgot to initialize..  Bonus


 

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question