I am have to create a program for my class. The instructions are to allow a user to input an integer and display the month and day that corresponds to the entered number. For example, user enters 32 and it will display February 1. I must create a DayOfYear class and create a function called print() to display the output message. I am a little lost and need some help. I compile my program and I get the following error message:

dayofyear.cpp(54) : error C2660: 'DayOfYear::print' : function does not take 1 arguments

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


//Day of the year class declaration
class DayOfYear
{
private:
	int day;

public:
	static const int MonthDays[];
	static const string MonthName[];
	void print();
};

//**************************************************
// This function displays the month and day using  *
// the number entered.                             *
//**************************************************

void DayOfYear::print()
{
	int month = 0;

	while (DayOfYear::MonthDays[month] < day)
		month = (month + 1) %12;

		//Display month and day
		cout << DayOfYear::MonthName[month] << " " << day - DayOfYear::MonthDays[month-1];
};


int main()
{
	//Set days of each month into an array
	const int MonthDays[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
	//Set the name of each month into an array
	const string MonthName[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	
	int day;
	//Ask user the total day number
	cout << "\nEnter a number you would like to convert into a month and day";
	cin >> day;

	//Error check for negative numbers and numbers higher than one year
	if(day <= 0 || day > 365)
	{
		cout << "You must enter a valid number (1 thru 365)" << endl;
	}

	//Send entered day number to the print() function
	DayOfYear::print(day);
	return 0;

}

Recommended Answers

All 6 Replies

void DayOfYear::print()

This is the definition of print(), and as you can see, it's not meant to accept any parameter.. however you tried to use it in this way:

DayOfYear::print(day)

This is wrong. To correct this error, change the definition of print() to this:

void DayOfYear::print(int num)

where num is the number the user is to enter. Hope this helped!

pertaining to my code, would I do this?

void DayOfYear::print(int day)
{
	int month = 0;

	while (DayOfYear::MonthDays[month] < day)
		month = (month + 1) %12;

		//Display month and day
		cout << DayOfYear::MonthName[month] << " " << day - DayOfYear::MonthDays[month-1];
};

Can someone please help me?

I used the DayOfYear object and got this error message:

1>DayOfYear.obj : error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const * const DayOfYear::MonthName" (?MonthName@DayOfYear@@2QBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@B)

1>DayOfYear.obj : error LNK2001: unresolved external symbol "public: static int const * const DayOfYear::MonthDays" (?MonthDays@DayOfYear@@2QBHB)

Thanks everyone for all your help!

My current code is:

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


//Day of the year class declaration
class DayOfYear
{
private:

public:
	static const int MonthDays[];
	static const string MonthName[];
	void print(int);
};

//**************************************************
// This function displays the month and day using  *
// the number entered.                             *
//**************************************************

void DayOfYear::print(int day)
{
	int month = 0;

	while (DayOfYear::MonthDays[month] < day)
		month = (month + 1) %12;

		//Display month and day
		cout << DayOfYear::MonthName[month] << " " << day - DayOfYear::MonthDays[month-1];
};


int main()
{
	DayOfYear dYear;

	//Set days of each month into an array
	const int MonthDays[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
	//Set the name of each month into an array
	const string MonthName[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	
	int day;
	//Ask user the total day number
	cout << "\nEnter a number you would like to convert into a month and day";
	cin >> day;

	//Error check for negative numbers and numbers higher than one year
	if(day <= 0 || day > 365)
	{
		cout << "You must enter a valid number (1 thru 365)" << endl;
	}
	
	//Send entered day number to the print() function
	dYear.print(day);
	return 0;

}

Change this bit:

int main()
{
	DayOfYear dYear;

	//Set days of each month into an array
	const int MonthDays[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
	//Set the name of each month into an array
	const string MonthName[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

to this:

int main()
{
	DayOfYear dYear;

	//Set days of each month into an array
	const int DayOfYear::MonthDays[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
	//Set the name of each month into an array
	const string DayOfYear::MonthName[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

This is a problem of understanding memory allocation, and scope.

As you probably know, class Instance variables (or "Properties", in the uselessly new parlance) are automatically allocated when you create a class object, and do not need to be separately declared:

class B
	{
	public:
	int k; // a NON-STATIC (i.e. "Instance") variable
	};

void main()
	{
	B b; // All class instance variables are created here...
	b.k= 42; // .. and can be used.
	}

.. But "static" class variables must be declared like this:

class A
	{
	public:
	static int i; // this is a promise, that somewhere, you will allocate one of these
	};

int A::i = 42; // this is where you allocate (make space for) the static variable

void main()
	{
	A::i = 900; // you can use it anywhere, but it must be allocated outside all functions.
	}

You must allocate all static variables outside the body of the class, but in the same scope as the class itself.

.. And, what is scope?

// in C++, braces begin and end "Scope", or, neighborhood.
// ALL VARIABLES ARE DECLARED INSIDE SOME SCOPE

// a class has braces.. and thus defines a scope:
class A
	{
	public:
	static int c; // this 'c' is only for class A.
	};

// '::' is called "scope resolution operator"
int A::c= 42; // means, allocate 'c' in class A's scope

void main()
	{
		// here is one scope...
		{
		int c= 47; // this has nothing to do with class A
		}

		// .. and here is another one:
		{
		int c= 99; // same thing..
		} // when this brace closes, the 'c' declared inside just disappears!

		// now use something from class A::
		A::c= 0xf00;  // fine!  this is a third scope.
	}

Your first attempt tried to allocate the variable inside another function scope, that is, the main program's code block:

#include <iostream>
class A
	{
	static std::string i[]; // here is the promise..
	};
// .. where is your actual data?

// it can't be inside a function like this:
void main() 
{
// this line gets a compiler error on my system...
std::string A::i[] = {"foo","bar"}; // wrong
}

The code above declares a static variable in class A, which must be allocated somewhere in the same neighborhood as class A, not inside another function scope.

Your second attempt also failed to move the allocation. Instead, it created a completely separate variable inside the function:

#include <iostream>
class A
	{
	static std::string i[]; // here is the promise..
	};
// .. where is your actual data? 

// now we go into a new function scope:
void main() 
{
// this declares a new, local string...
std::string i[] = {"foo","bar"}; // not in A:: scope.
} // .. main-function scope ends, and your local string disappears!

// so, the code compiles fine, with your promise, and the linker
// tries to put it all together, like a bride waiting at the altar, saying, where is he?
// (you never allocate A::i)

You must allocate all static variables outside the body of the class, but in the same scope as the class itself.

I oversimplified, a little. It's more accurate to say "static class variables need to be declared in non-local scope", but, the given explanation will work until you learn why it's slightly wrong.

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.