Thread: ld: undefined symbols

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    59

    ld: undefined symbols

    I looked for an answer on this but I can't seem to find a direct answer. What does ld: undefined symbols mean. It gives me that error and what causes it.. I am trying to learn about classes and functions.
    All this program does is take a certain amount of information located in a file and outputs them onto the screen.
    This is the error

    ld: undefined symbols
    printOutput(DayOfYear const*)

    Ah I might as well post the code..
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    class DayOfYear
    {
    public:
    	
    	int month;
    	int day;
    	int year;
    	int hour;
    	int min;
    	int sec;
    	void output() const;
    };
    	
    int initializeDates(DayOfYear dates[]);
    void printOutput(const DayOfYear dates[]);
    void output(const DayOfYear &date);
    	
    int main()
    {
    	DayOfYear dat[2];
    	
    	cout << "Data is being inputted from file..." << endl;
    	initializeDates(dat);
    	
    	cout << "Data is being outputted to screen..." << endl;
    	printOutput(dat);
    	
    	return 0;
    }
    
    int initializeDates(DayOfYear dates[])
    {
    	ifstream infile;
    	int i = 0;
    	infile.open("/Users/*************/Desktop/Test/dates");
    	
    	infile >> dates[0].month
    		   >> dates[0].day
    		   >> dates[0].year
    		   >> dates[0].hour
    		   >> dates[0].min
    		   >> dates[0].sec;
    		   
    	while(!infile.eof())
    	{
    		i++;
    		infile >> dates[i].month
    		   	   >> dates[i].day
    		       >> dates[i].year
    		       >> dates[i].hour
    		       >> dates[i].min
    		       >> dates[i].sec;
    	}
    	
    	infile.close();
    	return 0;
    }
    
    void output(const DayOfYear &date) 
    {
    	
    	cout << date.month << "/" << date.day << "/" << date.year;
    	cout << "   ";
    	cout << date.hour << ":" << date.min << ":" << date.sec << endl;	
    }
    
    void DayOfYear::output() const
    {
    	
    	cout << month << "/" << day << "/" << year;
    	cout << "   ";
    	cout << hour << ":" << min << ":" << sec << endl;	
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    void printOutput(const DayOfYear dates[]);

    You declare that function, but you never define it.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Geez, I can't believe I forgot that, but either way I still get the same error.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    void printOutput(const DayOfYear dates[]);

    void output(const DayOfYear &date)

    Assuming that the latter was supposed to be the definition of the former, they don't take quite the same argument, I think.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    I've been having trouble trying how to figure out to ouput the information and this is how one of the examples had it.
    The output function is used to output the first set of information, the member function of output is used to ouput all following sets after the first.

    If you have any suggestions on a better way to output that follows my theme I would appreciate it.

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I think this sort of example generally uses a member output() function to assist in overloading the stream insertion operator, <<. You need to do that to cout your objects.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    I believe I have that.. I updated the code a bit, and I got rid of that one error but now I have another..

    ERROR:
    Code:
    array.cxx: In function `int main()':
    array.cxx:30: error: could not convert `&dat' to `DayOfYear&'
    array.cxx:20: error: in passing argument 1 of `void output(DayOfYear&, int)'
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    class DayOfYear
    {
    public:
    	
    	int month;
    	int day;
    	int year;
    	int hour;
    	int min;
    	int sec;
    	void output();
    };
    	
    int initializeDates(DayOfYear dates[]);
    void output(DayOfYear &date, int count);
    	
    int main()
    {
    	DayOfYear dat[2];
    	
    	cout << "Data is being inputted from file..." << endl;
    	initializeDates(dat);
    	
    	cout << "Data is being outputted to screen..." << endl;
    	output(dat);
    	
    	return 0;
    }
    
    int initializeDates(DayOfYear dates[])
    {
    	ifstream infile;
    	int i = 0;
    	infile.open("/Users/danielluebcke/Desktop/Test/dates");
    	
    	infile >> dates[0].month
    		   >> dates[0].day
    		   >> dates[0].year
    		   >> dates[0].hour
    		   >> dates[0].min
    		   >> dates[0].sec;
    		   
    	while(!infile.eof())
    	{
    		i++;
    		infile >> dates[i].month
    		   	   >> dates[i].day
    		       >> dates[i].year
    		       >> dates[i].hour
    		       >> dates[i].min
    		       >> dates[i].sec;
    	}
    	
    	infile.close();
    	return 0;
    }
    
    void output(DayOfYear &date) 
    {
    	
    	cout << date.month << "/" << date.day << "/" << date.year;
    	cout << "   ";
    	cout << date.hour << ":" << date.min << ":" << date.sec << endl;	
    }
    
    void DayOfYear::output()
    {
    	
    	cout << month << "/" << day << "/" << year;
    	cout << "   ";
    	cout << hour << ":" << min << ":" << sec << endl;	
    }

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You are passing an array to your output function when it only takes a single object.

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Also, you've declared output to take two arguments but defined it to take only one.

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    I finally got rid of all the errors now that I know how everything works. I've been sitting here for 15 minutes trying to figure out why it isn't ouputting anything and I realized I had return 0 on the initialize function.. oops..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM