Overclock.net banner

1 - 1 of 1 Posts

·
Registered
Joined
·
1,234 Posts
Discussion Starter #1
ok this is a class exercise.

(I am quite sure that there are some logic errors dealing with the pointer I haven't gotten that far yet)

How do I declare a new instance of the class nodeType? I have tried all sorts of things. I never did fully understand templates we didn't get much experience with them in my last class.

There have some epic fails in there somewhere that I have missed but I am to frustrated currently to find them.

The instructions

Quote:
In the is chapter the, the class to implement the nodes of a linked list is defined as a struct. the following rewrites the definition of the struct nodeType so that is is declared as a class and the member variables are private:

Code:

Code:
[CODE]
#ifndef H_NodeType
#define H_NodeType

#include <iostream>

using namespace std;

template <class Type>
class nodeType
{
public:
const nodeType<Type>& operator=(const nodeType<Type>&);
//Overload the assignment operator.

void setInfo(const Type& elem);
//Function to set the info of the node.
//Postcondition: info = elem;

Type getInfo() const;
//Function to return the info of the node.
//Postcondition: The value of info is returned.

void setLink(nodeType<Type> *ptr);
//Function to set the link of the node.
//Postcondition: link = ptr;

nodeType<Type>* getLink() const;
//Function to return the link of the node.
//Postcondition: The value of link is returned.

nodeType();
//Default constructor
//Postcondition: link = NULL;

nodeType(const Type& elem, nodeType<Type> *ptr);
//Constructor with parameters
//Sets info point to the object elem points to and
//link is set to point to the object ptr points to.
//Postcondition: info = elem; link = ptr

nodeType(const nodeType<Type> &otherNode);
//Copy constructor

~nodeType();
//Destructor

private:
Type info;
nodeType<Type> *link;
};
[/CODE]

this is what I have

Code:

Code:
[CODE]
#ifndef _NODETYPE_H
#define    _NODETYPE_H

#include <iostream>

using namespace std;

template <class Type> class nodeType {
public:
const nodeType<Type>& operator=(const nodeType<Type>&);
//Overload the assignment operator.

void setInfo(const Type& elem);
//Function to set the info of the node.
//Postcondition: info = elem;

Type getInfo() const;
//Function to return the info of the node.
//Postcondition: The value of info is returned.

void setLink(nodeType<Type> *ptr);
//Function to set the link of the node.
//Postcondition: link = ptr;

nodeType<Type>* getLink() const;
//Function to return the link of the node.
//Postcondition: The value of link is returned.

nodeType();
//Default constructor
//Postcondition: link = NULL;

nodeType(const Type& elem, nodeType<Type> *ptr);
//Constructor with parameters
//Sets info point to the object elem points to and
//link is set to point to the object ptr points to.
//Postcondition: info = elem; link = ptr

nodeType(const nodeType<Type> &otherNode);
//Copy constructor

~nodeType();
//Destructor

private:
Type info;
nodeType<Type> *link;
};

template <class Type>
const nodeType<Type>& nodeType<Type>::operator=(const nodeType<Type>& n){
    info = n.info;
    link = n.link;
}

template <class Type>
void nodeType<Type>::setInfo(const Type& elem){
    info = elem;
}
template <class Type>
Type nodeType<Type>::getInfo() const{
    return info;
}
template <class Type>
void nodeType<Type>::setLink(nodeType<Type> *ptr){
    link = ptr;
}
template <class Type>
nodeType<Type>* nodeType<Type>::getLink() const{
    return link;
}
template <class Type>
nodeType<Type>::nodeType(){
    link = NULL;
}
template <class Type>
nodeType<Type>::nodeType(const Type& elem, nodeType<Type> *ptr){
    info = elem;
    link = ptr;
}
template <class Type>
nodeType<Type>::nodeType(const nodeType<Type> &otherNode){
    otherNode.setInfo(info);
    otherNode.setLink(link);
    //return otherNode;
}

//~nodeType::nodeType(){

//}
[/CODE]

and in the main file

Code:

Code:
[CODE]
#include <stdlib.h>

#include "nodeType.h"

using namespace std;

int main(int argc, char** argv) {
    nodeType<Type> first;
    nodeType<Type> last;
    nodeType<Type> newNode;
    first.setLink(newNode);
    newNode.setInfo(1);
    newNode.setLink(last);
    return (EXIT_SUCCESS);
}
[/CODE]


Solved:


Code:

Code:
[CODE]
    nodeType<int> *first = new nodeType<int>;
    nodeType<int> *last = new nodeType<int>;
    nodeType<int> *newNode = new nodeType<int>;
    first->setLink(newNode);
    newNode->setInfo(1);
    newNode->setLink(last);
[/CODE]

I was was frustrated and didn't notice the mega fail.
 
1 - 1 of 1 Posts
Top