type case problem

A

amit

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
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

amit said:
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
 
J

Jay Nabonne

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
 
A

amit

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,
 
A

amit

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.cpp(135) : error C2059:
syntax error : ')'
 
J

Jay Nabonne

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
 
A

amit

Sorry, about your question:

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

Jay Nabonne

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
 
A

amit

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);
}
 
J

James Lothian

amit said:
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
 
J

Jay Nabonne

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top