Thread: Starting to get aggrivated with classes....

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    28

    Starting to get aggrivated with classes....

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <fstream.h>
    
    
    class aclass
    {
    public:
    aclass();
    ~aclass();
    };
    
     
    int main(int argc, char *argv[])
    {
    
    aclass theclass;
     
      
      system("PAUSE");	
      return 0;
    }
    Why wont this compile. I declare the constructor, then the destructor. Then try to call an instance of the class and i get a linker error. If i do not add the 2 functions it will compile.
    ~matt~

  2. #2
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    add
    using namespace std;

    after the includes

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The linker expects the constructor/destructor to be defined somewhere... You've only declared them in that code.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    The compiler supplies a default constructor and a default destructor, so you don't need to declare and define either one if you want your code to compile--of course it won't do anything.

    Also, don't use standard library header files with a .h extension. All standard library header files have an equivalent header file without the .h extension.

    Finally, use the following format for main() unless you plan on using command line arguments(if you don't know what that means, just use the following format):

    int main()
    {

    ...

    return 0;
    }
    Last edited by 7stud; 05-30-2003 at 08:04 PM.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Indeed... Either of the following two are valid (and for that matter, equivalent).

    Code:
    class aclass
    {};
    
    ... and ...
    
    class aclass
    {
    public:
    aclass() {}
    ~aclass() {}
    };
    It will actually 'compile' fine the way you have it, but since you went ahead and declared those functions, it expects them to be defined somewhere (potentially in a different file), and its the job of the linker to make sure that every declaration matches a definition somewhere.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    28
    Ok, so basicly i don't have to worry about adding a constructor and destructor, because the compiler automaticly does that for me?
    ~matt~

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    It adds constructors and destructors that do nothing. If you want FUNCTIONAL constructors/destructors, you need to handle them yourself.

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    28
    For now this is fine, but i want to learn to create my own constructors and destructors as well. Can someone give me some example code creating a class with just the constructor and destructor?
    ~matt~

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    class aclass
    {
    public:
    
    aclass(int x, int y); { itsX=x; itsY=y; }
    ~aclass(); {} //doesn't need to do anything...
    
    private:
    
    int itsX;
    int itsY;
    
    };
    then in main you would create an object:

    Code:
    aclass num1(12,5);
    setting itsX to 12 and itsY to 5...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No compilation problem, but...

    #include <iostream>
    #include <fstream.h>

    Don't mix non-h and h. Use only non-h unless you have to use h.

    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Dec 2002
    Posts
    28
    I have to use .h, thats why i do that. Someone esle already told me that above.

    jawib: I get like 12 errors when i try to compile that code.
    ~matt~

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "I have to use .h, thats why i do that."

    Why? Doesn't this work on your compiler:
    Code:
    #include <iostream>
    using namespace std;
    
    int main () 
    {
    	cout<<"Hello world."<<endl;
    	
    	getchar();//To pause DOS window
    	return 0;
    }
    Post the code and post the errors.

  13. #13
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>Doesn't this work on your compiler?
    It shouldn't. You forgot to #include <cstdio>.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  14. #14
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Code:
    aclass(int x, int y); { itsX=x; itsY=y; }
    ~aclass(); {} //doesn't need to do anything...
    That's your problem right there. Should look like this:

    Code:
    aclass(int x, int y) { itsX=x; itsY=y; };
    ~aclass() {}; //doesn't need to do anything...
    Semicolon goes after the definition brackets. Now, you really should use an initializer list to initialize itsX and itsY, but that would just add to confusion...

  15. #15
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    oops hehe...usually i define my functions outside of the class...guess i confused myself
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. Just starting to learn C++ Question about classes
    By uraliss in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2006, 03:31 AM
  4. Problems with classes... just starting out w/ them.
    By criticalerror in forum C++ Programming
    Replies: 25
    Last Post: 03-05-2004, 12:19 PM
  5. Starting with classes: Your opinion
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-30-2002, 06:22 PM