Thread: Class/header file question

  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
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 02:46 PM