not understanding dereference and pointers

I am studying C++ using a book (Sams's teach yourself C++ in one hour a day) and I'm not really understanding when to use int*, versus &int, and int&& is completely confusing. here is a sample of code I wrote supposed to create a Date class, convert the date to a decimal value, and then convert back. I hae not implemented all of the functions yet, and get an error (Date::DateToInt': non-standard syntax; use '&' to create a pointer to member) along with other errors from the last function that is not working yet I am not understanding where to use the pointer&, por really why it is needed. anyone able to help?


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
#include <iostream>
using namespace std;

class Date
{
private:
	int day, month, year;

public:
	Date(int inMonth, int inDay, int inYear)
		:month(inMonth), day(inDay), year(inYear) {};
	int DateToInt (Date dateToConvert)
	{
		int convertedDate = (dateToConvert.year * 10000 + dateToConvert.month * 100 + dateToConvert.day);
		return convertedDate;
	}
	Date IntToDate(int intToConvert)
	{
		int convertedDay = (intToConvert % 10100);
		int convertedMonth = ((intToConvert - convertedDay) % 10000);
		int convertedYear = (intToConvert - convertedDay - convertedMonth);
		Date convertedInt (convertedMonth, convertedDay, convertedYear);
	}
	void DisplayDate()
	{
		cout << month << " / " << day << " / " << year << endl;
	}
};

int main()
{
	int newYear = 20200101;
	Date holiday(1, 1, 2020);
	cout << "New Years Day is: ";
	holiday.DisplayDate ();
	cout << "or another way of displaying it: " << holiday.DateToInt << endl;
	Date newYearsDay = IntToDate(newYear);
	cout << newYearsDay;
	return 0;
}
& has 2 functions. reference, and address of.
int x = 123;
int* px = &x; //px is a pointer, and it now points to x, by taking 'address of'
int &rx = x; //rx is a reference. You can usually tell by where the & is: left side, reference, right side, address of.
in a function header, & is a reference and * is a pointer. I don't know of a way to violate that.

I would not start with classes to understand this stuff. It just adds a layer of complexity. Start with free functions or just hack around in main to get comfy with it all.

some questions:
why, and where, do you think you want to use a pointer for this? What is the point of pointers anyway? What is the point of references? Which one do you need, or both, and why?
Stop coding, and understand what you are trying to accomplish first. Then understand what the two tools bring you and which one accomplishes what you wanted. THEN code it.

the 2 cent version (you need to read a good reference on it)..
references are 'aliases'. You are just changing the name of a variable. they are used a lot to pass parameters to subroutines, for efficiency reasons that i won't go into here.
so
int x = 3;
int &rx = x; //rx IS x
rx = 12; //so changing rx changes x
cout << x; //so x is 12

and pointers are generally used to get at a memory location, or to allocate a block of memory (vectors do this for you in most c++ now, and this is less useful in most code today).
arrays are an example...
int x[2][2];
int *px = &x[0][0]; //grab the raw memory location
px[3] = 11; //cheat to get to the [1][1] item ... px[0] is x[0][0], px[1] is x[0][1], ... etc
cout << x[1][1]; //11 ...
this would let you iterate a 2-d array in a linear loop, or similar ideas let you copy it via memcpy, and so on.

those books are, by their nature, going to be poor at really getting to the details of the language. C++ is rumored to take 2-3 years of fairly intense study to even say 'i know this language' and be confident in that statement. They are not bad programs to get started with, but its like 'learn spanish in 2 weeks' (python) vs 'learn mandarin chinese in 2 weeks' (c++)... which one will have you closer to functioning in that short time? I would extend it, take what they show you and dig in a little online for each topic... keep asking things here...
Last edited on
The only reason I mention the pointers and reference is the error I receive when I tried to compile. I receive the following compile error:

Date::DateToInt': non-standard syntax; use '&' to create a pointer to member

For whatever reason my function in the class Date that is supposed to convert the date to an integer (taking year * 10000, then adding month * 100, and then adding the day at the end) I could not find an error in the function itself, and no amount of adding reference or pointers to the variables made it magically work. my previous functions worked ok, but this one has me stumped.
Are we looking at the same code?

Date IntToDate(int intToConvert)
Starts on line 17 and should return a Date ... but doesn't return anything.



cout << "or another way of displaying it: " << holiday.DateToInt << endl;
To be a function it needs ()



Date newYearsDay = IntToDate(newYear);
IntToDate() is a member function (i.e. associated with an object of some class). You are trying to use it as a stand-alone function.


cout << newYearsDay;
newYearsDay is a Date. You haven't defined how to << a date.
I commented all of the IntToDate, and started working just on DateToInt. I should get a return of the variable that I converted (convertedDate) in the form of 20200101 and then it should cout as

"or another way of displaying it: 20200101"

when I try adding (), I get the compile error that it will not accept 0 arguments.

here is main with part commented out

int main()
{
int newYear = 20200101;
Date holiday(1, 1, 2020);
cout << "New Years Day is: ";
holiday.DisplayDate ();
int newYearsDay = holiday.DateToInt();
//cout << "or another way of displaying it: " << DateToInt (holiday) << endl;
//Date newYearsDay = IntToDate(newYear);
//cout << newYearsDay;
return 0;
}
FIX YOUR FIRST ERROR FIRST - other errors may accrue from it.

1
2
3
4
5
6
7
Date IntToDate(int intToConvert)
	{
		int convertedDay = (intToConvert % 10100);
		int convertedMonth = ((intToConvert - convertedDay) % 10000);
		int convertedYear = (intToConvert - convertedDay - convertedMonth);
		Date convertedInt (convertedMonth, convertedDay, convertedYear);
	}


This does NOT return anything. LOOK - NO return STATEMENT!!!


When you have sorted that, please look at the other things which I picked out (from trying to compile in cpp.sh).
Last edited on
oops yup you're correct. Added the following line in the function IntToDate

return convertedInt;

still not able to compile it.

int main()
{
int newYear = 20200101;
Date holiday(1, 1, 2020);
cout << "New Years Day is: ";
holiday.DisplayDate ();
Date newYearsDay = IntToDate(newYear);
newYearsDay.DisplayDate();
return 0;
}

Says that IntToDate identifier not found
orcusb wrote:
Says that IntToDate identifier not found


Please read what I wrote earlier. IntToDate is a member function i.e. it is applied (using . notation) to an object of the Date class. You are trying to apply it as a standalone function.
Last edited on
I tried doing this several ways, and they all failed.

class Date
{
private:
int day, month, year;

public:
Date(int inMonth, int inDay, int inYear)
:month(inMonth), day(inDay), year(inYear) {};
Date IntToDate(int intToConvert)
{
int convertedDay = (intToConvert % 10100);
int convertedMonth = ((intToConvert - convertedDay) % 10000);
int convertedYear = (intToConvert - convertedDay - convertedMonth);
Date convertedInt (convertedMonth, convertedDay, convertedYear);
return convertedInt;
}
void DisplayDate()
{
cout << month << " / " << day << " / " << year << endl;
}
};

int main()
{
int newYear = 20200101;
Date holiday(1, 1, 2020);
cout << "New Years Day is: ";
holiday.DisplayDate ();
int newYearsDay = 0;
newYearsDay = holiday.DateToInt;

return 0;
}


I receive two compile errors:
Severity Code Description Project File Line Suppression State
Error C3867 'Date::DateToInt': non-standard syntax; use '&' to create a pointer to member


Severity Code Description Project File Line Suppression State
Error C2440 '=': cannot convert from 'int (__thiscall Date::* )(Date)' to 'int'

Are they on:
newYearsDay = holiday.DateToInt;
Compare that to:
holiday.DisplayDate ();
The original error you reported:

Date::DateToInt: non-standard syntax.....


Applies to this line in main:

cout << "or another way of displaying it: " << holiday.DateToInt << endl;

The "DateToInt" is a member function, but this does not use the function operator () on it, nor does it provide the required parameter of type Date to call it.

In reality I'm not convinced this should be a member function as is, it should probably be a static member since the function takes a Date, returns an int, but takes no action on the Date object which performs the call.

Put another way, if, as a member function, you called:

int a = holiday.DateToInt();

Then this function does not need a parameter, and should be changed to reflect that.

Perhaps it's name should be just ToInt, converting holiday to an int is what it does.


Last edited on
I eliminated that line currently, there is no output from DateToInt. If I can get it to compile, I will start experimenting until I have a decent output line, something on the order of
cout << newYearsDay;

My objective is to take the Date variable holiday and convert it to an int variable called newYearsDay.

then to print newYearsDay.

Originally I was trying to go both ways with an int being converted and then converting it back. Since it failed before I got any output I tried to simplify it and get one of the functions to work correctly (don't care which one). My issue is that I do not understand why it is failing.

I guess my first problem is that I cannot successfully call the function DateToInt
I have tried:
newYearsDay = holiday.DateToInt(); (Receive funcion cannot accept 0 arguements)
newYearsDay = holiday.DateToInt; (receive cannot convert Date to int which should happen in the function)
newYearsDay = DateToInt(holiday); (that one says no identifier)
Of the three options you give, what you didn't write was the one declared.

The declaration is:

int DateToInt (Date dateToConvert);

...as a member of Date. Currently there is a logical problem, but ignoring that for a moment, this can only be called with:

newYearsDay = holiday.DateToInt( holiday );

This is required because the declared function requires a Date parameter, passed by value.

You'll note just looking at it, that makes no real sense. There should not have to be two "holidays" provided.

Since this is a member function, it must be called on an instance (as in holiday.DateToInt(...whatever...))

However, you should change the declaration so it does not require a parameter. It should act on the instance, something like this:

1
2
3
4
5
int DateToInt ()
	{
		int convertedDate = (year * 10000 + month * 100 + day);
		return convertedDate;
	}


Then, you can call it with your first attempt:

newYearsDay = holiday.DateToInt();

This will operate on the instance, and that instance provides the year, month and day values upon which to calculate the return.

I assume that is calculated as you require, though it is non-standard - no matter, you're experimenting.

Now, about this form:

holiday.DateToInt;

This prompted the error message relative to the title of your post, and may have been quite confusing. This is how one obtains a pointer to a function. It can't be used in this way, but the point is that without the () following the name, you are telling the compiler to give you the address of the member function.

It is, one might say, and advanced feature you are not ready to use, yet.

The parenthesis () following such an identifier is actually an operator, the function operator. Without it, you're referring to the address of the function, but with it you're calling the function (or attempting to).

If course, the meaning of () changes with context. It isn't a function operator when used with an "if", "for" or "while", or other similar constructs. When used in a declaration or definition, it encloses the parameter list.

It becomes the "function" or "function call" operator when it appears in the context of an indentifer, like DateToInt.
Last edited on
hmm i have not tried those yet. I will try them this evening and post results. Thank you for some ideas.
Ok worked like a charm. Thank you for your help. I did not understand that I needed to pass the parameter and call the function from a member. Doing both it worked fine. I have not figured out how to eliminate providing the parameter yet, I will play with that some more.
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
#include <iostream>
using namespace std;

class Date
{
private:
	int day, month, year;

public:
	Date(int inMonth, int inDay, int inYear)
		:month(inMonth), day(inDay), year(inYear) {};
	int DateToInt (Date dateToConvert)
	{
		
		int convertedDate = (dateToConvert.year * 10000 + dateToConvert.month * 100 + dateToConvert.day);
		return convertedDate;
	}
	
	Date IntToDate(int intToConvert)
	{
		
		int convertedDay = (intToConvert % 100);
		int convertedMonth = (((intToConvert - convertedDay)/100) % 10);
		int convertedYear = (intToConvert - convertedDay - convertedMonth)/10000;
		Date convertedInt (convertedMonth, convertedDay, convertedYear);
		
		return convertedInt;
	}
	
	void DisplayDate()
	{
		cout << month << " / " << day << " / " << year << endl;
	}
};

int main()
{
	int today = 20200908;
	int newYear = 20200101;
	Date Holiday(01, 01, 2020);
	cout << "Holiday is: ";
	Holiday.DisplayDate ();
	int newYearsDay = 0;
	newYearsDay = Holiday.DateToInt(Holiday);
	cout << "or another way of displaying it: " << newYearsDay << endl;
	Date newHoliday(0, 0, 0);
	newHoliday = newHoliday.IntToDate(newYear);
	cout << "Converted back results in: ";
	newHoliday.DisplayDate();
	cout << "and today is: ";
	
	Date todayDate(0, 0, 0);
	todayDate = todayDate.IntToDate(today);
	todayDate.DisplayDate();
	return 0;
}

Holiday is: 1 / 1 / 2020
or another way of displaying it: 20200101
Converted back results in: 1 / 1 / 2020
and today is: 9 / 8 / 2020

I have not figured out how to eliminate providing the parameter yet,


It's in my post above.
Topic archived. No new replies allowed.