473,406 Members | 2,698 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,406 software developers and data experts.

Problem casting from a vector

Hi,

I have a vector (myVector) of BaseClass objects. I store in it a
DerivedClass object (where DerivedClass inherits from BaseClass). Later
I want to get the last element in the vector and store it in a
DerivedClass variable, doing:

DerivedClass d =
(DerivedClass)myVector[(int)dataTablesVector.size()-1];

this gives me the following error:

error C2440: 'type cast' : cannot convert from
'std::allocator<_Ty>::value_type' to 'DerivedClass'
with
[
_Ty=BaseClass *
]
Where am I going wrong?

Thanks.

Jul 23 '05 #1
3 2539
DJ*********@gmail.com wrote:
Hi,

I have a vector (myVector) of BaseClass objects. I store in it a
DerivedClass object (where DerivedClass inherits from BaseClass). Later
I want to get the last element in the vector and store it in a
DerivedClass variable, doing:
That doesn't make much sense. If it is a BaseClass variable, how would you
copy its contents into a DerivedClass one? What would the myVector of the
copy be initialized to?
DerivedClass d =
(DerivedClass)myVector[(int)dataTablesVector.size()-1];
Don't use C style casts. They are a big no-no in C++.
this gives me the following error:

error C2440: 'type cast' : cannot convert from
'std::allocator<_Ty>::value_type' to 'DerivedClass'
with
[
_Ty=BaseClass *
]
Where am I going wrong?


You are trying to copy a BaseClass object into a DerivedClass one. That's
only possible if you declare a conversion constructor or conversion
operator that does this. But I don't see how that could work. Maybe you
actually want a reference?
Jul 23 '05 #2

<DJ*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi,

I have a vector (myVector) of BaseClass objects. I store in it a
DerivedClass object (where DerivedClass inherits from BaseClass). Later
I want to get the last element in the vector and store it in a
DerivedClass variable, doing:

DerivedClass d =
(DerivedClass)myVector[(int)dataTablesVector.size()-1];

this gives me the following error:

error C2440: 'type cast' : cannot convert from
'std::allocator<_Ty>::value_type' to 'DerivedClass'
with
[
_Ty=BaseClass *
]
Where am I going wrong?


Forget the code. The logic doesn't make sense. Why are you storing base
class objects in a Derived class? Can you store a vector of vehicles in a
car? Would you store a vector of shapes in a triangle? Why would you store
Animals in a Chicken?

Inheritence only works when a strict relationship exists between the base
and its derivative. Its an exceptional relationship.

Storing a type in a class is called composition. A Car has a motor. A truck
has a vector of wheels. An airplane has a landing gear. A building has a
vector of floors. A Ferry can hold vehicles, etc...

Also, as already mentioned to you, casting is not guesswork in C++. This is
a language with strong type checking which extends to its casting mechanisms
(static_cast, dynamic_cast, etc). Casting from a base class to a derived
class implies that the object is a derived object to begin with.

Jul 23 '05 #3
Peter Julian wrote:
I have a vector (myVector) of BaseClass objects. I store in it a
DerivedClass object (where DerivedClass inherits from BaseClass). Later
I want to get the last element in the vector and store it in a
DerivedClass variable, doing:

DerivedClass d =
(DerivedClass)myVector[(int)dataTablesVector.size()-1];

this gives me the following error:

error C2440: 'type cast' : cannot convert from
'std::allocator<_Ty>::value_type' to 'DerivedClass'
with
[
_Ty=BaseClass *
]
Where am I going wrong?

Forget the code. The logic doesn't make sense. Why are you storing base
class objects in a Derived class? Can you store a vector of vehicles in a
car?


Well, there are trucks that are used to transport cars.
Would you store a vector of shapes in a triangle?


Not in a triangle, but what about a compound object that is composed of
several simple shapes? Most 2D drawing programs offer something like that.

This structure is also called the composite design pattern, and it's not so
uncommon.

Jul 23 '05 #4

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

Similar topics

5
by: Jason Heyes | last post by:
I didn't want to repeat the same code so I casted to const. Here is a simplified example of when casting to const can help avoid code duplication: class MyClass { int numbers; public: //...
21
by: Makhno | last post by:
Hello, Why does my cast from Vector<class Float> to Vector<float> not work? It won't compile, template<class Float> class Vector { public: Vector(Float x1,Float y1,Float...
14
by: laniik | last post by:
Hi. Im just wondering what the syntax to overload a casting operator is. For example, i have struct Point { float x,y,z; }; struct Vector { float a,b,c; };
1
by: DJ.precario | last post by:
Hi, I have a vector (myVector) of BaseClass objects. I store in it a DerivedClass object (where DerivedClass inherits from BaseClass). Later I want to get the last element in the vector and...
17
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
1
by: Herby | last post by:
Can generic types be casted? say Vector<Type^>^ to Vector<CodeType^>^ Where CodeType is a derivative of Type Is this possible ? Or is there something quite static about generics?
4
by: propokergrad | last post by:
Hello, say I have two classes: class Base{...}; class Derived : public Base{...} I would like to do something similar to this: std::vector<Base*>::iterator b;...
10
by: Dom Jackson | last post by:
I have a program which crashes when: 1 - I use static_cast to turn a base type pointer into a pointer to a derived type 2 - I use this new pointer to call a function in an object of the...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.