Class/header file question

This is a discussion on Class/header file question within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to create a very basic class but keep getting an undefined reference error. I am confused ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    4

    Class/header file question

    Hi,
    I am trying to create a very basic class but keep getting an undefined reference error. I am confused about what this error means, and when searching online, I was unable to find any complete answer. Any help would be greatly appreciated. Here is my code:

    Code:
    //My main/driver
    #include <iostream>
    #include "Example.h"
    using std::cout;
    using std::endl;
    
    int main() {
    	cout << "THIS IS AN EXAMPLE" << endl;
    	Example sample;
    	sample.print();
    return 0;
    }
    
    
    //My example class, i just want it to print out something.
    #include <iostream>
    using std::cout;
    using std::endl;
    
    class Example 
    {
    	void print() {
    		cout << "THIS IS NOW PRINTING IN THE CLASS" << endl;
    	}
    };
    
    //My header file:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    class Example 
    {
    	public: 
    		void println();
    };
    I am compiling on unix by typing g++ -o main main.cpp and as I mentioned before, it returns an undefined reference error.
    Thanks for any help anyone can give me.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    type
    Code:
    g++ -o main main.cpp example.cpp
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    4
    Quote Originally Posted by ZuK View Post
    type
    Code:
    g++ -o main main.cpp example.cpp
    Kurt
    When I do that, the undefined reference error is now gone, but then I get two new errors:
    Example.cpp: redefinition of 'class Example'
    Example.h: previous definition of 'class Example'

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,456
    1. Your header file should have header guards.
    2. Your implementation should be written as:
    Code:
    //My example class, i just want it to print out something.
    #include <iostream>
    #include "Example.h"
    
    using std::cout;
    using std::endl;
    
    void Example::print() {
        cout << "THIS IS NOW PRINTING IN THE CLASS" << endl;
    }
    Incidentally, you should remove your using directives from your header file, so as to avoid contaminating the namespace of whatever source file that the header is included in.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    4
    Thanks so much for your help,
    Its working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21