Long Lines of Code, and Passing a reference to an array Question
This may not be the best way to write this:
Code:
double TempRandX = EnemyLowerBounds.x + (static_cast<double>(rand() % ((EnemyLowerBounds.x - EnemyUpperBounds.x) * 10)) * 0.1);
This is the 4th day on this project and the code is way clearer than my first prototype, believe me. Comments welcome!
I have this also:
Code:
void Enemy::SetPersonality(const int (&RefPersonality)[20])
{
for (int i = 0; i < 20; ++i)
{
Personality[i] = RefPersonality[i];
}
}
I know the above in my previous draft was this:
Code:
void Actor::SetPersonality(int *NewPersonality) {
for (size_t i = 0; i < 20; ++i) {
Personality[i] = NewPersonality[i];
}
}
The Reference solution seems more C++ ish, but it's definitely more convoluted.
Are there compelling subtleties to passing an array by reference, versus pointer?
Edit: Grr, I have to do even more casts in the top line to get it to compile. Be right back.
Edit 2:
Code:
// Random Position
double TempRandX = EnemyLowerBounds.x + (static_cast<double>(rand() % (static_cast<int>((EnemyLowerBounds.x - EnemyUpperBounds.x)) * 10)) * 0.1);
double TempRandY = EnemyLowerBounds.y + (static_cast<double>(rand() % (static_cast<int>((EnemyLowerBounds.y - EnemyUpperBounds.y)) * 10)) * 0.1);
double TempRandZ = EnemyLowerBounds.y + (static_cast<double>(rand() % (static_cast<int>((EnemyLowerBounds.y - EnemyUpperBounds.y)) * 10)) * 0.1);
TempRandZ = -28.0; // Currently all actors are locked to -28.0z
It compiles now... heck it may even work - but it's a terrible mess.
Polymorphism, or: How I Learned to Stop Worrying and Love the Bomb
In my base class <Actor>, I defined
Code:
virtual void Update(Actor &Player);
...This caused a vtable meltdown - but without me knowing what was going on, I over-reacted and hacked up a bunch of working code! Argh. So... here's the solution! Add braces!
Code:
virtual void Update(Actor &Player) {};
Original Error Messages:
Code:
obj/Debug/enemy.o||In function `~Actor':|
/storage/Projects/Recent/Mon Oct 24/actor.h|24|undefined reference to `vtable for Actor'|
obj/Debug/enemy.o:(.rodata._ZTI5Enemy[typeinfo for Enemy]+0x10)||undefined reference to `typeinfo for Actor'|
obj/Debug/main.o||In function `Actor':|
/storage/Projects/Recent/Mon Oct 24/actor.h|23|undefined reference to `vtable for Actor'|
obj/Debug/player.o:(.rodata._ZTI6Player[typeinfo for Player]+0x10)||undefined reference to `typeinfo for Actor'|
||=== Build finished: 4 errors, 0 warnings ===|
Quote:
Why?
This error is caused by declaring but not implementing one or more virtual function.
How to Fixed It?
Provide implementation to all virtual functions. If you declare the function, you MUST provide the implementation, even if it is an empty implementation. Otherwise, you will see the above error message.
Thank you Elysia, Mir, and Salem for some excellent back-and-forth discussion, encouragement, and patience. You all stand as shining examples of the effort moderators and members put into these forums.