Hello all you C++ fanatics. I've been (very) slowly learning C++ for a while now, and I thought I'd play about with classes, class constructors/destructors and linked lists. I'm not sure if I've done this the right way or not, but I'm sure you'll be able to advise me ;)
This code will compile (was quite shocked cos it compiled almost forst time... almost) but I get errors, 'unresolved external symbol' at link-time. Any ideas why?
There's probably some big glaring error in the code, I hope I'm not gonna look like an idiot ;)
Code:#include <iostream>
using namespace std;
class CValue
{
private:
long *value;
CValue *next;
CValue *prev;
static int numVals; //number of objects instantiated
static CValue *first; //first object in list
static CValue *last; //last object in list
public:
//a few basic functions...
int hexVal()
{ cout << hex << *value << endl;
return 0;
}
int decVal()
{ cout << dec << *value << endl;
return 0;
}
int octVal()
{ cout << oct << *value << endl;
return 0;
}
//some functions to access private member variables...
int howMany()
{ cout << "There are " << numVals
<< " CValue objects in existence."
<< endl;
return numVals;
}
static CValue* getFirst()
{ return first;
}
static CValue* getLast()
{ return last;
}
CValue* getNext()
{ return next;
}
CValue* getPrev()
{ return prev;
}
//class constructor
CValue(long x = 255)
{ value = new long(x);
if(numVals)
{ first = this;
last = this;
next = this;
prev = this;
}
else
{ prev = last;
next = first;
last = this;
}
numVals++;
}
//class destructor
~CValue()
{ next->prev = prev;
prev->next = next;
numVals--;
if(!numVals)
first = last = prev = next = NULL;
delete value;
}
};
int main()
{ //make a few instances of CValue
CValue firstVal;
CValue secondVal(100);
CValue arrayOfVals[5];
CValue* current = CValue::getFirst();
CValue* last = CValue::getLast();
//print out a list of decimal values, one for each object
if(current)
do
{ current->decVal();
current = current->getNext();
} while(current && current != last);
return 0;
}
Tadaaa.. any offers?
