ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I am running into a particularly problematic linking error. I know it's not a compile error, because when I compile the .cpp file alone, it compiles just fine. But when I try:
c++ 131p4.cpp -o 131p4.exe
I get:
/tmp/cc0o8I06.o(.text+0x1e): In function `main':
: undefined reference to `ListType::ListType[in-charge]()'
collect2: ld returned 1 exit status
I have never run into an error like that before. What does it mean and how do I fix it? If I comment out the list declaration, I am able to link and compile just fine. Unfortunately, the entire .cpp file will deal with the list and it's components, so I need it.
Here is a snippet from the .cpp file I am using. It is mostly still placeholders. The problem seems to occur after I declare ListType list (whose class is defined in the list4.h file):
PHP Code:
//131p4.cpp
#include "list4.h"
#include <iostream>
using namespace std;
//room for my functions
void SelectionMenu();
void FreezeScreen();
void ErrorMsg();
void CharValidate(char sel);
int main(void)
{
ListType list; //declares the list we'll load into
char sel;
//switch on sel
switch (toupper(sel))
{
case 'A':cout << "case a";
FreezeScreen();
break;
case 'D':cout << "case d";
FreezeScreen();
break;
case 'E':cout << "case e";
FreezeScreen();
break;
case 'R':cout << "case r";
FreezeScreen();
break;
case 'L':cout << "case l";
FreezeScreen();
break;
case 'S':cout << "case s";
FreezeScreen();
break;
case 'W':cout << "case w";
FreezeScreen();
break;
case 'Q': cout << endl << "Goodbye!" << endl << endl;
}
return 0;
}
The list4.h file has an implementation file, list4.cpp. Also, list4.h includes item4.h, where my clients specs appear as follows:
PHP Code:
//item4.h
#indef _ITEM4
#define _ITEM4
#include "name4.h"
const int MAX_LENGTH = 25;
typedef NameType ItemType;
#endif
I have name4.h included in that file, which is a specification file for a class.
I hope I have provided enough info - if anyone can help a newbie out on this I would definitely appreciate it. Even my C++ prof hasn't been any help on this one!
The first thing that I would suggest would be to include iostream first. Sometimes the headers define things that our custom header require, and this can cause errors. I don't know how important the order of the headers is anymore, but it used to be vital. (=
Second, did you compile list4.cpp into an object? Did you link it into the final executable? I think this is where the problems lies.
Code:
~> c++ -c -o list4.o list4.cpp
~> c++ list4.o 131p4.cpp -o 131p4.exe
Is list4.ccp where ListType is defined? Can you post this code? The error has to be that the place where ListType is defined, either it's missing something, or some other ccp file is calling to a method of ListType that has not be defined, but has been declared.
OK, so you've got the core source file, 131p4.cpp, which includes list4.h. list4.h has a source file, list4.cpp which, at minimum, includes item4.h, which includes name4.h.
Do item4.h or name4.h have support source files? If so, have they been compiled into objects and are they also linked in to the final executable?
If name4.h has a support source file, the first thing I would do is to compile it into an object, and test every class/method defined in it in a test program that includes nothing else of your project. Make sure that it is fully functional on it's own.
Then, if item4.h has a support source file, I'd compile it into an object and test it, linked with name4's object and test every calss/method defined in it.
Then, I'd continue with list4.h, making an object and testing everything in it with a test program, linking in the item4 and name4 stuff.
If you can get to this point, then the error is in the final source of 131p4.cpp.
Unforunately, you've only posted snippets of the code, so I have no actual code to go by, but what I've said above is where I would begin.
bool ListType::Retrieve (ItemType& item) const
{
int location = 0;
bool found = false;
while ((location < length) && (!found))
{
if (item== data[location])
{
found = true;
item = data[location];
}
else
location++;
}
return found;
}
bool ListType::Insert (ItemType item)
// the item is stored in next available space.
{
if (length<MAX_LENGTH)
{
data[length] = item;
length++;
return true;
}
else
return false;
}
bool ListType::Delete (ItemType item)
// Pre: item's key has been initialized.
{
int location = 0;
bool found = false;
while ((location < length) && (!found))
{
if (item==data[location])
{
found = true;
data[location] = data[length - 1];
length--;
}
else
location++;
}
return(found);
}
void ListType::SelSort()
{
ItemType item;
int passCount;
int searchIndx;
int minIndx;
bool ListType::IsPresent (ItemType item) const
{
int location = 0;
bool found = false;
while ((location < length) && (!found))
{
if (item== data[location])
found = true;
else
location++;
}
return found;
}
void ListType::Print(ostream& output ) const
{
for (int i =0; i<length; i++)
{
output << data[i]<<endl;
}
}
and here is the header file for list4:
PHP Code:
// list4.h
#ifndef _LIST
#define _LIST
#include "item4.h"
// Item header file must be provided by the user of this class.
// Item header file must contain the following definitions:
// MAX_LENGTH:the maximum number of items on the list
// ItemType: the definition of the objects on the list
class ListType
{
public:
ListType();
// Default constructor
void MakeEmpty();
// Function: Initializes list to empty state.
// Post: List is empty.
bool IsFull() const;
// Function: Determines whether list is full.
// Pre: List has been initialized.
// Post: Function value = (list is full)
bool IsEmpty() const;
// Function: Determines whether list is empty.
// Pre: List has been initialized.
// Post: Function value = (list is empty)
int Length() const;
// Function: Determines the number of elements in list.
// Pre: List has been initialized.
// Post: Function value = number of elements in list
bool Retrieve(ItemType& item) const;
// Function: Retrieves list item whose key matches item's key (if
// present).
// Pre: List has been initialized.
// Key member of item is initialized.
// Post: If there is a list item someItem whose key matches
// item's key, then
// return val = true and item is a copy of
// someItem;
// otherwise return val= false and item is unchanged.
// List is unchanged.
bool Insert(ItemType item);
// Function: Adds element to list.
// Pre: List has been initialized.
// List is not full.
// Post: item is in list.
bool Delete(ItemType item);
// Function: Deletes the list item whose key matches item's key.
// Pre: List has been initialized.
// Key member of item is initialized.
// Post: That item is no longer in list.
bool IsPresent(ItemType item) const;
// Function: Checks if item is in the list based on the key
// Pre: List has been initialized.
// Key member of item is initialized.
// Post: If there is a list item someItem whose key matches
// item's key, then
// return val = true
// otherwise return val= false
void SelSort();
// Function: Sorts the list in ascending order
// Pre: List has been initialized
// Post: List is sorted in ascending order based on key field(s).
void Print(ostream& output) const;
// Function: Prints the entire list to an output stream
// Pre: List has been initialized, and output stream is open
// Post: List was printed to an output stream
private:
int length;
ItemType data[MAX_LENGTH];
};
void SetName(string first, string middle, string last);
// Sets the data members with the parameter values
// Pre: parameters must have values
// Post: member data in NameClass has values from parameters
void InputName(istream& input);
// Gets the full name from an open input stream
// pre: input stream must be open
// prompt must already be displayed
// post: member data in NameClass is extracted
void PrintName(ostream& out) const;
// Prints the member data to an output stream in tabular format
// Pre: stream is open
// Post: stream contains member data
I have to run and make dinner, so I will begin troubleshooting with your advice, but I wanted to present the files in total, in case you find something I'm missing in the meantime. Thanks again for your time helping with this, I sincerely appreciate it!!!
Perhaps it is merely because I am running an older version of gcc, but I don't see where NameType, as typedef'd in item4.h, is defined. I searched through all gcc's header files and found no mention of this class... so either it belongs to a newer version than I have, or there's another piece of the puzzle I am missing. (=
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.