I'm trying to understand the underlying issue in my code that is
generating the following compile-time error message:
SingleLevelLogicalUnitNumber.cpp: In member function `bool
scsi::SingleLevelLogicalUnitNumber::invariant() const':
SingleLevelLogicalUnitNumber.cpp:37: error: invalid static_cast from
type `scsi::LogicalUnitAddressingField' to type `const
scsi::PeripheralDeviceAddress&'
The code section in question is the following:
bool SingleLevelLogicalUnitNumber::invariant() const {
bool level0Ok = false;
LogicalUnitAddressingField::Method addressMethod =
getAddress(0).getMethod();
if (LogicalUnitAddressingField::PeripheralDevice == addressMethod)
{
const PeripheralDeviceAddress & theAddress = static_cast<const
PeripheralDeviceAddress &>( getAddress(0) );
if (theAddress.getBusIdentifier() == 0 ) {
level0Ok = true;
}
} else if (LogicalUnitAddressingField::FlatSpace == addressMethod)
{
level0Ok = true;
} // else it's a violation of the class invariant to have any
other address method.
bool levels123Ok = MemIsZero( &myData[2], 6 );
return level0Ok && levels123Ok;
}
LogicalUnitAddressingField is a base class (non-virtual) for
PeripheralDeviceAddress (see class definitions below) and I would
expect that this cast would be legal; what am I missing here?
Thanks,
Frank
#ifndef SCSI_LOGICALUNITADDRESSINGFIELD_H
#define SCSI_LOGICALUNITADDRESSINGFIELD_H 1
#include "services/dbc.h"
#include <stdint.h>
#ifdef __GNUC__
#if __GNUCC < 3
#include <iostream>
#else
#include <ostream>
#endif
#else // there are other compilers, right?
#include <ostream>
#endif // __GNUC__
namespace scsi {
class LogicalUnitAddressingField {
public:
enum Method {
LogicalUnit = 2, // Logical unit addressing method
PeripheralDevice = 0, // Peripheral device addressing method
FlatSpace = 1, // Flat space addressing method
ExtendedLogicalUnit = 3 // Extended logical unit addressing
method
};
/**
* Construct a default logical unit addressing field.
*/
LogicalUnitAddressingField() {
myAddress[0] = '\0';
myAddress[1] = '\0';
}
/**
* Construct a logical unit addressing field.
*/
LogicalUnitAddressingField( Method itsAddressMethod, unsigned int
itsMethodSpecificAddress ) {
myAddress[0] = static_cast<uint8_t>( (itsAddressMethod << 6) |
((itsMethodSpecificAddress >8) & 0x3f) );
myAddress[1] = static_cast<uint8_t>( itsMethodSpecificAddress
& 0xff );
REQUIRE( itsMethodSpecificAddress <= 0x3fff );
}
/**
* Construct a logical unit addressing field from two bytes of
data.
*/
LogicalUnitAddressingField( const uint8_t * itsData ) {
myAddress[0] = itsData[0];
myAddress[1] = itsData[1];
}
/**
* Destroy a logical unit addressing field.
* @preconditions invariant()
*/
~LogicalUnitAddressingField() {}
#ifndef NDEBUG
/**
* Class invariant
*/
bool invariant() const;
#endif // NDEBUG
/**
* Return my addressing method.
*/
Method getMethod() const {
return static_cast<Method>( (myAddress[0] >6) & 0x03 );
}
/**
* Return my data.
*/
const uint8_t * getData() const {
return myAddress;
}
/**
* Print the address field on the supplied ostream.
*/
void printOn( std::ostream & os ) const;
protected:
/**
* My address.
*/
uint8_t myAddress[2];
};
} // namespace scsi
#endif // SCSI_LOGICALUNITADDRESSINGFIELD_H
#ifndef SCSI_PERIPHERALDEVICEADDRESS_H
#define SCSI_PERIPHERALDEVICEADDRESS_H 1
#include "LogicalUnitAddressingField.h"
#include "services/dbc.h"
namespace scsi {
class PeripheralDeviceAddress : public LogicalUnitAddressingField {
public:
/**
* Construct a default peripheral device address.
* The default addresses LUN 0.
*/
PeripheralDeviceAddress()
: LogicalUnitAddressingField( PeripheralDevice, 0 )
{}
/**
* Construct a peripheral device address.
*/
PeripheralDeviceAddress( unsigned int itsBusIdentifier, unsigned
int itsTargetOrLun )
: LogicalUnitAddressingField( PeripheralDevice,
(itsBusIdentifier << 8) | itsTargetOrLun )
{
REQUIRE( (itsBusIdentifier < 64) && (itsTargetOrLun < 256) );
}
/**
* Destroy a peripheral device address.
* @preconditions invariant()
*/
~PeripheralDeviceAddress() {}
#ifndef NDEBUG
/**
* Class invariant
*/
bool invariant() const;
#endif // NDEBUG
/**
* Return my bus identifier.
*/
unsigned int getBusIdentifer() const {
return myAddress[0] & 0x3f;
}
};
} // namespace scsi
#endif // SCSI_PERIPHERALDEVICEADDRESS_H 2 6341
Hi fj*****@egenera.com wrote:
I'm trying to understand the underlying issue in my code that is
generating the following compile-time error message:
SingleLevelLogicalUnitNumber.cpp: In member function `bool
scsi::SingleLevelLogicalUnitNumber::invariant() const':
SingleLevelLogicalUnitNumber.cpp:37: error: invalid static_cast from
type `scsi::LogicalUnitAddressingField' to type `const
scsi::PeripheralDeviceAddress&'
The code section in question is the following:
[...]
const PeripheralDeviceAddress & theAddress = static_cast<const
PeripheralDeviceAddress &>( getAddress(0) );
The question is what the declaration of "getAddress" looks like. My guess
would be that it's:
LogicalUnitAddressingField getAddress(int/unsigned/std::size_t i)
In that case:
1. getAddress(0) is an rvalue
2. getAddress(0) is not a PeripheralDeviceAddress
1. makes the compiler complain, 2. tells you why you shouldn't even try.
Could you post the declaration of getAddress, as this is only a guess?
Markus
Markus,
thank you for the reply; should have included that in the first
place. getAddress is declared as follows:
class EightByteLogicalUnitNumber : public LogicalUnitNumber {
public:
/**
* Construct a default eight-byte logical unit number.
*/
EightByteLogicalUnitNumber() {
std::memset( myData, '\0', sizeof( myData ) );
}
/**
* Return the specified address field.
*/
LogicalUnitAddressingField getAddress( int index ) const {
REQUIRE( (index >= 0) && (index < 4) );
return LogicalUnitAddressingField( &(myData[ index * 2 ]) );
}
Thank you,
Frank This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: buds |
last post by:
Hi all,
Following is the assigment operator of a derived class
Derived& Derived::operator=(const Derived& inDerived)
{
//to assign to the base class object the following statement
...
|
by: Gary Labowitz |
last post by:
Am I doing this correctly? It is a sample program for my class.
#include <iostream>
using namespace std;
int main( )
{
int x=3, y=4;
|
by: Scott Brady Drummonds |
last post by:
Hi, everyone,
I've checked a couple of on-line resources and am unable to determine how
reinterpret_cast<> is different from static_cast<>. They both seem to
perform a compile-time casting of...
|
by: Steven T. Hatton |
last post by:
The code shown below is an example from the Coin3D documentation. I believe
the use of the C-style cast is safe under the circumstances, but from what
I've been exposed to (TC++PL(SE)), I would...
|
by: nin234ATIyahoo.com |
last post by:
Hi all
I have a logger class and facing the following issue
enum eErrLvl
{
eLOG_DEBUG_5 = 1,
eLOG_DEBUG_4,
eLOG_DEBUG_3,
eLOG_DEBUG_2,
eLOG_DEBUG_1,
|
by: Amit |
last post by:
Greetings.
I am having some problem while using a cast operation(static_cast and/or
dynamic_cast) between base and derived objects when passing to functions.
what I have is something like this..
...
|
by: Rahul |
last post by:
Hi,
I have a
class A : public B {...member functions......data members};
and am doing the following
A *p=new A();
void *p=static_cast<void *>(p);
factory_instance->process(p);
|
by: Bo Peng |
last post by:
Dear C++ experts,
I need to store and retrieve a meta information that can be int or
double. The program would be significantly simpler if I can handle two
types uniformly. Right now, I am using...
|
by: jason.cipriani |
last post by:
There have been some recent threads about casting pointers to and from
void* that have me rethinking some of my usual practices. I have a
couple of questions.
1. What is the purpose of C++'s...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |