| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Matt Williams" |
| Date: |
09 Jan 2004 11:22:14 AM |
| Object: |
C++ linked list |
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
.
|
|
| User: "AngleWyrm" |
|
| Title: Re: C++ linked list |
13 Jan 2004 07:58:39 PM |
|
|
"Matt Williams" <mdwilliam@radford.edu> wrote in message
news:11e248a4.0401090922.52547ac0@posting.google.com...
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
}
.
|
|
|
|
| User: "Donovan Rebbechi" |
|
| Title: Re: C++ linked list |
09 Jan 2004 12:06:53 PM |
|
|
In article <11e248a4.0401090922.52547ac0@posting.google.com>, Matt Williams wrote:
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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
.
|
|
|
|
| User: "David Harmon" |
|
| Title: Re: C++ linked list |
09 Jan 2004 12:19:43 PM |
|
|
On 9 Jan 2004 09:22:14 -0800 in comp.lang.c++,
(Matt Williams) was alleged to have written:
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.
.
|
|
|
|

|
Related Articles |
|
|