Can someone please help me with this error I am getting when compiling:

DivSales.obj : error LNK2001: unresolved external symbol "private: static int DivSales::totalSales" (?totalSales@DivSales@@0HA)
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\...\Week 3 - Challenge 7\Debug\Week 3 - Challenge 7.exe : fatal error LNK1120: 2 unresolved externals


Here is my code:

DivSales.h:

class DivSales
{
public:
	DivSales();
	void addTotal(int a, int b, int c, int d);
	int printValue(const int t = 2);
	int quartSales[4];
private:
	static int totalSales;
};

DivSales.cpp

#include "DivSales.h"
#include <iostream>
using namespace std;


void DivSales::addTotal(int a, int b, int c, int d)
{
	cout << "Enter 1st Quarter:" << endl;
	cin >> a;
	cout << "Enter 2nd Quarter:" << endl;
	cin >> b;
	cout << "Enter 3rd Quarter:" << endl;
	cin >> c;
	cout << "Enter 4th Quarter:" << endl;
	cin >> d;

	quartSales[0] = a;
	quartSales[1] = b;
	quartSales[2] = c;
	quartSales[3] = d;
	totalSales = (a+b+c+d);
}

int DivSales::printValue(const int t)
{
		return quartSales[t];
}

RunDivSales.cpp

#include "DivSales.h"
#include <iostream>
using namespace std;


int main(void)
{
	DivSales divSales;

	divSales.addTotal();
	divSales.printValue()

	cin.get();
}

Thank you for the help.

static class data objects also have to be declared like normal globals.

#include "DivSales.h"
#include <iostream>
using namespace std;

DivSales::totalSales = 0;

void DivSales::addTotal(int a, int b, int c, int d)
{
	cout << "Enter 1st Quarter:" << endl;
	cin >> a;
	cout << "Enter 2nd Quarter:" << endl;
	cin >> b;
	cout << "Enter 3rd Quarter:" << endl;
	cin >> c;
	cout << "Enter 4th Quarter:" << endl;
	cin >> d;

	quartSales[0] = a;
	quartSales[1] = b;
	quartSales[2] = c;
	quartSales[3] = d;
	totalSales = (a+b+c+d);
}

Little correction to AD's post,

int DivSales::totalSales;

To Wolf CCMLG,
Take a look at static members
SUMMARY:

In fact, static members have the same properties as global variables but they enjoy class scope. For that reason, and to avoid them to be declared several times, we can only include the prototype (its declaration) in the class declaration but not its definition (its initialization). In order to initialize a static data-member we must include a formal definition outside the class, in the global scope, as in the previous example.

Thanks you guys. I am getting one more error:

1>\week 3 - challenge 7\rundivsales.cpp(10) : error C2065: 'a' : undeclared identifier
1>\week 3 - challenge 7\rundivsales.cpp(10) : error C2065: 'b' : undeclared identifier
1>\week 3 - challenge 7\rundivsales.cpp(10) : error C2065: 'c' : undeclared identifier
1>\week 3 - challenge 7\rundivsales.cpp(10) : error C2065: 'd' : undeclared identifier

I don't understand why I am getting this error. addTotal() takes four int arguments a,b,c,d but I don't know why it acts like they are undeclared. :confused:

Code with Error:

int main(void)
{
	DivSales divSales;

	divSales.addTotal(a,b,c,d);
	divSales.printValue();

	cin.get();
}


void addTotal()

#include "DivSales.h"
#include <iostream>
using namespace std;

int DivSales::totalSales;

void DivSales::addTotal(int a, int b, int c, int d)
{
	cout << "Enter 1st Quarter:" << endl;
	cin >> a;
	cout << "Enter 2nd Quarter:" << endl;
	cin >> b;
	cout << "Enter 3rd Quarter:" << endl;
	cin >> c;
	cout << "Enter 4th Quarter:" << endl;
	cin >> d;

	quartSales[0] = a;
	quartSales[1] = b;
	quartSales[2] = c;
	quartSales[3] = d;
	totalSales = (a+b+c+d);
}

int DivSales::printValue(const int t)
{
		return quartSales[t];
}

Your compiler isn't lying. You didn't delcare a, b,c or d here:

int main(void)
{
	DivSales divSales;
	divSales.addTotal(a,b,c,d); // <--- here
       [....]

You should call the function with either numbers, or declare a,b,c,d:

int main(void)
{
	DivSales divSales;
	divSales.addTotal(1,2,3,4); // with numbers
	divSales.printValue();
	cin.get();
}

or

int main(void)
{
	DivSales divSales;
        int a = 1, b = 2, c = 3, d = 4; // declare some variables
	divSales.addTotal(a,b,c,d); // pass them to function
	divSales.printValue();
	cin.get();
}

Thank you everyone, I got it working. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.