473,385 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Jul 22 '05 #1
3 6235
In article <11**************************@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/
Jul 22 '05 #2
On 9 Jan 2004 09:22:14 -0800 in comp.lang.c++, md*******@radford.edu
(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.

Jul 22 '05 #3
"Matt Williams" <md*******@radford.edu> wrote in message
news:11**************************@posting.google.c om...
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
}
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.