compiler can't find memeber function
Jun 28, 2010 at 11:08pm UTC
In the test script. I'm just trying to add a value to a integer list.
the error message is wierd:
testList.cpp:6: error: request for member ‘add’ in ‘nums’, which is of non-class type ‘list<int>()’
This is my test script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include "list.h"
int main () {
list<int > nums ();
nums.add(0);
/* nums.add(1);
nums.add(2);
std::cout << nums.firstElement() << std::endl;
std::cout << nums.include(1) << std::endl;
std::cout << nums.include(2) << std::endl;
nums.removeFirst();
std::cout << nums.include(2) << std::endl;
*/
return 0;
}
This is my template implementation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
#ifndef LIST_H
#define LIST_H
template <class T>
class link2 {
public :
// the link2 ADT is the actual chain of values
link2* insert (T val);
//returns the head
private :
//constructor
link2(T & value, link2* nextPtr);
//duplicate a whole link2
link2* duplicate();
//data area
T value;
link2* ptrToNextlink2;
friend class list <T>;
// friend class listIterator<T>;
};
template <class T>
link2<T> :: link2 (T & val, link2<T>* nextPtr) {
//this creates a new link2 , think of it as a new node, which encode
//the value and the ptr to the next link2(node)
value = val;
ptrToNextlink2 = nextPtr;
}
template <class T>
link2<T> * link2<T> :: insert (T value) {
//insert FOLLOWING the current element
ptrToNextlink2 = new link2<T> (value, ptrToNextlink2);
assert(ptrToNextlink2 != 0);
return ptrToNextlink2;
}
template <class T>
link2<T> * link2<T> :: duplicate () {
//duplicate and return a ptr to a link2 that encodes all the information to
//the current link2
link2<T> * result ;
if (ptrToNextlink2 == 0)
result = new link2<T> (value, 0);
else
result = new link2<T> (value, ptrToNextlink2->duplicate());
assert(result!=0);
return result;
}
template <class T>
class list {
public :
//constructors
list ();
list (const list & source);
virtual ~list ();
//ADT operators
virtual void add (T value);
virtual void deleteAllValues ();
T firstElement () const ;
virtual int includes (T value) const ;
int isEmpty () const ;
virtual void removeFirst ();
protected :
//data field
link2<T> * ptrToFirstlink2;
// friend class listIterator<T>;
};
template <class T>
list<T> :: list() : ptrToFirstlink2(0) {}
template <class T>
void list<T> :: add (T value) {
//add a value to the current list
ptrToFirstlink2 = new link2<T> (value, ptrToFirstlink2);
assert(ptrToFirstlink2!=0);
}
template <class T>
int list<T> :: isEmpty() const {
return (0==ptrToFirstlink2);
}
template <class T>
T list <T> :: firstElement () const {
assert(!isEmpty());
return ptrToFirstlink2->value;
}
template <class T>
void list<T> :: removeFirst() {
//remove first element
assert(!isEmpty());
link2<T> * temp = ptrToFirstlink2;
ptrToFirstlink2 = ptrToFirstlink2 -> ptrToNextlink2;
delete temp;
}
template <class T>
int list<T> :: includes (T val) const {
link2<T> * iter = ptrToFirstlink2;
while (iter!=0) {
if (iter->value = val)
return 1;
iter = iter -> ptrToNextlink2;
}
return 0;
}
template <class T>
list<T> :: ~list () {deleteAllValues(); }
template <class T>
void list<T> :: deleteAllValues () {
if (ptrToFirstlink2 != 0) {
link2<T> *iter = ptrToFirstlink2;
ptrToFirstlink2 = ptrToFirstlink2 -> ptrToNextlink2;
delete iter;
}
}
template <class T>
list<T> :: list (const list<T> & source) {
if (source.isEmpty())
ptrToFirstlink2 = 0;
else
ptrToFirstlink2 = (source.ptrToFirstlink2)->duplicate();
}
#endif
Jun 28, 2010 at 11:12pm UTC
1 2 3 4 5
#include <iostream>
#include "list.h"
int main () {
list<int > nums (); //ERROR - this is a function declaration - NOT a list<int> variable called nums created using the default constructor.
You need to remove the () from that line.
Jun 28, 2010 at 11:14pm UTC
ohhhh, i've actually read about this from a book.
Thanks a lot.
Topic archived. No new replies allowed.