ThisExample

not sure what is going on but my Dev C++ Freezes every time I try to compile ThisExample

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
class Example
{
      int x;
  public:
      Example(int a)
      {
           x = a;
      }
      void setValue(int);
      void printAddressAndValue();
};
#include "ThisExample.h"
#include <iostream>
using namespace 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"
using namespace 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;
}

Is there something I am doing wrong?
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"?
Last edited on
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.
Your mistake is using Dev-C++. Use Code::Blocks or VS.
ok this is what I tried to compile for a .h file by its self.

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
class Example
{
      int x;
  public:
      Example(int a)
      {
           x = a;
      }
      void setValue(int);
      void printAddressAndValue();
};
#include "ThisExample.h"
#include <iostream>
using namespace 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;
}

and it gets stuck
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/

As an alternative, I would suggest Code::Blocks http://www.codeblocks.org/
dev offers stability. I have Visualstudio and it takes soooooooo. long to load. I can get it done just as fast on Dev.
Xander314 wrote:
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.
Last edited on
ThisExample.h:2: error: redefinition of `class Example'
pr11-1.cpp:2: error: previous definition of `class Example'

used Code::Blocks 10.05
and still comming up with the errors above.

looks to me it has something to do with the class Example
Well yeah, you first declare the class Example and then you're including ThisExample.h, which also declares it.
dev offers stability


It's broken and it will never be fixed. That is stability, and clearly it's a bad thing in this case. Don't think that stable == good.
Last edited on
Topic archived. No new replies allowed.