By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,874 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

type case problem

P: n/a


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
Share this Question
Share on Google+
14 Replies


P: n/a
make the m_elements mutable using the mutable keyword.

Raj

Jul 23 '05 #2

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a

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

Jul 23 '05 #6

P: n/a

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

Jul 23 '05 #7

P: n/a

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

P: n/a
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

P: n/a

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

P: n/a
>>
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a

Hi Jame,

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

Jul 23 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.