(Usual) undefined reference to...

closed account (jvqpDjzh)
Where am i doing wrong? What can't i do here?

/******************************************************************************/
/*
main.cpp
*/
#include <iostream>
#include "address.h"

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {

cout<<"This program returns the address of a variable which you choose the value.\n\n";

int n=0;

cout<<"Choose an integer to see its memory address: ";
cin>>n;

cout<<"The address of the variable n (with value = "<<n<<") is "<<address(n)<<endl;

return 0;
}
/******************************************************************************/

/*
address.h
*/

#ifndef ADDRESS_H
#define ADDRESS_H

template <class T>
T address(T var);

#endif

/******************************************************************************/

/*
address.cpp
*/

#include "address.h"

template <class T>
T address(T var){
return (&var);
}

/******************************************************************************/
Last edited on
1) definition of templated function should be visible at place of instantiation. I.e. you have to place all templates in header files.

2) The type of expression &T will be T*

3) Your function will not work if T overloads unary & operator

4) there is and std::addressof() function in <memory> header
http://en.cppreference.com/w/cpp/memory/addressof
Last edited on
closed account (jvqpDjzh)
I confess i don't undestand well the 2) and 3) point:

2) what you mean is that i should return a pointer to the variable of type T (parameter) to have the address?

3) i can't understand well what you mean with "if T overloads &"...
2) Pointer to variable is basically is a variable address.

3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
struct troll
{
    troll* operator&() //Operator overloading
    {
        return (troll*)3735928559;
    }
};

int main()
{
    troll A, B;

    //Note that those two have seemingly same address, 
    //but if you try to use it it will likely lead to crash
    std::cout << &A << ' '<< &B;
}
0xdeadbeef 0xdeadbeef

closed account (jvqpDjzh)
3) Maybe it's because i haven't studied yet what "operator"... means but sincerely i don't understand what really you are doing. Maybe i should study first the chapter about operators overloading.

TRYING TO UNDERSTAND...

a) What you are doing in the struct is creating a function with a return type that is a pointer to the struct where the function is?

b) Then you convert a constant to a pointer? (is that possible?!)

c) hmmm... maybe i should study hard!
Well, you will get to it sometime, and then you will understand why your function would fail on it.

Things like &, +, -, * are operators. http://en.cppreference.com/w/cpp/language/expressions#Operators Most of them are not defined for user created classes. You can overload them i.e. add some new behavior to it. That is done by using specially named functions (started with word operator). When you do &A, you expect it to return a pointer to the address where A is. However for user defined classes you can make it do whatever you like. I could make that operator to return string "address is secret" instead.

Then you convert a constant to a pointer? (is that possible?!)
Pointers can be converted to integral value and back. That was that way starting from C. I used decimal constant to hide value which will be output to screen. I could wrote 0xDEADBEEF directly.

c) hmmm... maybe i should study hard!
As I said you will get there eventually. Overloading of & is rarely seen and usually done to help programmer, not to hinder him
Last edited on
closed account (jvqpDjzh)
Thanks for being patient!
Topic archived. No new replies allowed.