Yes you will get C2011: 'Object' : 'class' type redefinition error.
This is because main.cpp line 1 - includes interger.cpp which itself includes object.cpp.
and on line 2 there is include object.cpp
So object cpp gets included twice which means that class Object is seen twice by the compiler - hence the error.
The way you are including cpp files is not the recommended way of doing things.
Class declarations are usually put into header files. The code for the class is put into the cpp file.
The header file should have a header guard to prevent multiple inclusion errors.
Example of header guard
object.h
1 2 3 4 5 6 7 8
//header guard at start of header file
#ifndef OBJECT_H
#define OBJECT_H
class Object
{
};
//End guard at bottom of header file
#endif
Do something similar for the other class declaration files;
integer.h
1 2 3 4 5 6 7 8
#ifndef INTEGER_H
#define INTEGER_H
#include "Object.h"
class Integer : public Object
{
};
#endif
boolean.h
1 2 3 4 5 6 7 8 9
#ifndef BOOLEAN_H
#define BOOLEAN_H
#include "Object.h"
class Boolean : public Object
{
};
#endif
Then in main.cpp you will have:
1 2 3 4 5 6 7 8 9 10
#include "Integer.h"
#include "Object.h" //probably won't need this line
#include boolean.h
int main()
{
Integer i;
Boolean b;
return 0;
}
Hello,
I am facing a tye cast problem, but logically it seems right.
Modifying Integer.cpp
1 2 3 4 5 6 7 8 9 10 11 12
class Integer : public Object
{
Object& getElementAt(int index);
};
Object& Integer::getElementAt(int index)
{
return // some process to return reference to Object
}
Modifying Main.cpp
1 2 3 4 5 6
int main()
{
Integer i;
Boolean b=(Boolean)i.getElementAt(0); // giving compile time error
return 0;
}
The error given is :-
'type cast' : cannot convert from 'class Object' to 'class Boolean'
An Object is been returned which is just type casted to Child-class(Boolean).
Yes, this cast only works in very limited cases and there are trivial ways to modify Integer and/or Boolean that would cause this code to fail eventually.
Without stepping onto the design soapbox, perhaps one solution would be to write some casting constructors:
1 2 3 4 5 6 7
class Integer : public Object {
explicit Integer( const Boolean& b );
};
class Boolean : public Object {
explicit Boolean( const Integer& i );
};
In Vector, you declare an array of Objects by pointer: Object* data;.
When you new Object[100], you are allocating memory for 100 Objects. Object is an empty class.
Now in addElement, you take an object passed by reference and put it into the array. The array holds only Objects, however. This means that the compiler is "splicing" your parameter into two pieces -- the Object part and "the "rest" -- and it is copying ONLY THE Object PART into the array.
You have essentially lost the derived portion of your object at this point. This, of course, then makes your static_cast on line 9 in Mainc.cpp invalid.
This is not going to be an easy thing to fix. However, I will make one suggestion to you (kind of related):
Use dynamic_cast<> instead of static_cast<> for polymorphic "downcasting". This will actually cause the compiler to generate code to ensure the cast is legal. Had you done this on the above code, an exception would have been thrown on line 9.
(Note: if you dynamic_cast to a pointer, NULL is returned if the cast fails. if you dynamic_cast to a reference, an exception is thrown on failure instead).
"This means that the compiler is "splicing" your parameter into two pieces -- the Object part and "the "rest" -- and it is copying ONLY THE Object PART into the array." -->
This means that i have to make an array for holding references to objects.
But a bit of look here and there...got to know that creating array for holding references is not possible in c++.