First, I'll go ahead and thank any of you who can help me. Godspeed.
Second, I'll give some small background which may or may not be a part of my problem.
I'm a college student in the entry level C++ course. I've had two semesters of Java and a lot of practice programming. The concepts aren't lost on me, but a lot of the syntax and terms are for C++.
Now, to the problem:
I've spent the past few days working on the first assignment for my course. Essentially it involves creating a list of assignments. Each assignment is an object and each object has two pieces of data; A name, and a due date.
I'm using Dev-C++ 4.9.9.2 and there are 5 files in my project. I've managed to get it to compile without any of the normal issues I run into, typically syntax errors. Now I'm getting the dreaded "[Linker error] undefined reference to 'class::function()' type error.
Specifically, the error Dev-C++ is giving me is
[Linker error] undefined reference to `assignment::getDue()'
[Linker error] undefined reference to `assignment::getName()'
[Linker error] undefined reference to `assignment::getDue()'
[Linker error] undefined reference to `assignment::getName()'
[Linker error] undefined reference to `assignment::getDue()'
[Linker error] undefined reference to `assignment::getName()'
[Linker error] undefined reference to `assignment::getName()'
[Linker error] undefined reference to `assignment::getName()'
Id returned 1 exit status
[Build Error] [Assignment1.exe]Error1
I understand what that error is and I've had it before. Usually it's because I forgot to put a ; at the end of my header file or forgot to include it in my .cpp file. I've been combing through my code for a little over an hour now and I just can't figure this one out. And here we are.
#ifndef ASSLIST_H
#define ASSLIST_H
#include "assignment.h"
#include <iostream>
#include <list>
#include <string>
using namespace std;
class assList
{
public:
assList();
//create list
void add(assignment ***);
//Add a new assignment to the list
void remove(string myName);
//Remove an assignment from the list
void printList();
//Cout the list in the order they were input
void find();
//Find the assignment with the earliest due date.
list<assignment> myList;
};
#endif
I'm a college student in the entry level C++ course. I've had two semesters of Java and a lot of practice programming.
All I will say about that is that Java is not C++. Pretend you don't know Java when programming C++, as anyone who tries to use Java techniques when programming C++ will make a mess of things.
Second, those errors are not compiler errors, those are linker errors. The linker is responsible for taking object code and resolving the functions that are being called. What that error is telling you is that you're calling those functions, but where are they?
When you compile a C++ application, you must compile all of the source modules seperately, and then the linker takes these compiled modules (object code) and ties them together to create the final executable. The linker is responsible for actually figuring out where these functions you're calling reside.
More than likely, you did not put together your project correctly, as your project consists of multiple .cpp source files. You probably only compiled one of those source files, and then told the linker "go ahead and create the executable". That is when the linker said back to you "these functions cannot be found". You need to compile main.cpp, assignment.cpp, and asslist.cpp thus creating 3 object files. Then the linker must take these 3 object files and create the executable. You are obviously not doing this.
I knew what my compiler was trying to tell with the linker error, I just don't understand why I'm getting the error.
Using Dev-C++ is fairly simple. Create a project, add the files to it, choose rebuild all, and it works.
Just to be sure though, I created a new project, added all 5 files to the project and tried again. Same exact error.
I checked the directory where I'm saving these files, and there is a .o file for each of my 3 .cpp files.
Been going over my code some more though, and I notice that the numbers and the order of the errors directly coincides with the code I have in my assList.cpp file where I call temp.getDue(); temp.getName(); nextTemp.getDue(); and nextTemp.getName();
I'm guessing that it has something to do with that.
Two of these things are not like the others, six of these things are kinda, the same. Two of these things are not like the others, and now it is time---to play our game!
Here are two files. Create a project. Compile and link. Does it work? If not, then there is something wrong with your handling of the Dev-C++ environment, and you need to sort those problems out first before proceeding.
Two of these things are not like the others, six of these things are kinda, the same. Two of these things are not like the others, and now it is time---to play our game!
HAHA! Of course!
Well that solves that issue. It will compile now. Now it crashes when it tries to execute List.printList();
In regards to my global variables, they are unnecessary. The reason for them in the first place was just me troubleshooting some earlier issue and--once I added those global variables--it stopped being an issue. Just so happened it just a 'bandaid' to get me to the larger issue.
As far as dereferencing a pointer, again, you are right. When I was writing the code for this assignment I was keeping in mind the scope of what I would be doing in main.cpp. I didn't include the necessary NULL check in there because, in this particular example, I knew it would never be null.
Finally, no one hid the parameter, it was filtered by the forums I imagine. I hadn't realized it until Lindley made his post, but that particular parameter is the first three letters in assignment.
The issues I had with the crashing were from me pointing my iterators to a position just beyond the size of my list. Once I figured that out, the program runs now.
Bookmarks