Thread: C++ Funtion/Include Problems.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I have done this. When I compile, it tells me

    [Linker error] undefined reference to `someFunc()'

    I'm still checking everything over right now.
    What compiler are you using? You have to make sure all the files are part of your project. In addition the file extensions must be correct: .h for the header file, and .cpp for the file with the definitions.

    Using multiple files is not some alternative to classes. It has to do with program organization. If you had a class, you would organize your program the same way:

    main.cpp:
    Code:
    #include "header.h"
    
    int main()
    {
    	One myone;
    	myone.display();
    
    	Two mytwo(3.2);
    	mytwo.show();
    	
    	return 0;
    }
    header.h:
    Code:
    #ifndef __HEADER_H__
    #define __HEADER_H__
    
    ////CLASS ONE//////
    class One
    {
    private:
    	int num;
    
    public:
    	One(int n);
    	One();
    	void display();
    };
    
    //////CLASS TWO//////
    class Two
    {
    private:
    	double time;
    
    public:
    	Two(double t);
    	Two();
    	void show();
    };
    
    #endif //__HEADER_H__
    definitions.cpp:
    Code:
    #include <iostream>
    #include "header.h"
    
    using namespace std;
    
    ////DEFINITIONS/////
    
    One::One(int n)
    {
    	num = n;
    }
    
    One::One()
    {
    	num = 0;
    }
    
    void One::display()
    {
    	cout<<num<<endl;
    }
    
    Two::Two(double t)
    {
    	time = t;
    }
    
    Two::Two()
    {
    	time = 1.0;
    }
    
    void Two::show()
    {
    	cout<<time<<endl;
    }
    You don't have to organize your code that way. Instead, you can just include everything in one file, which I suggest you do if you are having too much trouble with the multiple file concept. For instance, the same program above contained in a single file:
    Code:
    #include <iostream>
    using namespace std;
    
    ////CLASS ONE//////
    class One
    {
    private:
    	int num;
    
    public:
    	One(int n);
    	One();
    	void display();
    };
    
    //////CLASS TWO//////
    class Two
    {
    private:
    	double time;
    
    public:
    	Two(double t);
    	Two();
    	void show();
    };
    
    //////MAIN//////////
    int main()
    {
    	One myone;
    	myone.display();
    
    	Two mytwo(3.2);
    	mytwo.show();
    	
    	return 0;
    }
    
    ////DEFINITIONS/////
    
    One::One(int n)
    {
    	num = n;
    }
    
    One::One()
    {
    	num = 0;
    }
    
    void One::display()
    {
    	cout<<num<<endl;
    }
    
    Two::Two(double t)
    {
    	time = t;
    }
    
    Two::Two()
    {
    	time = 1.0;
    }
    
    void Two::show()
    {
    	cout<<time<<endl;
    }
    Last edited by 7stud; 04-21-2005 at 08:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM