473,396 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

type case problem



Hello group,

In a method of my class I have below problem in terms of type case. Can
somebody please give me insight into this?

This function returns a pointer into the array at the specified index.
Please know that the array containing image data.
void* CImageArray::ElementIndex( long iIndex) const
{
return ((unsigned char*) (void*)m_elements) + ( iIndex *
m_ElementSize);

}

Consider that m_element has a type of CMemoryBlock.

**************** Error message ***************

error C2440: 'type cast' : cannot convert from 'const class
CMemoryBlock' to 'void *'
No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called
Error executing cl.exe.



Your help will be appreciated greatly.
Thanks,
amit

Jul 23 '05 #1
14 1808
make the m_elements mutable using the mutable keyword.

Raj

Jul 23 '05 #2
amit wrote:
In a method of my class I have below problem in terms of type case.
I think you mean type "cast" here.
This function returns a pointer into the array at the specified index.
Please know that the array containing image data.
void* CImageArray::ElementIndex( long iIndex) const
{
return ((unsigned char*) (void*)m_elements) + ( iIndex *
m_ElementSize);

}
Don't use C-style casts in C++. You don't need them.
Consider that m_element has a type of CMemoryBlock.

**************** Error message ***************

error C2440: 'type cast' : cannot convert from 'const class
CMemoryBlock' to 'void *'


This is because, the ElementIndex function is const, meaning that the
members of CImageArray are non-mutable in this function. You cannot return
'void*' to constant data.

You have two options:

1) Make the function non-const: remove the 'const' at the end

2) Return pointer-to-const: probably 'void const *'

In any case, use one of the C++ type conversion operators. In this case,
reinterpret_cast:

void const * CImageArray::ElementIndex(size_t iIndex) const
{
return reinterpret_cast<unsigned char const *>(m_elements) + iIndex *
m_ElementSize;
}

Ali

Jul 23 '05 #3
On Thu, 14 Apr 2005 10:22:21 -0700, amit wrote:


Hello group,

In a method of my class I have below problem in terms of type case. Can
somebody please give me insight into this?

This function returns a pointer into the array at the specified index.
Please know that the array containing image data.
void* CImageArray::ElementIndex( long iIndex) const
{
return ((unsigned char*) (void*)m_elements) + ( iIndex *
m_ElementSize);

}

Consider that m_element has a type of CMemoryBlock.

**************** Error message ***************

error C2440: 'type cast' : cannot convert from 'const class
CMemoryBlock' to 'void *'
No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called
Error executing cl.exe.


CMemoryBlock is an object, correct? What is your expectation as far as teh
result of casting this object to a pointer? (What should it point to?)

- Jay
Jul 23 '05 #4

I'm going to load a TIF file into an array but every TIF file could
have several pages. CImageArray is a type that I can instantiate an
object for array and CMemoryBlock is a class which should give me
methods like
free(), alloc() and realloc().

Please consider that the application I'm working on is based on libtiff
which is written in C Ansi.

Please let me know if you have any suggestions.

Thanks,
Amit,

Jay Nabonne wrote:
On Thu, 14 Apr 2005 10:22:21 -0700, amit wrote:


Hello group,

In a method of my class I have below problem in terms of type case. Can somebody please give me insight into this?

This function returns a pointer into the array at the specified index. Please know that the array containing image data.
void* CImageArray::ElementIndex( long iIndex) const
{
return ((unsigned char*) (void*)m_elements) + ( iIndex *
m_ElementSize);

}

Consider that m_element has a type of CMemoryBlock.

**************** Error message ***************

error C2440: 'type cast' : cannot convert from 'const class
CMemoryBlock' to 'void *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.
CMemoryBlock is an object, correct? What is your expectation as far

as teh result of casting this object to a pointer? (What should it point to?)
- Jay


Jul 23 '05 #5

No, mutable didn't hlep. Thanks anyway for your response.

Jul 23 '05 #6

No, mutable didn't hlep. Thanks anyway for your response.

Jul 23 '05 #7

I tried all steps 1 ~ 3 but still complains.

I thought solution 3 would work but it turns out with this:

error C2440: 'reinterpret_cast' : cannot convert from 'class
suMemBlock' to 'unsigned char'
Conversion requires a constructor or user-defined-conversion
operator, which can't be used by const_cast or reinterpret_cast
C:\Development\testing\faxlibconsole\ImageArray.cp p(135) : error C2059:
syntax error : ')'

Jul 23 '05 #8
On Thu, 14 Apr 2005 12:10:09 -0700, amit wrote:

I'm going to load a TIF file into an array but every TIF file could
have several pages. CImageArray is a type that I can instantiate an
object for array and CMemoryBlock is a class which should give me
methods like
free(), alloc() and realloc().

Please consider that the application I'm working on is based on libtiff
which is written in C Ansi.

Please let me know if you have any suggestions.


Then I suspect you need to call some of the methods on the object.

Or at least answer my question about what you expected the cast of the
object to a pointer to accomplish. :)

- Jay
Jul 23 '05 #9

Sorry, about your question:

It should return a pointer into the array at the specified index.

Jay Nabonne wrote:
On Thu, 14 Apr 2005 12:10:09 -0700, amit wrote:

I'm going to load a TIF file into an array but every TIF file could
have several pages. CImageArray is a type that I can instantiate an
object for array and CMemoryBlock is a class which should give me
methods like
free(), alloc() and realloc().

Please consider that the application I'm working on is based on libtiff which is written in C Ansi.

Please let me know if you have any suggestions.

Then I suspect you need to call some of the methods on the object.

Or at least answer my question about what you expected the cast of

the object to a pointer to accomplish. :)

- Jay


Jul 23 '05 #10
>>
Then I suspect you need to call some of the methods on the object.

Or at least answer my question about what you expected the cast of the
object to a pointer to accomplish. :)


Sorry, about your question:

It should return a pointer into the array at the specified index.


Does it have a method for accessing the data it contains? If so, you would
probably want to use that.

- Jay
Jul 23 '05 #11

Exactly.

This is the code:

As you will see function elementPtr() is returning a pointer to an
elemnt
of array. It has the index of the element * its size (m_elementSize) +
m_element which is from class CMemoryBlock.

I don't know why it complains for type cast ? Anyidea?
I will really appreciate it.

class CImageArrray {

public:

CImageArrray(unsigned long inElementSize = 0, long inBlockSize =
10);
virtual ~CImageArrray(void);

long numElements(void) const;
bool isEmpty(void) const;
unsigned long elementSize(void) const;
long blockSize(void) const;

etc ...
etc ...
private:

etc ...
protected:
// Instance variables:
CMemoryBlock m_elements; // Buffer of array items
etc ...
};

class CMemoryBlock {

public:

CMemoryBlock(unsigned long inSize = 0, bool inClearMem = false);
virtual ~CMemoryBlock(void);

etc ...
private:

};


void* CImageArrray::elementPtr(long inIndex) const
{

return ((unsigned char*) (void*) m_elements) + (inIndex *
m_elementSize);
}

Jay Nabonne wrote:

Then I suspect you need to call some of the methods on the object.

Or at least answer my question about what you expected the cast of the
object to a pointer to accomplish. :)


Sorry, about your question:

It should return a pointer into the array at the specified index.


Does it have a method for accessing the data it contains? If so, you

would probably want to use that.

- Jay


Jul 23 '05 #12
amit wrote:
Exactly.

This is the code:

As you will see function elementPtr() is returning a pointer to an
elemnt
of array. It has the index of the element * its size (m_elementSize) +
m_element which is from class CMemoryBlock.

I don't know why it complains for type cast ? Anyidea?
I will really appreciate it.

class CImageArrray {

public:

CImageArrray(unsigned long inElementSize = 0, long inBlockSize =
10);
virtual ~CImageArrray(void);

long numElements(void) const;
bool isEmpty(void) const;
unsigned long elementSize(void) const;
long blockSize(void) const;

etc ...
etc ...
private:

etc ...
protected:
// Instance variables:
CMemoryBlock m_elements; // Buffer of array items
etc ...
};

class CMemoryBlock {

public:

CMemoryBlock(unsigned long inSize = 0, bool inClearMem = false);
virtual ~CMemoryBlock(void);

etc ...
private:

};


void* CImageArrray::elementPtr(long inIndex) const
{

return ((unsigned char*) (void*) m_elements) + (inIndex *
m_elementSize);
}

What Jay has been trying to tell you is that you can't cast an object
(m_elements, of type CMemoryBlock) to a pointer to that object. You need
to use the & (address-of) operator, or something similar.

James
Jul 23 '05 #13
On Fri, 15 Apr 2005 11:21:11 +0100, James Lothian wrote:
amit wrote:


class CMemoryBlock {

public:

CMemoryBlock(unsigned long inSize = 0, bool inClearMem = false);
virtual ~CMemoryBlock(void);

etc ...
private:

};
void* CImageArrray::elementPtr(long inIndex) const
{

return ((unsigned char*) (void*) m_elements) + (inIndex *
m_elementSize);
}

What Jay has been trying to tell you is that you can't cast an object
(m_elements, of type CMemoryBlock) to a pointer to that object. You need
to use the & (address-of) operator, or something similar.


And even taking address-of isn't going to do it. Amit needs to get a
pointer to the memory *contained within* the CMemoryBlock object. Taking
the address of the object will just give you a pointer to the beginning of
the object, with its vtable pointer, etc.

There *must* be a method of CMemoryBlock that returns a pointer to the
memory it has internally allocated.

Amit, if you provide a more complete definition of that class, it should
become clear. We're still just guessing at this point.

- Jay

Jul 23 '05 #14

Hi Jame,

that's the thing. I did it already but didn't work.
Thanks

Jul 23 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
4
by: wkaras | last post by:
I would like to propose the following changes to the C++ Standard, the goal of which are to provide an improved ability to specify the constraints on type parameters to templates. Let me say from...
16
by: David Ford | last post by:
I have a macro that I use across the board for freeing ram. I'd like to clean up my code so I don't get these warnings. #define sfree(x) _internal_sfree((void **)&x) #define _internal_sfree(x)...
0
by: Nashat Wanly | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui06032003.asp Don't Lock Type Objects! Why Lock(typeof(ClassName)) or SyncLock GetType(ClassName) Is Bad Rico...
12
by: Joel Byrd | last post by:
I'm having a little problem with using type-ahead functionality for an auto-suggest box. Sometimes, when I start to type something and the type-ahead shows up, the AJAX will send a request query...
13
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only...
10
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize...
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
17
by: The Frog | last post by:
Hello everyone, I am working on an application that can build database objects in MS Access from text files. I suppose you could call it a backup and restore type routine. Accessing the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.