C++ linked list

M

Matt Williams

I'm coming from a Java background - trying to build a linked list in
C++. I've read up on and understand the differences btw. the two
languages when it comes to pointers and most of the basics. My linked
list consisting of a driver, linkedlist, and node class all compile
correctly, but I am getting 13 linking errors:

Linking...
ListDriver.obj : error LNK2005: "public: __thiscall Node::Node(int)"
(??0Node@@QAE@H@Z) already defined in LinkedList.obj
ListDriver.obj : error LNK2005: "public: void __thiscall
Node::setNext(class Node *)" (?setNext@Node@@QAEXPAV1@@Z) already
defined in LinkedList.obj
ListDriver.obj : error LNK2005: "public: void __thiscall
Node::setPrevious(class Node *)" (?setPrevious@Node@@QAEXPAV1@@Z)
already defined in LinkedList.obj
ListDriver.obj : error LNK2005: "public: class Node * __thiscall
Node::getNext(void)" (?getNext@Node@@QAEPAV1@XZ) already defined in
LinkedList.obj
ListDriver.obj : error LNK2005: "public: class Node * __thiscall
Node::getPrevious(void)" (?getPrevious@Node@@QAEPAV1@XZ) already
defined in LinkedList.obj
ListDriver.obj : error LNK2005: "public: int __thiscall
Node::getData(void)" (?getData@Node@@QAEHXZ) already defined in
LinkedList.obj
ListDriver.obj : error LNK2005: "public: __thiscall
LinkedList::LinkedList(void)" (??0LinkedList@@QAE@XZ) already defined
in LinkedList.obj
ListDriver.obj : error LNK2005: "public: void __thiscall
LinkedList::add(int)" (?add@LinkedList@@QAEXH@Z) already defined in
LinkedList.obj
ListDriver.obj : error LNK2005: "public: void __thiscall
LinkedList::print(void)" (?print@LinkedList@@QAEXXZ) already defined
in LinkedList.obj
Node.obj : error LNK2005: "public: __thiscall Node::Node(int)"
(??0Node@@QAE@H@Z) already defined in LinkedList.obj
Node.obj : error LNK2005: "public: void __thiscall Node::setNext(class
Node *)" (?setNext@Node@@QAEXPAV1@@Z) already defined in
LinkedList.obj
Node.obj : error LNK2005: "public: void __thiscall
Node::setPrevious(class Node *)" (?setPrevious@Node@@QAEXPAV1@@Z)
already defined in LinkedList.obj
Node.obj : error LNK2005: "public: class Node * __thiscall
Node::getNext(void)" (?getNext@Node@@QAEPAV1@XZ) already defined in
LinkedList.obj
Node.obj : error LNK2005: "public: class Node * __thiscall
Node::getPrevious(void)" (?getPrevious@Node@@QAEPAV1@XZ) already
defined in LinkedList.obj
Node.obj : error LNK2005: "public: int __thiscall Node::getData(void)"
(?getData@Node@@QAEHXZ) already defined in LinkedList.obj
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/FirstProject.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

Any help would be greatly appreciated..... thanks
 
D

Donovan Rebbechi

I'm coming from a Java background - trying to build a linked list in
C++. I've read up on and understand the differences btw. the two
languages when it comes to pointers and most of the basics. My linked
list consisting of a driver, linkedlist, and node class all compile
correctly, but I am getting 13 linking errors:

You are defining the functions in two different translation units. You're
only allowed to compile each function definition once. This is called the
"one definition rule".

Perhaps it would help if you explained your code layout, paying particular
attention to what #includes you're using. For example, what are your project
files ? In which file is Node::Node(int) declared ? defined ? What files
#include the file that declares Node::Node ? Do any files #include the
file that defined Node::Node ? (they shouldn't)

Cheers,
 
D

David Harmon

I'm coming from a Java background - trying to build a linked list in
C++.

Recommend using std::list
Linking...
ListDriver.obj : error LNK2005: "public: __thiscall Node::Node(int)"
(??0Node@@QAE@H@Z) already defined in LinkedList.obj

OK, so why is the Node constructor defined in _either_ of the two files
ListDriver or LinkList, much less both of them. You understand that
this area is rather different in C++ from Java, right? What do your
#include lines in those two modules look like? Where is the definition
of the Node class constructor intended to be?
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/FirstProject.exe : fatal error LNK1120: 1 unresolved externals

And you seem to have omitted your
int main() { etc.
 
A

AngleWyrm

Matt Williams said:
I'm coming from a Java background - trying to build a linked list in
C++. I've read up on and understand the differences btw. the two
languages when it comes to pointers and most of the basics. My linked
list consisting of a driver, linkedlist, and node class all compile
correctly, but I am getting 13 linking errors:

C++ comes with a list; here's me example:

#include <iostream> // cout
#include <stdlib.h> // system()
#include <list> // list
using namespace std;

////////////////////////////////////////////
// Silly little streamable example class
class myDriver{
public:
// constructors
myDriver(void){ val = 10; }; // default c'tor
myDriver(int in):val(in){}; // with input val

// public interface
int Get(void)const {return val;};
bool operator<(const myDriver& them){ return val < them.Get(); };
myDriver operator=(const int in){ val = in; };
friend ostream & operator<<(ostream& os, const myDriver& me);

private:
int val;
};
// friend stream output function for class
ostream& operator<<(ostream& os, const myDriver& me){
cout << " " << me.val << " "; // marginal formatting
return os;
};


/////////////////////////////////////
// Main
int main(void) // command line unused
{
// Create an empty list to store myDriver objects
list<myDriver> driverList;

// Create some myDriver objects
myDriver someDriver, anotherDriver(42), yetAnotherDriver(100);

// add them to the list
driverList.push_back(someDriver);
driverList.push_back(anotherDriver);
driverList.push_front(yetAnotherDriver); // decided this one should be
first

// add one from user input
cout << "Give me a driver value: ";
int inputNumber;
cin >> inputNumber;
anotherDriver = inputNumber;
driverList.push_back(anotherDriver);

// display the list
cout << endl;
list<myDriver>::iterator i;
for( i = driverList.begin(); i != driverList.end(); ++i){
cout << *i << endl;
}

// sort it
driverList.sort();
cout << endl << "sorted" << endl;
for( i = driverList.begin(); i != driverList.end(); ++i){
cout << *i << endl;
}


system("PAUSE"); // wait for a keypress
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,100
Latest member
MelodeeFaj
Top