class Example
{
int x;
public:
Example(int a)
{
x = a;
}
void setValue(int);
void printAddressAndValue();
};
#include "ThisExample.h"
#include <iostream>
usingnamespace std;
//**************************************************************
// Set value of object. *
//**************************************************************
void Example::setValue(int a)
{
x = a;
}
//**************************************************************
// Print address and value *
//**************************************************************
void Example::printAddressAndValue()
{
cout << " The object at address " << this << " has "
<< "value " << (*this).x << endl;
}
//This program illustrates the this pointer..
#include <iostream>
#include "ThisExample.h"
usingnamespace std;
int main()
{
Example ob1(10), ob2(20);
// Print the addresses of the two objects.
cout << "Address of objects are " << &ob1
<< " and " << &ob2 << endl;
// Print the addresses and values from within
ob1.printAddressAndValue();
ob2.printAddressAndValue();
system("PAUSE");
return EXIT_SUCCESS;
}
Code looks fine....I just compiled it in Visual Studio 2010, and it ran fine, so I don't know what the problem is...Btw, did you put lines 1-30 into file "ThisExample.h"?
no didn't put it into a .h file can't I run it all in one file? I don't need to make a seperate .h file do I ? Thought there was a way to run it in the .cpp file as well.
class Example
{
int x;
public:
Example(int a)
{
x = a;
}
void setValue(int);
void printAddressAndValue();
};
#include "ThisExample.h"
#include <iostream>
usingnamespace std;
//**************************************************************
// Set value of object. *
//**************************************************************
void Example::setValue(int a)
{
x = a;
}
//**************************************************************
// Print address and value *
//**************************************************************
void Example::printAddressAndValue()
{
cout << " The object at address " << this << " has "
<< "value " << (*this).x << endl;
}
You can't compile a .h file by itself. At least, you shouldn't. Header (.h, .hpp, etc.) files are meant only to be #include d in source (.cpp, etc.) files, which are compiled.
I second Athar on this. Especially since bl4ckb3rry got it to work in Visual Studio. DevC++ is buggy abandonware with an outdated compiler version. See this thread for details: http://cplusplus.com/articles/36vU7k9E/
I understand your issues with leaving behind an IDE which you've become so used to, but the problem is that DevC++ won't offer you stability in the long term. It has a lot of outstanding bugs.
On the other hand, Visual Studio is fairly resource heavy as IDEs go. But Code::Blocks should be nice and fast.