473,396 Members | 2,029 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.

invalid static_cast problem

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

Nov 16 '07 #1
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

Nov 16 '07 #2
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
Nov 16 '07 #3

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

Similar topics

7
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 ...
7
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;
11
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...
26
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...
6
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,
2
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.. ...
24
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);
11
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...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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
tracyyun
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...

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.