Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 03-16-09   #1 (permalink)
PC Gamer
 
Col. Newman's Avatar
 
amd ati

Join Date: Apr 2008
Location: Omaha, Nebraska
Posts: 813

Rep: 31 Col. Newman is acknowledged by some
Unique Rep: 27
Folding Team Rank: 11
Trader Rating: 2
Default Solved: C++ create a linked list node as a class: HELP

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:
PHP 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 Typeelem);
//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 TypeelemnodeType<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;
}; 
this is what I have
PHP 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 Typeelem);
//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 TypeelemnodeType<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 Typeelem){
    
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 TypeelemnodeType<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(){
    
//} 
and in the main file
PHP Code:
#include <stdlib.h>

#include "nodeType.h"

using namespace std;

int main(int argcchar** argv) {
    
nodeType<Typefirst;
    
nodeType<Typelast;
    
nodeType<TypenewNode;
    
first.setLink(newNode);
    
newNode.setInfo(1);
    
newNode.setLink(last);
    return (
EXIT_SUCCESS);


Solved:

PHP 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); 
I was was frustrated and didn't notice the mega fail.
__________________
DDR != GDDR, DDR2 != GDDR2, DDR3 != GDDR3, DDR4 != GDDR4, DDR5 != GDDR5
So STOP using the terms interchangeably people

System: Main Rig
CPU
AMD Phenom II 955BE
Motherboard
Gigabyte MA790FXT-UD5P
Memory
2x2GB Patriot Viper DDR3 1600
Graphics Card
Sapphire HD 4850 X2 2GB
Hard Drive
Samsung 500GB SATA II
Power Supply
Antec TPQ-850 850W
Case
Antec 900
CPU cooling
Xigmatek HDT-S1283
GPU cooling
Stock
OS
Windows Vista Ultimate x64 / Kubuntu 9.10 64
Monitor
Samsung 26" LCD, Acer 22" LCD
10 Million+ Folding at Home points

Last edited by Col. Newman : 03-16-09 at 07:36 AM
Col. Newman is offline I fold for Overclock.net   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 02:08 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.09723 seconds with 8 queries