Can't initialize class object in .h or .cpp?

Hello, everybody. I have been attempting to learn C++ recently, and I have hit a snag while learning about classes with headers. I simply wrote this program to better understand how classes and their members are defined when using a header, but for the life of me I cannot get this to compile unless I initialize the object (cZombieHead) in main(). Problem is I want to initialize the object in either things.h after the class definition or even in things.cpp and then just call that object from main() as to keep the clutter down in main(). So, can I 'legally' initialize class objects in things.h or even things.cpp and then access them from main() without getting "multiple definition" and "not defined in this scope" errors? I hope this question is clear and in the right forum. Any help would be greatly appreciated. Here is my program:
_______________________________________________________________________________
MAIN.CPP
#include <cstdlib>
#include <iostream>
#include "things.h"

using namespace std;
Things cZombieHead(3,5,200.00f); //I DO NOT WANT THIS BUT IT WORKS

int main()
{
cout << cZombieHead.mnfuncGetWeight() << endl;
cout << cZombieHead.mdfuncGetValue() << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
_______________________________________________________________________________
THINGS.H
#ifndef THINGS_H
#define THINGS_H

class Things
{
private:
int mnSize;
int mnWeight;
double mdValue;
Things(){} //private default constructor
public:
Things(int nSize, int nWeight, double dValue); //non-default constructor
void mfuncSetVals(int nSize, int nWeight, double dValue);
//getters
int mnfuncGetSize(){return mnSize;}
int mnfuncGetWeight(){return mnWeight;}
double mdfuncGetValue(){return mdValue;}
};
Things cZombieHead(3,5,200.00f); //I WANT THIS SO ALL MY CLASS OBJECTS CAN BE
//LISTED HERE INSTEAD OF IN MAIN() BUT I GET
//ERRORS WHEN TRYING TO COMPILE
#endif
_______________________________________________________________________________
THINGS.CPP
#include <iostream>
#include "things.h"

using namespace std;

Things::Things(int nSize, int nWeight, double dValue)
{
mfuncSetVals(nSize,nWeight,dValue);
}
void Things::mfuncSetVals(int nSize, int nWeight, double dValue)
{
mnSize=nSize;
mnWeight=nWeight;
mdValue=dValue;
}
_______________________________________________________________________________
I apologize if this seems trivial but since I am learning on my own it is hard
to find someone who can give real feedback on what exactly I am doing wrong.
I'm sorry but classes don't work that way. You cannot instantiate an object within its own class. A class is just a data type. You can do something like this if you had another class but then that other class will have to instantiate its own object in main() and it could be very inefficient depending on the purpose of this.

You'll need to create Thing cZombieHead(3,5,200.00f); as a part of main() or a function called by main (or globally).
Last edited on
Thank you for the reply soranz. This is exactly what I needed to know. -imsonull
Topic archived. No new replies allowed.