0

I'm getting the following error and I don't know how to fix it:

Error 1 error LNK2001: unresolved external symbol "public: static int HotDogStand::DogCounter" (?DogCounter@HotDogStand@@2HA) CartTesting.obj CartTesting

I'm trying to use the HotDogStand::JustSold() function to increment the DogCounter variable by 1, or none (depending on user entry). I think my error may lie in the fact that I don't know how to properly import the DogCounter...

#include <iostream>
#include <fstream>
using namespace std;

class HotDogStand
{
public:
		void JustSold();
		int Cart;
		int IdNumber;
		int SalesToday;
		int TotalSales;
		int IncrementalSale;
		static int DogCounter;
};

int main()
{
		int numCarts;
		static int DogCounter;

		HotDogStand HotDogSales;

		cout << "Please Enter The Number of Carts: ";
		cin >> numCarts;

		HotDogStand * CartSales = new HotDogStand[numCarts];
		DogCounter = 0;
		
		for(int x=0; x<numCarts; x++)
		{
				cout << "ID of Cart #" << x+1 << ": ";
				cin >> CartSales[x].Cart;
				for (int y=1;y<numCarts; y++)
				{
					cout << "How many Hot Dogs Did Cart # " << CartSales[x].Cart << " sell?"
						<< endl;
					cin >> CartSales[y].Cart;
					DogCounter += CartSales[y].Cart;
				}
		}
		cout << endl;
		cout << "Total Hot Dogs Sold Thus Far = " << DogCounter << endl;
		cout << endl;

		HotDogSales.JustSold();

		return 0;
}

void HotDogStand::JustSold()
{
	int Answer;
	int JustSoldCounter;

	cout << "Did Your Carts Sell a Hot Dog?" << endl;
	cout << "Enter 1 for Yes, or 2 for No" << endl;
	cin >> Answer;

	JustSoldCounter = 0;

	if (Answer == 1)
	{
		JustSoldCounter += 1;
		DogCounter ++ ;
		cout << JustSoldCounter;
	}
	else if (Answer == 2)
	{
		JustSoldCounter += 0;
		cout << JustSoldCounter;
	}
	else
		cout << "Error!" << endl;
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by mitrmkar
1

When you have a static non-const member variable, it also needs definition outside the class declaration, so you need to have it like

<snip>
class HotDogStand
{
public:
		void JustSold();
		int Cart;
		int IdNumber;
		int SalesToday;
		int TotalSales;
		int IncrementalSale;
		static int DogCounter;
};

// definition here, outside the class
int HotDogStand::DogCounter = 0;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.