LNK2001 error
Apr 18, 2010 at 4:37am UTC
I've tried to compile this code but received the same error - LNK2001. I've gone over my code but still, I can't find what may cause the error. So can anyone plz point it out for me?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include "Payroll.h"
#include <vector>
using std::vector;
class CtrBrkReport
{public :
static vector <Payroll> aDept;
static vector <Payroll> hDept;
static vector <Payroll> mDept;
static vector <Payroll> qDept;
static vector <Payroll> sDept;
Payroll temp;
CtrBrkReport::CtrBrkReport(ifstream &file1, ifstream &file2): temp(Payroll(file1,file2))
{
switch (temp.getDept())
{
case 'A' : aDept.push_back(temp);
cout << "Add Accout. " << endl;
break ;
case 'H' : hDept.push_back(temp);
cout << "Add Shipp. " << endl;
break ;
case 'M' : mDept.push_back(temp);
cout << "Add Manufac. " << endl;
break ;
case 'Q' : qDept.push_back(temp);
cout << "Add Quality. " << endl;
break ;
case 'S' : sDept.push_back(temp);
cout << "Add Sales. " << endl;
break ;
default : break ;
}
}
~CtrBrkReport();
};
And here's the declaration for Payroll class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
// Payroll class declaration
#ifndef PAYROLL_H
#define PAYROLL_H
#include <fstream>
using std::ifstream;
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::left;
using std::right;
using std::setw;
using std::setprecision;
using std::fixed;
class Payroll
{
struct Info
{
unsigned int id;
char last[15];
char first[15];
double wage;
double hour;
char dept;
double percentChange;
bool married;
unsigned int depend;
bool ins;
}info;
struct Deduction
{
double fica;
double fed;
double state;
double insurance;
}deduction;
double gross;
double netpay;
void calcGross();
void calcFICA();
double getDeduction(double single, double married);
void calcFedTax();
void calcStateTax();
void calcInsurance();
void calcNetpay();
public :
Payroll();
Payroll(ifstream &, ifstream &);
void printReport();
char getDept();
};
#endif
Apr 18, 2010 at 4:43am UTC
Please tell us what the error message is.
"LNK2001" isn't very descriptive
Apr 18, 2010 at 4:46am UTC
1>main.obj : error LNK2001: unresolved external symbol "public: static class std::vector<class Payroll,class std::allocator<class Payroll> > CtrBrkReport::qDept" (?qDept@CtrBrkReport@@2V?$vector@VPayroll@@V?$allocator@VPayroll@@@std@@@std@@A)
That's what it said.
Apr 18, 2010 at 4:55am UTC
For static data members, you have to basically declare them twice.
1) Once to declare them
2) And again to instantiate them
You are doing #1 with the following:
1 2 3 4 5
class CtrBrkReport
{public :
static vector <Payroll> aDept;
static vector <Payroll> hDept;
// ...
In order to do #2, you also need to put these vectors in
exactly one source file in order for them to be instantiated.
At the top of your cpp file for CtrBrkReport, put the following:
1 2 3 4 5
#include //... your includes here
vector<Payroll> CtrBrkReport::aDept;
vector<Payroll> CtrBrkReport::hDept;
// ...
Once you do this, that error will disappear.
It's kind of like how you need to define both a function prototype and a function body. The 'static' declaration is the prototype, but you need to give the variable a body by instantiating it in a cpp file.
Last edited on Apr 18, 2010 at 4:56am UTC
Apr 18, 2010 at 5:01am UTC
Oh i see. I'll try it. Thank you.
Apr 18, 2010 at 5:06am UTC
OMG oMG!! it runs! thank you so much!!!!!!!!!!!!
Topic archived. No new replies allowed.