• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Quick little C++ debugging Problem

TheJokerV

Weaksauce
Joined
Mar 23, 2007
Messages
81
I have the following class.

Code:
class SimpleLink {
public:
   SimpleLink(int newDatum);
   SimpleLink(int *data, int size);
   int getDatum();
   SimpleLink *getNext();
   void setDatum(int newValue);
   void setNext(SimpleLink *item);

protected:
   int datum;        
   SimpleLink *next; 
};

Code:
#include <iostream>
#include "SimpleLink.h"

   SimpleLink::SimpleLink(int newDatum=0) {
      // initialize one link
      datum=newDatum; next=NULL;
   }

   SimpleLink::SimpleLink(int *data, int size) {
      /* initialize a linked list with each link containing
      ** the next value in "data"
      */
      datum = data[0];

      if (size!=1)
         next = new SimpleLink(data+1,size-1);
      else  // the last link
         next = NULL;
   }

   // accessors
   int SimpleLink::getDatum() { return datum; }
   SimpleLink SimpleLink::*getNext() { return next; }

   // setters
   void SimpleLink::setDatum(int newValue) { datum= newValue; }
   void SimpleLink::setNext(SimpleLink *item) { next = item; }

And when I try to compile I get
vlsi22:~/EC327/Homework/2$ g++ SimpleLink.cpp Problem1main.cpp -o main
SimpleLink.cpp: In function ‘SimpleLink SimpleLink::* getNext()’:
SimpleLink.cpp:23: error: ‘next’ was not declared in this scope

I know that there is something really simple that I'm probably missing but I've been stuck on this and a few of my fellow students have looked at it and come up with no explanation for the error.
 
Help anyone? I've been mulling over this for 2 hours now and its starting to drive me nuts.
 
i haven't done c++ in a while so i'm just throwing this out there.

isn't it supposed to be:
SimpleLink* SimpleLink::getNext() { return next; }

if i'm completely wrong, sorry.
 
new problem I've been working on my code a bit and so far I have:

SimpleLink.h
Code:
class SimpleLink {
public:
   SimpleLink(int newDatum);
   SimpleLink(int *data, int size);
   ~SimpleLink();
   int getDatum();
   int getcount();
   SimpleLink *getNext();
   SimpleLink *last();
   void setDatum(int newValue);
   void setNext(SimpleLink *item);
   void changecount(int x);

protected:
   int datum;        
   SimpleLink *next; 
   static int count;
};

SimpleLink.cpp
Code:
#include <iostream>
#include "SimpleLink.h"

using namespace std;

   SimpleLink::SimpleLink(int newDatum=0) {
      // initialize one link
      datum=newDatum; next=NULL;
      count++;
   }

   SimpleLink::SimpleLink(int *data, int size) {
      /* initialize a linked list with each link containing
      ** the next value in "data"
      */
      datum = data[0];

      if (size!=1)
         next = new SimpleLink(data+1,size-1);
      else  // the last link
         next = NULL;
      count += size;
   }

   SimpleLink::~SimpleLink() { count--; }
   // accessors
   int SimpleLink::getDatum() { return datum; }
   int SimpleLink::getcount() { return count; }
   SimpleLink* SimpleLink::getNext() { return next; }
   SimpleLink* SimpleLink::last() {
	SimpleLink *temp = this;
	SimpleLink *temp1 = temp;	
	while(temp->getNext() != NULL){
		temp = temp1->getNext();
		temp1 = temp;
	}
	return temp;
	
   }
   // setters
   void SimpleLink::setDatum(int newValue) { datum= newValue; }
   void SimpleLink::setNext(SimpleLink *item) { next = item; }
   void SimpleLink::changecount(int x) { count += x; }

main.cpp
Code:
#include <iostream>
using namespace std;

#include "SimpleLink.h"

int countList(SimpleLink * const aList) {	
	return aList->getcount();
}


int printList(SimpleLink * const foo) {
	SimpleLink * temp1 = foo;
	SimpleLink * temp2 = foo;
	cout << "List: ";
	while (temp1->getNext() != NULL) {
		cout << temp1->getDatum() << " ";
		temp1 = temp2->getNext();
		temp2 = temp1;
	}
	cout << temp1->getDatum() << endl;
}

int main() {
	//Problem 1	
	//Part A
	int a[8] = {3,1,4,1,6,9,2,6};
	int size = 8;
	SimpleLink newList(a,size);
	cout << newList.getNext()->getNext()->getDatum() << endl;
	SimpleLink *temp1 = newList.getNext();
	SimpleLink *temp2 = &newList;
        newList = *temp1;
	delete temp2;
	

	//Problem 1	
	//Part B
	cout << "Count: " << countList(&newList) << endl;
	printList(&newList);
	
	return 0;
}

When I compile it I get the following:
vlsi22:~/EC327/Homework/2$ g++ SimpleLink.cpp Problem1main.cpp -o Main
/tmp/vlsi22.bu.edu.tmp.CTu28553/cckZxlkZ.o: In function `SimpleLink::SimpleLink(int)':
SimpleLink.cpp:(.text+0x16): undefined reference to `SimpleLink::count'
SimpleLink.cpp:(.text+0x1e): undefined reference to `SimpleLink::count'
/tmp/vlsi22.bu.edu.tmp.CTu28553/cckZxlkZ.o: In function `SimpleLink::SimpleLink(int)':
SimpleLink.cpp:(.text+0x3a): undefined reference to `SimpleLink::count'
SimpleLink.cpp:(.text+0x42): undefined reference to `SimpleLink::count'
/tmp/vlsi22.bu.edu.tmp.CTu28553/cckZxlkZ.o: In function `SimpleLink::~SimpleLink()':
SimpleLink.cpp:(.text+0x4c): undefined reference to `SimpleLink::count'
/tmp/vlsi22.bu.edu.tmp.CTu28553/cckZxlkZ.o:SimpleLink.cpp:(.text+0x54): more undefined references to `SimpleLink::count' follow
collect2: ld returned 1 exit status

I have no idea what this means. I guessing that for some reason count is undefined but I don't see how that's possible because I define it in the .h file. Can anyone see anything wrong?
 
i'm not about this either but i think when accessing static variables from non-static methods, you have to scope the variable.

SimpleLink::~SimpleLink() { SimpleLink::count--; }
 
i'm not about this either but i think when accessing static variables from non-static methods, you have to scope the variable.

SimpleLink::~SimpleLink() { SimpleLink::count--; }

Yea that didn't work:

Code:
vlsi22:~/EC327/Homework/2$ g++ SimpleLink.cpp Problem1main.cpp -o Main
/tmp/vlsi22.bu.edu.tmp.CTu28553/cccoFKZ7.o: In function `SimpleLink::SimpleLink(int)':
SimpleLink.cpp:(.text+0x16): undefined reference to `SimpleLink::count'
SimpleLink.cpp:(.text+0x1e): undefined reference to `SimpleLink::count'
/tmp/vlsi22.bu.edu.tmp.CTu28553/cccoFKZ7.o: In function `SimpleLink::SimpleLink(int)':
SimpleLink.cpp:(.text+0x3a): undefined reference to `SimpleLink::count'
SimpleLink.cpp:(.text+0x42): undefined reference to `SimpleLink::count'
/tmp/vlsi22.bu.edu.tmp.CTu28553/cccoFKZ7.o: In function `SimpleLink::~SimpleLink()':
SimpleLink.cpp:(.text+0x4c): undefined reference to `SimpleLink::count'
/tmp/vlsi22.bu.edu.tmp.CTu28553/cccoFKZ7.o:SimpleLink.cpp:(.text+0x54): more undefined references to `SimpleLink::count' follow
collect2: ld returned 1 exit status

Am I doing something wrong?
.cpp file with suggestion:
Code:
#include <iostream>
#include "SimpleLink.h"

using namespace std;

   SimpleLink::SimpleLink(int newDatum=0) {
      // initialize one link
      datum=newDatum; next=NULL;
      SimpleLink::count++;
   }

   SimpleLink::SimpleLink(int *data, int size) {
      /* initialize a linked list with each link containing
      ** the next value in "data"
      */
      datum = data[0];

      if (size!=1)
         next = new SimpleLink(data+1,size-1);
      else  // the last link
         next = NULL;
      SimpleLink::count += size;
   }

   SimpleLink::~SimpleLink() { SimpleLink::count--; }
   // accessors
   int SimpleLink::getDatum() { return datum; }
   int SimpleLink::getcount() { return count; }
   SimpleLink* SimpleLink::getNext() { return next; }
   SimpleLink* SimpleLink::last() {
	SimpleLink *temp = this;
	SimpleLink *temp1 = temp;	
	while(temp->getNext() != NULL){
		temp = temp1->getNext();
		temp1 = temp;
	}
	return temp;
	
   }
   // setters
   void SimpleLink::setDatum(int newValue) { datum= newValue; }
   void SimpleLink::setNext(SimpleLink *item) { next = item; }
   void SimpleLink::changecount(int x) { count += x; }
 
You're including the class in both files; the header's probably conflicting. Wrap it in ifndef blocks. i.e.:

Code:
#ifndef __MY_HEADER_NAME
#define __MY_HEADER_NAME

// code goes here

#endif
 

Well first I would like to thank reil. I was able to compile the program successfully. The new .cpp file is:
Code:
#include <iostream>
#include "SimpleLink.h"

using namespace std;

   int SimpleLink::count;
	
   SimpleLink::SimpleLink(int newDatum=0) {
      // initialize one link
      datum=newDatum; next=NULL;
      count++;
   }

   SimpleLink::SimpleLink(int *data, int size) {
      /* initialize a linked list with each link containing
      ** the next value in "data"
      */
      datum = data[0];

      if (size!=1)
         next = new SimpleLink(data+1,size-1);
      else  // the last link
         next = NULL;
      count += size;
   }

   SimpleLink::~SimpleLink() { count--; }
   // accessors
   int SimpleLink::getDatum() { return datum; }
   int SimpleLink::getcount() { return count; }
   SimpleLink* SimpleLink::getNext() { return next; }
   SimpleLink* SimpleLink::last() {
	SimpleLink *temp = this;
	SimpleLink *temp1 = temp;	
	while(temp->getNext() != NULL){
		temp = temp1->getNext();
		temp1 = temp;
	}
	return temp;
	
   }
   // setters
   void SimpleLink::setDatum(int newValue) { datum= newValue; }
   void SimpleLink::setNext(SimpleLink *item) { next = item; }
   void SimpleLink::changecount(int x) { count += x; }
However when I ran the program I got the following:
Code:
vlsi37:~/EC327/Homework/2$ g++ SimpleLink.cpp Problem1main.cpp -o Main
vlsi37:~/EC327/Homework/2$ ./Main
4
*** glibc detected *** ./Main: munmap_chunk(): invalid pointer: 0xbfd544cc ***
======= Backtrace: =========
/lib/libc.so.6(cfree+0x1bb)[0x39f6db]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x2feef1]
./Main[0x8048bb3]
/lib/libc.so.6(__libc_start_main+0xdc)[0x34bdec]
./Main(__gxx_personality_v0+0x45)[0x8048691]
======= Memory map: ========
001fe000-00217000 r-xp 00000000 08:01 19529746   /lib/ld-2.5.so
00217000-00218000 r-xp 00018000 08:01 19529746   /lib/ld-2.5.so
00218000-00219000 rwxp 00019000 08:01 19529746   /lib/ld-2.5.so
0024e000-0032b000 r-xp 00000000 08:01 6361924    /usr/lib/libstdc++.so.6.0.8
0032b000-0032e000 r-xp 000dd000 08:01 6361924    /usr/lib/libstdc++.so.6.0.8
0032e000-00330000 rwxp 000e0000 08:01 6361924    /usr/lib/libstdc++.so.6.0.8
00330000-00336000 rwxp 00330000 00:00 0 
00336000-0046d000 r-xp 00000000 08:01 19529753   /lib/libc-2.5.so
0046d000-0046f000 r-xp 00137000 08:01 19529753   /lib/libc-2.5.so
0046f000-00470000 rwxp 00139000 08:01 19529753   /lib/libc-2.5.so
00470000-00473000 rwxp 00470000 00:00 0 
0061e000-00643000 r-xp 00000000 08:01 19529761   /lib/libm-2.5.so
00643000-00644000 r-xp 00024000 08:01 19529761   /lib/libm-2.5.so
00644000-00645000 rwxp 00025000 08:01 19529761   /lib/libm-2.5.so
007fd000-007fe000 r-xp 007fd000 00:00 0          [vdso]
00bba000-00bc5000 r-xp 00000000 08:01 19529730   /lib/libgcc_s-4.1.1-20070105.so.1
00bc5000-00bc6000 rwxp 0000a000 08:01 19529730   /lib/libgcc_s-4.1.1-20070105.so.1
08048000-08049000 r-xp 00000000 00:18 24082535   /ad/eng/users/n/k/nkani/EC327/Homework/2/Main
08049000-0804a000 rw-p 00001000 00:18 24082535   /ad/eng/users/n/k/nkani/EC327/Homework/2/Main
09a70000-09a91000 rw-p 09a70000 00:00 0 
b7fc8000-b7fca000 rw-p b7fc8000 00:00 0 
b7fd9000-b7fdb000 rw-p b7fd9000 00:00 0 
bfd41000-bfd57000 rw-p bfd41000 00:00 0          [stack]
Aborted
Wow. If it makes any difference I am compiling/running in red hat linux. I went through the code and I narrowed the problem down to the delete function. For some reason the delete function believes temp2 is an invalid pointer. The program works without it but the counting is off on the count of the deconstructors not being called. Does anyone see something I'm doing wrong with the delete function.
 
Code:
SimpleLink newList(a,size);
cout << newList.getNext()->getNext()->getDatum() << endl;
SimpleLink *temp1 = newList.getNext();
SimpleLink *temp2 = &newList;
newList = *temp1;
delete temp2;

when to declare a variable like:
Code:
SimpleLink newList(a,size);
the variable exists in the stack. which will be freed after the function goes out of scope. don't delete these.

when you use a pointer (not done in your code):
Code:
SimpleLink *temp1 = new SimpleLink(a,size)
you allocate memory on the heap for the object. these are the ones you have to delete.

so you're just pointing temp2 to something on the stack so you shouldn't be deleting it.

use newList as a pointer and it should work but once again, hell if i know, i'm doing this from memory.
 
OK so I got the SimpleLink class working but now there is a new problem. I have to build a double link class which is essentially a derivative of the simple link class. So far this is what I have:
SimpleLink.h
Code:
class SimpleLink {
public:
   SimpleLink();
   SimpleLink(int newDatum);
   SimpleLink(int *data, int size);
   ~SimpleLink();
   int getDatum();
   int getcount();
   SimpleLink *getNext();
   SimpleLink *last();
   void setDatum(int newValue);
   void setNext(SimpleLink *item);
   void changecount(int x);
   void insertAfter(int num);
   void insertAfter(int *numArray, int size);
   void deleteAfter();
   void deleteAfter(int num);

protected:
   int datum;        
   SimpleLink *next; 
   static int count;
};
SimpleLink.cpp
Code:
#include <iostream>
#include "SimpleLink.h"

using namespace std;

   int SimpleLink::count;
   
   SimpleLink::SimpleLink(){
	next = NULL;
	datum = 0;
   }
   	
   SimpleLink::SimpleLink(int newDatum=0) {
      // initialize one link
      datum=newDatum; next=NULL;
      count++;
   }

   SimpleLink::SimpleLink(int *data, int size) {
      /* initialize a linked list with each link containing
      ** the next value in "data"
      */
      datum = data[0];

      if (size!=1)
         next = new SimpleLink(data+1,size-1);
      else  // the last link
         next = NULL;
      count++;
   }

   SimpleLink::~SimpleLink() { count--; }
   // accessors
   int SimpleLink::getDatum() { return datum; }
   int SimpleLink::getcount() { return count; }
   SimpleLink* SimpleLink::getNext() { return next; }
   SimpleLink* SimpleLink::last() {
	SimpleLink *temp = this;
	SimpleLink *temp1 = temp;	
	while(temp->getNext() != NULL){
		temp = temp1->getNext();
		temp1 = temp;
	}
	return temp;
	
   }
   void SimpleLink::insertAfter(int num){
	SimpleLink *temp = new SimpleLink(num);
	temp->setNext(next);
	next = temp;
   }
   void SimpleLink::insertAfter(int *numArray, int size){	
	SimpleLink *temp1;
	SimpleLink *temp2;
	SimpleLink *temp3;
	temp3 = next;
	temp1 = new SimpleLink(*numArray);
	next = temp1;	
	temp2 = temp1;
	for(int i = 1; i < size; i++){
		temp1 = new SimpleLink(*(numArray+i));
		temp2->setNext(temp1);
		temp2 = temp1;
	}
	temp2->setNext(temp3);
   }
   void SimpleLink::deleteAfter() {
	SimpleLink *temp1 = next;
	next = temp1->getNext();
	delete temp1;
   }
   void SimpleLink::deleteAfter(int num){
	SimpleLink *temp1;	
	for(int i = 0; i < num; i++){
		if (next != NULL) {
			temp1 = next;
			next = temp1->getNext();
			delete temp1;
		}else{
			delete next;
			next = NULL;
		}
	}
   }			
   // setters
   void SimpleLink::setDatum(int newValue) { datum= newValue; }
   void SimpleLink::setNext(SimpleLink *item) { next = item; }
   void SimpleLink::changecount(int x) { count += x; }
DoubleLink.h
Code:
#include "SimpleLink.h"

class DoubleLink: public SimpleLink {
public:
	DoubleLink(int newDatum);
	DoubleLink(int *data, int size);
	void setPrev(DoubleLink *item);
	void insertBefore(int num);
	void insertBefore(int *numArray, int size);
	void deleteBefore();
	void deleteBefore(int num);
	DoubleLink *first();
	DoubleLink *getPrev();
        DoubleLink *getNext();
	void insertAfter(int num);
	void insertAfter(int *numArray, int size);
	void deleteAfter();
	void deleteAfter(int num);

protected:
	DoubleLink *previous;
	DoubleLink *next;
};
DoubleLink.cpp
Code:
#include <iostream>
#include "DoubleLink.h"

using namespace std;

DoubleLink::DoubleLink(int newDatum):SimpleLink(newDatum){
	previous = NULL;
}
DoubleLink::DoubleLink(int *data, int size){
	previous = NULL;
	datum = data[0];
        if (size!=1){
           next = new DoubleLink(data+1,size-1);
	   next->setPrev(this);
	}
        else {  // the last link
           next = NULL;
	}
        count++;
}
void DoubleLink::setPrev(DoubleLink *item){
	previous = item;
}
DoubleLink* DoubleLink::getPrev(){
	return previous;
}
void DoubleLink::insertBefore(int num){
	DoubleLink *temp1 = previous;
	previous = new DoubleLink(num);
	previous->setPrev(temp1);
	previous->setNext(this);
}
void DoubleLink::insertBefore(int *numArray, int size){
	DoubleLink *temp1 = previous;
	if (size > 0){	
		previous = new DoubleLink(*numArray);
		previous->setNext(this);
		previous->setPrev(temp1);
		this->insertBefore(numArray+1, size-1);
	}
}
void DoubleLink::deleteBefore(){
	if (previous != NULL){
		DoubleLink *temp = previous;
		previous = previous->getPrev();
		previous->setNext(this);
		delete temp;
	}
}
void DoubleLink::deleteBefore(int num){
	DoubleLink *temp = NULL;	
	if (num > 0) {
		temp = previous;
		previous = previous->getPrev();
		previous->setNext(this);
		delete temp;
		if (previous != NULL){
			this->deleteBefore(num-1);
		}
	}
}
DoubleLink* DoubleLink::first(){
	DoubleLink *temp = this;
	DoubleLink *temp1 = temp;	
	while(temp->getPrev() != NULL){
		temp = temp1->getPrev();
		temp1 = temp;
	}
	return temp;
}
DoubleLink* DoubleLink::getNext() { return next; }
void DoubleLink::insertAfter(int num){
	DoubleLink *temp = new DoubleLink(num);
	temp->setNext(next);
	temp->setPrev(this);
	next = temp;
}
void DoubleLink::insertAfter(int *numArray, int size){
	DoubleLink *temp1 = next;
	if (size > 0){	
		next = new DoubleLink(*numArray);
		next->setNext(temp1);
		next->setPrev(this);
		next->insertAfter(numArray+1, size-1);
	}
}
void DoubleLink::deleteAfter() {
	DoubleLink *temp1 = next;
	next = temp1->getNext();
	next->setPrev(this);
	delete temp1;
}
void DoubleLink::deleteAfter(int num){
	DoubleLink *temp1;	
	for(int i = 0; i < num; i++){
		if (next != NULL) {
			temp1 = next;
			next = temp1->getNext();
			next->setPrev(this);
			delete temp1;
		}else{
			delete next;
			next = NULL;
		}
	}
}
main.cpp
Code:
#include <iostream>
using namespace std;

#include "SimpleLink.h"
#include "DoubleLink.h"

int countList(SimpleLink * const aList) {	
	return aList->getcount();
}


int printList(SimpleLink * const foo) {
	SimpleLink * temp1 = foo;
	SimpleLink * temp2 = foo;
	cout << "List: ";
	while (temp1->getNext() != NULL) {
		cout << temp1->getDatum() << " ";
		temp1 = temp2->getNext();
		temp2 = temp1;
	}
	cout << temp1->getDatum() << endl;
}

