Ugh, I hate making so many topics lately.. but I've been reading a book and end up having a problem every 50 pages or so.
I've got two problems, anyone mind helping me with:
1.) I get an error about some code I got from a C++ programming book:
c:\programming\projects\game\v0001\bitvector.h(19) : warning C4800: 'unsigned long' : forcing value to bool 'true' or 'false' (performance warning)
Code:
class Bitvector
{
protected:
unsigned long int *m_array;
public:
Bitvector (int);
~Bitvector ();
bool operator[] (int p_index)
{
int cell = p_index / 32;
int bit = p_index % 32;
return (m_array[cell] & (1 << bit)) >> bit;
}
};
I think I understand the error, but why must the code in the book that says it compiles in MSVC6 not work, agh.. is the only way to fix it to typecast it (if that works)?
2.) I've got a problem with my code, which I will naturally blame on templates. MSVC6 errors out:
Code:
Main.obj : error LNK2001: unresolved external symbol "public: __thiscall Array<class Monster>::Array<class Monster>(int)" (??0?$Array@VMonster@@@@QAE@H@Z)
Main.obj : error LNK2001: unresolved external symbol "public: __thiscall Array<class Monster>::~Array<class Monster>(void)" (??1?$Array@VMonster@@@@QAE@XZ)
Main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Array<class Monster>::Resize(int)" (?Resize@?$Array@VMonster@@@@QAEHH@Z)
What I find interesting though is it only complains about the constructor, the deconstructor, and the resize method (top three in the .cpp file, which IS in my Project List), when I've used other methods from that class also. This is the part using the methods:
Code:
//Main.cpp (Is in the Project List)
#include "Array.h"
#include "Monster.h"
Array<Monster> MonsterArray (32);
int g_monsterCount = 0;
bool AddMonster ()
{
if (g_monsterCount == MonsterArray.Size ()) {
MonsterArray.Resize (MonsterArray.Size () + 32);
}
MonsterArray[g_monsterCount].SetX(GetRand (0, 640));
MonsterArray[g_monsterCount].SetY(GetRand (0, 640));
MonsterArray[g_monsterCount].SetHealth(11 + GetRand (0, 10));
++g_monsterCount;
return true;
}
//Array.h
template< typename Datatype >
class Array
{
private:
Datatype* m_array;
int m_size;
public:
Array (int);
~Array ();
Datatype& operator[] (int p_index) {
if (p_index >= m_size || p_index < 0) {
std::cout << "Error. The element used in the array does not exist." << std::endl;
return m_array[m_size - 1];
} else {
return m_array[p_index];
}
}
operator Datatype* () {
return m_array;
}
int Resize (int);
int Remove (int);
int Insert (Datatype, int);
void Display ();
int Size()
{
return m_size;
}
bool ReadFile (const char*);
bool WriteFile (const char*);
};
//Array.cpp (Is in the Project List)
#include "Array.h"
template< typename Datatype >
Array<Datatype>::Array (int p_sizeInitial)
{
m_array = new Datatype[p_sizeInitial];
m_size = p_sizeInitial;
}
template< typename Datatype >
Array<Datatype>::~Array ()
{
if (m_array != 0)
delete[] m_array;
m_array = 0;
}
template< typename Datatype >
int Array<Datatype>::Resize (int p_sizeChanged)
{
//removed for post
}
template< typename Datatype >
int Array<Datatype>::Insert (Datatype p_item, int p_index)
{
//removed for post
}
//Monster.h
#ifndef MONSTER_H_
#define MONSTER_H_
class Monster
{
private:
int x;
int y;
int m_health;
int m_count;
public:
Monster () : x(0), y(0), m_health(0) {}
int RetX ();
int RetY ();
void SetX (int);
void SetY (int);
int GetHealth ();
void SetHealth (int);
};
#endif //MONSTER_H_
//Monster.cpp (Is in the Project List)
#include "Monster.h"
int Monster::RetX ()
{
return x;
}
//etc etc etc
In Dev-Cpp I also get the errors, but it calls it "[Linker error] undefined reference to `Array<Monster>::~Array()'", etc.
I searched the internet and found solutions: 1) you are declaring the method in the class in .h, but not coding the method in the .cpp not the solution: I have coded it in .cpp, 2) in MSVC6 you can add the .h files under Settings->Link->Object/Library not the solution: didnt seem to work, 3) some MFC library that you need to use, but this isnt using MFC code so that solution didnt effect me.
Any help REALLY appreciated! thanks in advance,
EDIT: Array.cpp, and Array.h work stand-alone... so something else must be causing this error. Maybe the fact I am using the Monster class which is inside the Monster.h, as a parameter to the template Array class which is in Array.h, and its just getting confused?