Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

warning C4270

Posted on 1997-06-02
3
Medium Priority
?
200 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
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 200 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

824 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