int main() {
	//Problem 1	
	//Part A
	int a[8] = {3,1,4,1,6,9,2,6};
	int size = 8;
	SimpleLink *newList = new SimpleLink(a,size);
	cout << newList->getNext()->getNext()->getDatum() << endl;
	SimpleLink *temp1 = newList->getNext();
	SimpleLink *temp2 = newList;
        newList = temp1;
	delete temp2;

	//Problem 1	
	//Part B
	cout << endl << "Test of countList and printList" << endl;
	cout << "Count: " << countList(newList) << endl;
	printList(newList);
	cout << "Last: " << newList->last()->getDatum() << endl;
	
	//Problem 1
	//Part C
	cout << endl << "Test of insertAfter(int)" << endl;
	newList->insertAfter(5);
	printList(newList);
	cout << "Count: " << countList(newList) << endl;
	cout << "Test of insertAfter(array, size)" << endl;
	newList->insertAfter(a,size);
	printList(newList);
	cout << "Count: " << countList(newList) << endl;
	cout << "Test of deleteAfter()" << endl;
	newList->deleteAfter();
	printList(newList);
	cout << "Count: " << countList(newList) << endl;
	cout << "Test of deleteAfter(int)" << endl;
	newList->deleteAfter(7);
	printList(newList);
	cout << "Count: " << countList(newList) << endl;

	//Problem 2
	//Part A
	DoubleLink *newList2 = new DoubleLink(a,size);
	cout << newList2->getNext()->getNext()->getDatum() << endl;
	DoubleLink *temp11 = newList2->getNext();
	DoubleLink *temp12 = newList2;
        newList = temp11;
	delete temp12;

	cout << endl << "Test of countList and printList" << endl;
	cout << "Count: " << countList(newList2) << endl;
	printList(newList2);
	cout << "Last: " << newList2->last()->getDatum() << endl;
	cout << "First: " << newList2->last()->getDatum() << endl;
	
	cout << endl << "Test of insertAfter(int)" << endl;
	newList2->insertAfter(5);
	printList(newList2);
	cout << "Count: " << countList(newList) << endl;
	cout << "Test of insertAfter(array, size)" << endl;
	newList2->insertAfter(a,size);
	printList(newList2);
	cout << "Count: " << countList(newList) << endl;
	cout << "Test of deleteAfter()" << endl;
	newList2->deleteAfter();
	printList(newList2);
	cout << "Count: " << countList(newList) << endl;
	cout << "Test of deleteAfter(int)" << endl;
	newList2->deleteAfter(7);
	printList(newList2);
	cout << "Count: " << countList(newList) << endl;

	cout << endl << "Test of insertBefore(int)" << endl;
	newList2->insertBefore(5);
	printList(newList2);
	cout << "Count: " << countList(newList) << endl;
	cout << "Test of insertBefore(array, size)" << endl;
	newList2->insertBefore(a,size);
	printList(newList2);
	cout << "Count: " << countList(newList) << endl;
	cout << "Test of deleteBefore()" << endl;
	newList2->deleteBefore();
	printList(newList2);
	cout << "Count: " << countList(newList) << endl;
	cout << "Test of deleteBefore(int)" << endl;
	newList2->deleteBefore(7);
	printList(newList2);
	cout << "Count: " << countList(newList) << endl;
	return 0;
}

And after an hour of debugging I am left with this when I try to compile:
Code:
vlsi37:~/EC327/Homework/2$ g++ SimpleLink.cpp DoubleLink.cpp main.cpp -o Main
SimpleLink.h:1: error: redefinition of ‘class SimpleLink’
SimpleLink.h:1: error: previous definition of ‘class SimpleLink’
I'm honestly stumped. I can't see anyhting wrong. Though I have been coding for a couple hours now so my eyes may just be tired. Could anyone plese shed some light on this error/what I'm doing wrong. Thanks for your help guys.
 
You're including the class in both files; the header's probably conflicting. Wrap it in ifndef blocks. i.e.:

Code:
#ifndef __MY_HEADER_NAME
#define __MY_HEADER_NAME

// code goes here

#endif
My previous post explained what's happened with the class redefinition error. See this link for a bigger explanation.
 
Arainach, your fix works, I should have listened to you sooner. Now I have a bit of a problem with my program. See the DoubleLink class has to be a derivative of the SimpleLink class. The problem is with the fact that the next variable is originally declared as a SingleLink pointer in the SimpleLink class. As you can see in a couple of the functions in my doubleLink class need to use DoubleLink class functions with the next variable (i.e. Double link insert after(next->setPrev)). If I redefine a next function in my doubleLink class as I have done, this leads to confusion because the SimpleLink functions use one next and the doubleLink functions use another next. Does any know of a way for me to be able to use the DoubleLink functions with the next SingleLink variable. I thought of adding a virtual function to SingleLink but when I did this:
Code:
#ifndef SINGLELINK_H
#define SINGLELINK_H

class SimpleLink {
public:
   SimpleLink();
   SimpleLink(int newDatum);
   SimpleLink(int *data, int size);
   ~SimpleLink();
   int getDatum();
   int getcount();
   SimpleLink *getNext();
   SimpleLink *last();
   void setDatum(int newValue);
   void setNext(SimpleLink *item);
   void changecount(int x);
   void insertAfter(int num);
   void insertAfter(int *numArray, int size);
   void deleteAfter();
   void deleteAfter(int num);
   virtual void setPrev() = 0;

protected:
   int datum;        
   SimpleLink *next; 
   static int count;
};

#endif

