Solved

warning C4270

Posted on 1997-06-02
3
179 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 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now