Solved

warning C4270

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ assignment question 7 237
C++ Language error 28 249
c++ using a LPCOLESTR * and adding a string varaible to the Pointer 2 67
draw a Christmas tree by using a nested loop? 26 77
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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

830 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