I got this on compilation:
Code:
vlsi37:~/EC327/Homework/2$ g++ SimpleLink.cpp DoubleLink.cpp main.cpp -o Main
SimpleLink.h:21: error: ‘DoubleLink’ has not been declared
SimpleLink.cpp: In constructor ‘SimpleLink::SimpleLink(int*, int)’:
SimpleLink.cpp:26: error: cannot allocate an object of abstract type ‘SimpleLink’
SimpleLink.h:4: note:   because the following virtual functions are pure within ‘SimpleLink’:
SimpleLink.h:21: note:  virtual void SimpleLink::setPrev(int*)
SimpleLink.cpp: In member function ‘void SimpleLink::insertAfter(int)’:
SimpleLink.cpp:48: error: cannot allocate an object of abstract type ‘SimpleLink’
SimpleLink.h:4: note:   since type ‘SimpleLink’ has pure virtual functions
SimpleLink.cpp: In member function ‘void SimpleLink::insertAfter(int*, int)’:
SimpleLink.cpp:57: error: cannot allocate an object of abstract type ‘SimpleLink’
SimpleLink.h:4: note:   since type ‘SimpleLink’ has pure virtual functions
SimpleLink.cpp:61: error: cannot allocate an object of abstract type ‘SimpleLink’
SimpleLink.h:4: note:   since type ‘SimpleLink’ has pure virtual functions
SimpleLink.h:21: error: ‘DoubleLink’ has not been declared
DoubleLink.cpp: In constructor ‘DoubleLink::DoubleLink(int*, int)’:
DoubleLink.cpp:13: error: cannot allocate an object of abstract type ‘DoubleLink’
DoubleLink.h:6: note:   because the following virtual functions are pure within ‘DoubleLink’:
SimpleLink.h:21: note:  virtual void SimpleLink::setPrev(int*)
DoubleLink.cpp:14: error: no matching function for call to ‘SimpleLink::setPrev(DoubleLink* const)’
SimpleLink.h:21: note: candidates are: virtual void SimpleLink::setPrev(int*)
DoubleLink.cpp: In member function ‘void DoubleLink::insertBefore(int)’:
DoubleLink.cpp:29: error: cannot allocate an object of abstract type ‘DoubleLink’
DoubleLink.h:6: note:   since type ‘DoubleLink’ has pure virtual functions
DoubleLink.cpp: In member function ‘void DoubleLink::insertBefore(int*, int)’:
DoubleLink.cpp:36: error: cannot allocate an object of abstract type ‘DoubleLink’
DoubleLink.h:6: note:   since type ‘DoubleLink’ has pure virtual functions
DoubleLink.cpp: In member function ‘DoubleLink* DoubleLink::getNext()’:
DoubleLink.cpp:71: error: invalid conversion from ‘SimpleLink*’ to ‘DoubleLink*’
DoubleLink.cpp: In member function ‘void DoubleLink::insertAfter(int)’:
DoubleLink.cpp:73: error: cannot allocate an object of abstract type ‘DoubleLink’
DoubleLink.h:6: note:   since type ‘DoubleLink’ has pure virtual functions
DoubleLink.cpp: In member function ‘void DoubleLink::insertAfter(int*, int)’:
DoubleLink.cpp:79: error: invalid conversion from ‘SimpleLink*’ to ‘DoubleLink*’
DoubleLink.cpp:81: error: cannot allocate an object of abstract type ‘DoubleLink’
DoubleLink.h:6: note:   since type ‘DoubleLink’ has pure virtual functions
DoubleLink.cpp:83: error: no matching function for call to ‘SimpleLink::setPrev(DoubleLink* const)’
SimpleLink.h:21: note: candidates are: virtual void SimpleLink::setPrev(int*)
DoubleLink.cpp: In member function ‘void DoubleLink::deleteAfter()’:
DoubleLink.cpp:88: error: invalid conversion from ‘SimpleLink*’ to ‘DoubleLink*’
DoubleLink.cpp:90: error: no matching function for call to ‘SimpleLink::setPrev(DoubleLink* const)’
SimpleLink.h:21: note: candidates are: virtual void SimpleLink::setPrev(int*)
DoubleLink.cpp: In member function ‘void DoubleLink::deleteAfter(int)’:
DoubleLink.cpp:97: error: invalid conversion from ‘SimpleLink*’ to ‘DoubleLink*’
DoubleLink.cpp:99: error: no matching function for call to ‘SimpleLink::setPrev(DoubleLink* const)’
SimpleLink.h:21: note: candidates are: virtual void SimpleLink::setPrev(int*)
SimpleLink.h:21: error: ‘DoubleLink’ has not been declared
main.cpp: In function ‘int main()’:
main.cpp:77: error: cannot allocate an object of abstract type ‘DoubleLink’
DoubleLink.h:6: note:   because the following virtual functions are pure within ‘DoubleLink’:
SimpleLink.h:21: note:  virtual void SimpleLink::setPrev(int*)
I'm so confused and very frustrated. Any help...please?:(:confused:
 
i wouldn't add a pure virtual function for your situation. i would also say look at your inheritance again, there should be no reason to redefine the next pointer in DoubleLink, just use the next pointer from SingleLink. You may need to define some (all?) methods as virtual so DoubleLink methods will be invoked from a SingleLink pointer. As for your errors, you can't instantiate an abstract class.
 
See that's the problem when I add a virtual function to the .cpp file such as this:
Code:
   virtual void SimpleLink::setPrev(){}
it gives me this:
Code:
DoubleLink.cpp:83: error: no matching function for call to ‘SimpleLink::setPrev(DoubleLink* const)’
SimpleLink.h:21: note: candidates are: virtual void SimpleLink::setPrev()
When I try this:
Code:
   virtual void SimpleLink::setPrev(DoubleLink *item){}
it gives me:
Code:
SimpleLink.h:21: error: ‘DoubleLink’ has not been declared

am I wrong in thinking this is a catch m22 of sorts?
 
See that's the problem when I add a virtual function to the .cpp file such as this:
Code:
   virtual void SimpleLink::setPrev(){}
it gives me this:
Code:
DoubleLink.cpp:83: error: no matching function for call to ‘SimpleLink::setPrev(DoubleLink* const)’
SimpleLink.h:21: note: candidates are: virtual void SimpleLink::setPrev()
When I try this:
Code:
   virtual void SimpleLink::setPrev(DoubleLink *item){}
it gives me:
Code:
SimpleLink.h:21: error: ‘DoubleLink’ has not been declared

am I wrong in thinking this is a catch m22 of sorts?

i have no idea what you're trying to do here. why are you making a virtual method of set without any parameters? if SimpleLink is a singly linked list, why does it even have a setPrev? if a setPrev is needed for DoubleLink, then it should only exist in that class not SimpleLink. if this is some weird way to get access to the next variable in SimpleLink, you already have access as its protected and not private.
 
Honestly I've probably been coding for 6 hours straight and I am exhausted so maybe I being a dumb noob. I think I need to go get a little shut eye but before I do go let me explain myself fully so if someone wants to they can investigate the problem and/or post a fix. Here it goes: my programming teacher wants us to learn how basic classes such as linked lists and vectors work so for this particular homework assignment we have to create a personal linked list class. Now the first part was to create the single linked list. With the help of the [H] community I was able to get that to work flawlessly so I know that's good. Now the second part of the assignment is to create a double linked list which is derived from the single linked list. Now the following is the best I ever got it...

SimpleLink.h
Code:
#ifndef SIMPLELINK_H
#define SIMPLELINK_H

class SimpleLink {
public:
   SimpleLink();
   SimpleLink(int newDatum);
   SimpleLink(int *data, int size);
   ~SimpleLink();
   int getDatum();
   int getcount();
   SimpleLink *getNext();
   SimpleLink *last();
   void setDatum(int newValue);
   void setNext(SimpleLink *item);
   void changecount(int x);
   void insertAfter(int num);
   void insertAfter(int *numArray, int size);
   void deleteAfter();
   void deleteAfter(int num);

protected:
   int datum;        
   SimpleLink *next; 
   static int count;
};

#endif

SimpleLink.cpp
Code:
#include <iostream>
#include "SimpleLink.h"

