-
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;
}
-
void printOutput(const DayOfYear dates[]);
You declare that function, but you never define it.
-
Geez, I can't believe I forgot that, but either way I still get the same error.
-
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.
-
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.
-
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.
-
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;
}
-
You are passing an array to your output function when it only takes a single object.
-
Also, you've declared output to take two arguments but defined it to take only one.
-
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..