Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > Visual C++ 2005
|
Visual C++ 2005 For discussion of Visual C++ 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 14th, 2007, 09:20 PM
Registered User
 
Join Date: Nov 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default I don't know where is the problem...

I wrote a Class named "Mystring", its head file like following:
#pragma once
class Mystring
{
private:
char* m_ptext;
int m_length;

public:
Mystring(const char*,int);
~Mystring(void);
Mystring(const Mystring&);
int messurelength(void);
}

The cpp file like follwing:
#include <iostream>
#include "Mystring.h"

Mystring::Mystring(const char* ptext="error",int length=5){
m_ptext=new char[length];
int count=0;
while(ptext[count]!='\0') count++;
if(length>=count) *m_ptext=*ptext;
else throw "error";
}

Mystring::Mystring(const Mystring& source){
delete[] m_ptext;
[u]char* m_ptext=new char[source.messurelength]; </u> //line 15
*m_ptext=*(source.m_ptext);
m_length=source.m_length;
}

Mystring::~Mystring(){
delete[] m_ptext;
}

int Mystring::messurelength(void){
int count=0;
while(m_ptext[count]!='\0') count++;
return count;
}

when I compiled it, a warning came out. it said:
"mystring.cpp(15) : errorC2662: 'Mystring::messurelength' : cannot convert 'this' pointer from 'const Mystring' to 'Mystring &' Conversion loses qualifiers"

I don't know where is the error, how to handle this?

thanks for any suggestions.


 
Old May 12th, 2008, 03:27 AM
Authorized User
 
Join Date: Mar 2008
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

this is a very simple problem,variable m_ptext has been defined in class Mystring.when you are going to give a value to it, you should not using char* anymore.directly :m_ptext=new char[xxxx.length];thats all.

fight now,date in future.









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.