using namespace std;

   int SimpleLink::count;
   
   SimpleLink::SimpleLink(){
	next = NULL;
	datum = 0;
   }
   	
   SimpleLink::SimpleLink(int newDatum=0) {
      // initialize one link
      datum=newDatum; next=NULL;
      count++;
   }

   SimpleLink::SimpleLink(int *data, int size) {
      /* initialize a linked list with each link containing
      ** the next value in "data"
      */
      datum = data[0];

      if (size!=1)
         next = new SimpleLink(data+1,size-1);
      else  // the last link
         next = NULL;
      count++;
   }

   SimpleLink::~SimpleLink() { count--; }
   // accessors
   int SimpleLink::getDatum() { return datum; }
   int SimpleLink::getcount() { return count; }
   SimpleLink* SimpleLink::getNext() { return next; }
   SimpleLink* SimpleLink::last() {
	SimpleLink *temp = this;
	SimpleLink *temp1 = temp;	
	while(temp->getNext() != NULL){
		temp = temp1->getNext();
		temp1 = temp;
	}
	return temp;
	
   }
   void SimpleLink::insertAfter(int num){
	SimpleLink *temp = new SimpleLink(num);
	temp->setNext(next);
	next = temp;
   }
   void SimpleLink::insertAfter(int *numArray, int size){	
	SimpleLink *temp1;
	SimpleLink *temp2;
	SimpleLink *temp3;
	temp3 = next;
	temp1 = new SimpleLink(*numArray);
	next = temp1;	
	temp2 = temp1;
	for(int i = 1; i < size; i++){
		temp1 = new SimpleLink(*(numArray+i));
		temp2->setNext(temp1);
		temp2 = temp1;
	}
	temp2->setNext(temp3);
   }
   void SimpleLink::deleteAfter() {
	SimpleLink *temp1 = next;
	next = temp1->getNext();
	delete temp1;
   }
   void SimpleLink::deleteAfter(int num){
	SimpleLink *temp1;	
	for(int i = 0; i < num; i++){
		if (next != NULL) {
			temp1 = next;
			next = temp1->getNext();
			delete temp1;
		}else{
			delete next;
			next = NULL;
		}
	}
   }		
   // setters
   void SimpleLink::setDatum(int newValue) { datum= newValue; }
   void SimpleLink::setNext(SimpleLink *item) { next = item; }
   void SimpleLink::changecount(int x) { count += x; }

DoubleLink.h
Code:
#ifndef DOUBLELINK_H
#define DOUBLELINK_H

#include "SimpleLink.h"

class DoubleLink: public SimpleLink {
public:
	DoubleLink(int newDatum);
	DoubleLink(int *data, int size);
	void setPrev(DoubleLink *item);
	void insertBefore(int num);
	void insertBefore(int *numArray, int size);
	void deleteBefore();
	void deleteBefore(int num);
	DoubleLink *first();
	DoubleLink *getPrev();
        DoubleLink *getNext();
	void insertAfter(int num);
	void insertAfter(int *numArray, int size);
	void deleteAfter();
	void deleteAfter(int num);

protected:
	DoubleLink *previous;
};

#endif
DoubleLink.cpp
Code:
#include <iostream>
#include "DoubleLink.h"

using namespace std;

DoubleLink::DoubleLink(int newDatum):SimpleLink(newDatum){
	previous = NULL;
}
DoubleLink::DoubleLink(int *data, int size){
	previous = NULL;
	datum = data[0];
        if (size!=1){
           next = new DoubleLink(data+1,size-1);
	   next->setPrev(this);
	}
        else {  // the last link
           next = NULL;
	}
        count++;
}
void DoubleLink::setPrev(DoubleLink *item){
	previous = item;
}
DoubleLink* DoubleLink::getPrev(){
	return previous;
}
void DoubleLink::insertBefore(int num){
	DoubleLink *temp1 = previous;
	previous = new DoubleLink(num);
	previous->setPrev(temp1);
	previous->setNext(this);
}
void DoubleLink::insertBefore(int *numArray, int size){
	DoubleLink *temp1 = previous;
	if (size > 0){	
		previous = new DoubleLink(*numArray);
		previous->setNext(this);
		previous->setPrev(temp1);
		this->insertBefore(numArray+1, size-1);
	}
}
void DoubleLink::deleteBefore(){
	if (previous != NULL){
		DoubleLink *temp = previous;
		previous = previous->getPrev();
		previous->setNext(this);
		delete temp;
	}
}
void DoubleLink::deleteBefore(int num){
	DoubleLink *temp = NULL;	
	if (num > 0) {
		temp = previous;
		previous = previous->getPrev();
		previous->setNext(this);
		delete temp;
		if (previous != NULL){
			this->deleteBefore(num-1);
		}
	}
}
DoubleLink* DoubleLink::first(){
	DoubleLink *temp = this;
	DoubleLink *temp1 = temp;	
	while(temp->getPrev() != NULL){
		temp = temp1->getPrev();
		temp1 = temp;
	}
	return temp;
}
DoubleLink* DoubleLink::getNext() { return next; }
void DoubleLink::insertAfter(int num){
	DoubleLink *temp = new DoubleLink(num);
	temp->setNext(next);
	temp->setPrev(this);
	next = temp;
}
void DoubleLink::insertAfter(int *numArray, int size){
	DoubleLink *temp1 = next;
	if (size > 0){	
		next = new DoubleLink(*numArray);
		next->setNext(temp1);
		next->setPrev(this);
		next->insertAfter(numArray+1, size-1);
	}
}
void DoubleLink::deleteAfter() {
	DoubleLink *temp1 = next;
	next = temp1->getNext();
	next->setPrev(this);
	delete temp1;
}
void DoubleLink::deleteAfter(int num){
	DoubleLink *temp1;	
	for(int i = 0; i < num; i++){
		if (next != NULL) {
			temp1 = next;
			next = temp1->getNext();
			next->setPrev(this);
			delete temp1;
		}else{
			delete next;
			next = NULL;
		}
	}
}
main.cpp
Code:
#include <iostream>
using namespace std;

#include "SimpleLink.h"
#include "DoubleLink.h"

int countList(SimpleLink * const aList) {	
	return aList->getcount();
}


int printList(SimpleLink * const foo) {
	SimpleLink * temp1 = foo;
	SimpleLink * temp2 = foo;
	cout << "List: ";
	while (temp1->getNext() != NULL) {
		cout << temp1->getDatum() << " ";
		temp1 = temp2->getNext();
		temp2 = temp1;
	}
	cout << temp1->getDatum() << endl;
}

int printList(DoubleLink * const foo) {
	DoubleLink * temp1 = foo;
	DoubleLink * temp2 = foo;
	cout << "List: ";
	while (temp1->getNext() != NULL) {
		cout << temp1->getDatum() << " ";
		temp1 = temp2->getNext();
		temp2 = temp1;
	}
	cout << temp1->getDatum() << endl;
}

