Thread: Pointers, Classes, and Errors o my!

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    Pointers, Classes, and Errors o my!

    Ok heres my class under header files as P1.h:


    typedef unsigned long int ULONG;

    class P1;
    {
    public:
    ULONG* pointer;
    pointer=&h;
    private:
    ULONG h, m, s;
    return 0;
    }

    Now heres main.cpp:

    #include <iostream.h>
    #include "p1.h"

    int main()
    {
    P1 test;
    test.pointer = 100;
    cout << "test.pointer = " << test.pointer;
    return 0;
    }

    Now heres the annoying errors I get:

    --------------------Configuration: PointerNClass - Win32 Debug--------------------
    Compiling...
    main.cpp
    c:\program files\microsoft visual studio\myprojects\pointernclass\p1.h(5) : error C2447: missing function header (old-style formal list?)
    c:\program files\microsoft visual studio\myprojects\pointernclass\main.cpp(6) : error C2079: 'test' uses undefined class 'P1'
    c:\program files\microsoft visual studio\myprojects\pointernclass\main.cpp(7) : error C2228: left of '.pointer' must have class/struct/union type
    c:\program files\microsoft visual studio\myprojects\pointernclass\main.cpp(8) : error C2228: left of '.pointer' must have class/struct/union type
    Error executing cl.exe.

    PointerNClass.exe - 4 error(s), 0 warning(s)


    I am trying to learn pointers and classes. They are hard to understand and I cant figure this one out.
    Last edited by Scottc1988; 03-13-2003 at 09:53 PM.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    missing semicolon at end of class declaration.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    PS.

    Didnt your mother tell you not to play with your privates in public?!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    3
    Lol

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    btw void main is also undefined behaviour.... use int main instead and return something back to the os at the end of main commonly 0 is returned.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    Guess you shouldn´t be making statements in class declaration:

    Code:
    pointer=&h;
    Do this in your constructor instead!

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    3
    I dont understand constructor/destructors.

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    nope dont need those () after P1 either
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    its simple a constructor is called to make an object and a destructor is called to destroy an object.

    your class...
    Code:
    class P1
    {
    public:
        typedef unsigned long ULONG;
        P1() : h(0),m(0),s(0) {} // constructor. syntax looks daunting
        P1( ULONG H,ULONG M,ULONG S) 
            { 
                 h = H;
                 m=( M > 59 )? 59 : M ;
                 s = (S > 59 ) ? 59:S;
             }
         P1( const P1& rhs ) : h(rhs.h),m(rhs.m),s(rhs.s) {}
         ~P1() {} // the do nothing destructor
    private:
        ULONG h, m, s;
    };
    Notice we have three constructors.
    the first allows us to do this....
    Code:
    P1 myp1;
    Its called a default constructor.
    The next allows us to do this...
    Code:
    P1 myp1 (10,10,10);
    constructing from ULONG,ULONG,ULONG
    the last one is called the copy constructor. This constructs from another P1 object.
    Code:
    P1 a;
    P1 b(a);
    The destructor does othing in this example as there are no resources to be freed. Destructors get heavier use when you deal with dynamic memory.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    967846745645
    Guest

    return 0; ?

    class p1
    { ...
    return 0; // ???
    };

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    int main()
    {
    // do stuff here
    return 0;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    This should work...although its not using the header file like you want.

    Code:
    #include <iostream.h>
    
    typedef unsigned long ULONG;
    
    class P1{
    public:
    ULONG* pointer;
    P1() { pointer = &h; }  // constructor
    display();
    private:
    ULONG h, m, s;
    };
    
    int main()  //always use int main ( )
    {
    P1 *test;	   // Make it a pointer so you can allocate dynamic memory
    test = new P1; // You must allocate memory 
    
    // the value of the pointer should be = 100
    
    *test->pointer = 10;  
    
    cout << "test.pointer = ";
    
    test->display();
    
    return 0;  // always return a value
    }
    
    P1::display()
    {
    	cout << *pointer << endl;
    }

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    you should delete at the end too

    delete test;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes: adding a new variable produces errors
    By drunkuilled in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 11:58 AM
  2. Errors compiling classes with VC++ 6.0
    By xErath in forum C++ Programming
    Replies: 2
    Last Post: 07-02-2004, 07:58 AM
  3. Pointers to classes
    By sirSolarius in forum C++ Programming
    Replies: 9
    Last Post: 11-25-2003, 07:25 PM
  4. Classes Compiler Errors
    By taiDasher in forum C++ Programming
    Replies: 5
    Last Post: 07-03-2002, 08:11 AM
  5. errors with classes (again)
    By neandrake in forum C++ Programming
    Replies: 1
    Last Post: 03-18-2002, 08:37 PM