int main() {
	//Problem 1	
	//Part A
	int a[8] = {3,1,4,1,6,9,2,6};
	int size = 8;
/*
	SimpleLink *newList = new SimpleLink(a,size);
	cout << newList->getNext()->getNext()->getDatum() << endl;
	SimpleLink *temp1 = newList->getNext();
	SimpleLink *temp2 = newList;
        newList = temp1;
	delete temp2;

	//Problem 1	
	//Part B
	cout << endl << "Test of countList and printList" << endl;
	cout << "Count: " << countList(newList) << endl;
	printList(newList);
	cout << "Last: " << newList->last()->getDatum() << endl;
	
	//Problem 1
	//Part C
	cout << endl << "Test of insertAfter(int)" << endl;
	newList->insertAfter(5);
	printList(newList);
	cout << "Count: " << countList(newList) << endl;
	cout << "Test of insertAfter(array, size)" << endl;
	newList->insertAfter(a,size);
	printList(newList);
	cout << "Count: " << countList(newList) << endl;
	cout << "Test of deleteAfter()" << endl;
	newList->deleteAfter();
	printList(newList);
	cout << "Count: " << countList(newList) << endl;
	cout << "Test of deleteAfter(int)" << endl;
	newList->deleteAfter(7);
	printList(newList);
	cout << "Count: " << countList(newList) << endl;
*/
	//Problem 2
	//Part A
	DoubleLink *newList2 = new DoubleLink(a,size);
	cout << newList2->getNext()->getNext()->getDatum() << endl;
	DoubleLink *temp11 = newList2->getNext();
	DoubleLink *temp12 = newList2;
        newList2 = temp11;
	delete temp12;

	cout << endl << "Test of countList and printList" << endl;
	cout << "Count: " << countList(newList2) << endl;
	printList(newList2);
	cout << "Last: " << newList2->last()->getDatum() << endl;
	cout << "First: " << newList2->first()->getDatum() << endl;
	
	cout << endl << "Test of insertAfter(int)" << endl;
	newList2->insertAfter(5);
	printList(newList2);
	cout << "Count: " << countList(newList2) << endl;
	cout << "Test of insertAfter(array, size)" << endl;
	newList2->insertAfter(a,size);
	printList(newList2);
	cout << "Count: " << countList(newList2) << endl;
	cout << "Test of deleteAfter()" << endl;
	newList2->deleteAfter();
	printList(newList2);
	cout << "Count: " << countList(newList2) << endl;
	cout << "Test of deleteAfter(int)" << endl;
	newList2->deleteAfter(7);
	printList(newList2);
	cout << "Count: " << countList(newList2) << endl;

	cout << endl << "Test of insertBefore(int)" << endl;
	newList2->insertBefore(5);
	printList(newList2);
	cout << "Count: " << countList(newList2) << endl;
	cout << "Test of insertBefore(array, size)" << endl;
	newList2->insertBefore(a,size);
	printList(newList2);
	cout << "Count: " << countList(newList2) << endl;
	cout << "Test of deleteBefore()" << endl;
	newList2->deleteBefore();
	printList(newList2);
	cout << "Count: " << countList(newList2) << endl;
	cout << "Test of deleteBefore(int)" << endl;
	newList2->deleteBefore(7);
	printList(newList2);
	cout << "Count: " << countList(newList2) << endl;

	return 0;
}

Unfortunately that code gives me the following when trying to compile it:
Code:
vlsi37:~/EC327/Homework/2$ g++ SimpleLink.cpp DoubleLink.cpp main.cpp -o Main
DoubleLink.cpp: In constructor ‘DoubleLink::DoubleLink(int*, int)’:
DoubleLink.cpp:14: error: ‘class SimpleLink’ has no member named ‘setPrev’
DoubleLink.cpp: In member function ‘DoubleLink* DoubleLink::getNext()’:
DoubleLink.cpp:71: error: invalid conversion from ‘SimpleLink*’ to ‘DoubleLink*’
DoubleLink.cpp: In member function ‘void DoubleLink::insertAfter(int*, int)’:
DoubleLink.cpp:79: error: invalid conversion from ‘SimpleLink*’ to ‘DoubleLink*’
DoubleLink.cpp:83: error: ‘class SimpleLink’ has no member named ‘setPrev’
DoubleLink.cpp: In member function ‘void DoubleLink::deleteAfter()’:
DoubleLink.cpp:88: error: invalid conversion from ‘SimpleLink*’ to ‘DoubleLink*’
DoubleLink.cpp:90: error: ‘class SimpleLink’ has no member named ‘setPrev’
DoubleLink.cpp: In member function ‘void DoubleLink::deleteAfter(int)’:
DoubleLink.cpp:97: error: invalid conversion from ‘SimpleLink*’ to ‘DoubleLink*’
DoubleLink.cpp:99: error: ‘class SimpleLink’ has no member named ‘setPrev’

So as you can see there are two problems here. First is that I try to occasionally do a conversion between SimpleLink and DoubleLink objects. Unfortunately I still haven't found a fix for this so any input would be greatly appreciated;
Second I try to use DoubleLink functions with next which is a SimpleLink object. This was the reason I was trying to use virtual functions. However my attempts proved futile as I'm no closer to working code than I was 2 hours ago.:( So I could either use suggestions on how to work around this constraint or use virtual functions in the SimpleLink class.
Anyway I having troube keeping my eyes open and this one assignment has about destroyed me mentally. I think it is best that I get an hour or two of sleep before having to wake up early today to do yet another engineering assignment. Anyway my thanks to the [H] community for all the help you have given me. God Bless you guys. Special thanks to reil who has been very active during this whole ordeal. But sadly its not over yet. Any and all suggestiosn for making this program work will be greatly appreciated. PLEASE HELP.:(
 
Since you're using the next variable from SimpleLink, it is restricted by the SimpleLink interface. If you want to use the DoubleLink methods, you have to cast the variable.
Code:
DoubleLink *temp1 = (DoubleLink*)next;
Generally you have to be careful with this and ensure that you are casting only when you're sure that the type is correct.
 
reil, as always thanks for your input, your suggestion helped alot and I got this program to compile. My new DoubleLink.cpp and main files are:

DoubleLink.cpp
Code:
#include <iostream>
#include "DoubleLink.h"

using namespace std;

DoubleLink::DoubleLink(int newDatum):SimpleLink(newDatum){
	previous = NULL;
}
DoubleLink::DoubleLink(int *data, int size){
	DoubleLink *temp = NULL;
	previous = NULL;
	datum = data[0];
        if (size!=1){
           temp = new DoubleLink(data+1,size-1);
	   next = (SimpleLink*)temp;
	   temp->setPrev(this);
	}
        else {  // the last link
           next = NULL;
	}
        count++;
}
void DoubleLink::setPrev(DoubleLink *item){
	previous = item;
}
DoubleLink* DoubleLink::getPrev(){
	return previous;
}
void DoubleLink::insertBefore(int num){
	DoubleLink *temp1 = previous;
	previous = new DoubleLink(num);
	previous->setPrev(temp1);
	previous->setNext(this);
}
void DoubleLink::insertBefore(int *numArray, int size){
	DoubleLink *temp1 = previous;
	if (size > 0){	
		previous = new DoubleLink(*numArray);
		previous->setNext(this);
		previous->setPrev(temp1);
		this->insertBefore(numArray+1, size-1);
	}
}
void DoubleLink::deleteBefore(){
	if (previous != NULL){
		DoubleLink *temp = previous;
		previous = previous->getPrev();
		previous->setNext(this);
		delete temp;
	}
}
void DoubleLink::deleteBefore(int num){
	DoubleLink *temp = NULL;	
	if (num > 0) {
		temp = previous;
		previous = previous->getPrev();
		previous->setNext(this);
		delete temp;
		if (previous != NULL){
			this->deleteBefore(num-1);
		}
	}
}
DoubleLink* DoubleLink::first(){
	DoubleLink *temp = this;
	DoubleLink *temp1 = temp;	
	while(temp->getPrev() != NULL){
		temp = temp1->getPrev();
		temp1 = temp;
	}
	return temp;
}
void DoubleLink::insertAfter(int num){
	DoubleLink *temp = new DoubleLink(num);
	temp->setNext(next);
	temp->setPrev(this);
	next = temp;
}
void DoubleLink::insertAfter(int *numArray, int size){
	DoubleLink *temp1 = (DoubleLink*)next;
	DoubleLink *temp2 = (DoubleLink*)next;
	if (size > 0){	
		temp2 = new DoubleLink(*numArray);
		next = (SimpleLink*)temp2;
		temp2->setNext(temp1);
		temp2->setPrev(this);
		temp2->insertAfter(numArray+1, size-1);
	}
}
void DoubleLink::deleteAfter() {
	DoubleLink *temp1 = (DoubleLink*)next;
	next = temp1->getNext();
	DoubleLink *temp2 = (DoubleLink*)next;
	temp2->setPrev(this);
	delete temp1;
}
void DoubleLink::deleteAfter(int num){
	DoubleLink *temp1 = NULL;
	DoubleLink *temp2 = NULL;	
	for(int i = 0; i < num; i++){
		if (next != NULL) {
			temp1 = (DoubleLink*)next;
			next = temp1->getNext();
			temp2 = (DoubleLink*)next;
			temp2->setPrev(this);
			delete temp1;
		}
	}
}

main.cpp
Code:
#include <iostream>
using namespace std;

#include "SimpleLink.h"
#include "DoubleLink.h"

int countList(SimpleLink * const aList) {	
	return aList->getcount();
}


int printList(SimpleLink * const foo) {
	SimpleLink * temp1 = foo;
	SimpleLink * temp2 = foo;
	cout << "List: ";
	while (temp1->getNext() != NULL) {
		cout << temp1->getDatum() << " ";
		temp1 = temp2->getNext();
		temp2 = temp1;
	}
	cout << temp1->getDatum() << endl;
}

int printList(DoubleLink * const foo) {
	DoubleLink * temp1 = foo;
	DoubleLink * temp2 = foo;
	cout << "List: ";
	while (temp1->getNext() != NULL) {
		cout << temp1->getDatum() << " ";
		temp1 = (DoubleLink*)temp2->getNext();
		temp2 = temp1;
	}
	cout << temp1->getDatum() << endl;
}

int main() {
	//Problem 1	
	//Part A
	int a[8] = {3,1,4,1,6,9,2,6};
	int size = 8;
/*
	SimpleLink *newList = new SimpleLink(a,size);
	cout << newList->getNext()->getNext()->getDatum() << endl;
	SimpleLink *temp1 = newList->getNext();
	SimpleLink *temp2 = newList;
        newList = temp1;
	delete temp2;

	//Problem 1	
	//Part B
	cout << endl << "Test of countList and printList" << endl;
	cout << "Count: " << countList(newList) << endl;
	printList(newList);
	cout << "Last: " << newList->last()->getDatum() << endl;
	
	//Problem 1
	//Part C
	cout << endl << "Test of insertAfter(int)" << endl;
	newList->insertAfter(5);
	printList(newList);
	cout << "Count: " << countList(newList) << endl;
	cout << "Test of insertAfter(array, size)" << endl;
	newList->insertAfter(a,size);
	printList(newList);
	cout << "Count: " << countList(newList) << endl;
	cout << "Test of deleteAfter()" << endl;
	newList->deleteAfter();
	printList(newList);
	cout << "Count: " << countList(newList) << endl;
	cout << "Test of deleteAfter(int)" << endl;
	newList->deleteAfter(7);
	printList(newList);
	cout << "Count: " << countList(newList) << endl;
*/
	//Problem 2
	//Part A
	DoubleLink *newList2 = new DoubleLink(a,size);
	cout << newList2->getNext()->getNext()->getDatum() << endl;
	DoubleLink *temp11 = (DoubleLink*)newList2->getNext();
	DoubleLink *temp12 = newList2;
        newList2 = temp11;
	delete temp12;

	cout << endl << "Test of countList and printList" << endl;
	cout << "Count: " << countList(newList2) << endl;
	printList(newList2);
	cout << "Last: " << newList2->last()->getDatum() << endl;
	cout << "First: " << newList2->first()->getDatum() << endl;
	
	cout << endl << "Test of insertAfter(int)" << endl;
	newList2->insertAfter(5);
	printList(newList2);
	cout << "Count: " << countList(newList2) << endl;
	cout << "Test of insertAfter(array, size)" << endl;
	newList2->insertAfter(a,size);
	printList(newList2);
	cout << "Count: " << countList(newList2) << endl;
	cout << "Test of deleteAfter()" << endl;
	newList2->deleteAfter();
	printList(newList2);
	cout << "Count: " << countList(newList2) << endl;
	cout << "Test of deleteAfter(int)" << endl;
	newList2->deleteAfter(7);
	printList(newList2);
	cout << "Count: " << countList(newList2) << endl;

	cout << endl << "Test of insertBefore(int)" << endl;
	newList2->insertBefore(5);
	printList(newList2);
	cout << "Count: " << countList(newList2) << endl;
	cout << "Test of insertBefore(array, size)" << endl;
	newList2->insertBefore(a,size);
	printList(newList2);
	cout << "Count: " << countList(newList2) << endl;
	cout << "Test of deleteBefore()" << endl;
	newList2->deleteBefore();
	printList(newList2);
	cout << "Count: " << countList(newList2) << endl;
	cout << "Test of deleteBefore(int)" << endl;
	newList2->deleteBefore(7);
	printList(newList2);
	cout << "Count: " << countList(newList2) << endl;

	return 0;
}

Now there is a new problem. The code gives the following output:
Code:
vlsi29:~/EC327/Homework/2$ g++ SimpleLink.cpp DoubleLink.cpp main.cpp -o Main
vlsi29:~/EC327/Homework/2$ ./Main
4

Test of countList and printList
Count: 7
List: 1 4 1 6 9 2 6
Last: 6
First: 0

Test of insertAfter(int)
List: 1 5 4 1 6 9 2 6
Count: 8
Test of insertAfter(array, size)
List: 1 3 1 4 1 6 9 2 6 5 4 1 6 9 2 6
Count: 16
Test of deleteAfter()
List: 1 1 4 1 6 9 2 6 5 4 1 6 9 2 6
Count: 15
Test of deleteAfter(int)
List: 1 5 4 1 6 9 2 6
Count: 8

Test of insertBefore(int)
List: 1 5 4 1 6 9 2 6
Count: 9
Test of insertBefore(array, size)
List: 1 5 4 1 6 9 2 6
Count: 17
Test of deleteBefore()
List: 1 5 4 1 6 9 2 6
Count: 16
Test of deleteBefore(int)
List: 1 5 4 1 6 9 2 6
Count: 9

For some reason my first function is not working properly. I looked at it and tried to debug it but I can't see anything wrong. Can anyone else see something wrong with that function? Thanks
 
To start, your constructors of the type "SimpleLink::SimpleLink(int *data, int size)" need to have checks on the size variable to make sure it's >= 1. If I passed it a size of -1, I'd get an infinite loop that would run the computer out of memory.

Second, testing should be done incrementally. Start by testing the SimpleLink and all of its functions (exhaustively, with a variety of inputs) and identify any problems there, THEN move on to testing the DoubleLink.
 
Thanks, I figured it out. I have to say while I believe Visual Studio promotes lazy programming and a lack of understanding of the compilation promise. But for debugging its pretty nice.:D
 
Back
Top