I don't know whats wrong with the code, but keep getting these error messages in visual studio.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Error 18 error C1903: unable to recover from previous error(s); 29
Error 15 error C2039: 'Node' : is not a member of '`global namespace'' 23
Error 6 error C2039: 'node_count' : is not a member of '`global namespace'' 13
Error 5 error C2039: 'node_count_' : is not a member of '`global namespace'' 10
Error 3 error C2059: syntax error : '<'
Error 13 error C2059: syntax error : '<' 23
Error 11 error C2086: 'int CS170::List' : redefinition 23
Error 7 error C2143: syntax error : missing ';' before '{' 14
Error 1 error C2143: syntax error : missing ';' before '<' 10
Error 9 error C2143: syntax error : missing ';' before '<' 23
Error 8 error C2447: '{' : missing function header (old-style formal list?) 14
Error 17 error C2588: '::~Node' : illegal global destructor 29
Error 2 error C2988: unrecognizable template declaration/definition 10
Error 12 error C2988: unrecognizable template declaration/definition 23
Error 4 error C3083: 'Node': the symbol to the left of a '::' 10
Error 14 error C3083: 'Node': the symbol to the left of a '::' must be a type 23
Error 16 error C3083: 'Node': the symbol to the left of a '::' must be a type 29
Error 10 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 23
////////////////////////////////////////////////////////////////////////////////
#ifndef LIST_H
#define LIST_H
////////////////////////////////////////////////////////////////////////////////
#include <iostream> /* ostream, endl */
namespace CS170
{
template <typename T> class List;
template <typename T>
std::ostream & operator<<(std::ostream & os, const List<T> &list);
template <typename T>
class List
{
public:
// Default constructor
List(void);
// Copy constructor for constructing a list from an existing list
List(const List &list);
// Construct a list from a T array
List(const T *array, int size);
// Destructor
~List(void);
void push_front(const T& Value); // adds the item to the front of the list
void push_back(const T& Value); // adds the item to the end of the list
void pop_front(void); // removes the first item in the list
T front(void) const; // retrieves the first item in the list
int size(void) const; // returns the number of items in the list
bool empty(void) const; // true if empty, else false
void clear(void); // clears the list
// Overloaded assignment operator (=) for assigning one list to another
List& operator=(const List &list);
// Overloaded addition operator (+) for adding two lists
List operator+(const List &list) const;
// Overloaded addition/assignment (+=) for adding to a list "in place"
List& operator+=(const List &list);
// Overloaded subscript operators
const T& operator[](int index) const;
T& operator[](int index);
// Output operator for printing lists (<<)
friend std::ostream & operator<< <T> (std::ostream & os, const List &list);
// Returns the number of Nodes that have been created
staticint node_count(void);
private:
// Used to build the linked list
struct Node
{
Node *next; // pointer to the next Node
T data; // the actual data in the node
Node(T value); // non-default constructor (conversion)
~Node(void); // destructor
staticint node_count_; // number of Nodes created
};
Node *head_; // pointer to the head of the list
Node *tail_; // pointer to the last node
int size_; // number of items on the list
// All nodes are created in this method
Node *new_node(const T& data) const;
};
} // namespace CS170
#include "List.cpp"
#endif
////////////////////////////////////////////////////////////////////////////////
namespace CS170
{
struct Node
{
Node *next; // pointer to the next Node
T data; // the actual data in the node
Node(T value); // non-default constructor (conversion)
~Node(void); // destructor
staticint node_count_; // number of Nodes created
};
//rest of your code here
inside the namespace but outside the class
you might have to put it outside the namespace, not sure
////////////////////////////////////////////////////////////////////////////////
#ifndef LIST_H
#define LIST_H
////////////////////////////////////////////////////////////////////////////////
#include <iostream> /* ostream, endl */
#include "List.cpp"
namespace CS170
{
template <typename T> class List;
template <typename T>
std::ostream & operator<<(std::ostream & os, const List<T> &list);
// Used to build the linked list
struct Node
{
Node *next; // pointer to the next Node
T data; // the actual data in the node
Node(T value); // non-default constructor (conversion)
~Node(void); // destructor
staticint node_count_; // number of Nodes created
};
template <typename T>
class List
{
public:
// Default constructor
List(void);
// Copy constructor for constructing a list from an existing list
List(const List &list);
// Construct a list from a T array
List(const T *array, int size);
// Destructor
~List(void);
void push_front(const T& Value); // adds the item to the front of the list
void push_back(const T& Value); // adds the item to the end of the list
void pop_front(void); // removes the first item in the list
T front(void) const; // retrieves the first item in the list
int size(void) const; // returns the number of items in the list
bool empty(void) const; // true if empty, else false
void clear(void); // clears the list
// Overloaded assignment operator (=) for assigning one list to another
List& operator=(const List &list);
// Overloaded addition operator (+) for adding two lists
List operator+(const List &list) const;
// Overloaded addition/assignment (+=) for adding to a list "in place"
List& operator+=(const List &list);
// Overloaded subscript operators
const T& operator[](int index) const;
T& operator[](int index);
// Output operator for printing lists (<<)
friend std::ostream & operator<< <T> (std::ostream & os, const List &list);
// Returns the number of Nodes that have been created
staticint node_count(void);
private:
Node *head_; // pointer to the head of the list
Node *tail_; // pointer to the last node
int size_; // number of items on the list
// All nodes are created in this method
Node *new_node(const T& data) const;
};
} // namespace CS170
#endif
////////////////////////////////////////////////////////////////////////////////
also, im not sure if you need this line template <typename T> class List;
Thanks for the answer CreativeMFS, but the problem is I am not allowed to change anything inside the .h file. There has to be another way to make it compile.
sheesh, wierd assignment, it might not be able to compile, what version of VS was it made in and what version are you using. I don't know enough about the compiler options to even go there. Why can't you change it, is that your assignment, to learn about the compiler options or something?
List(const List &list); is wrong because &list should be &otherlist.
Just seems like there is a lot wrong with the header, but I am a fairly new programmer so I can not be %100 certain.
I tried it in both visual studio 2008 and 2010. got the same errors.
List(const List &list); is just a copy constructor there is nothing wrong with that statement.
it takes a constant reference to a List object which allows the user to construct a new object.
such as;
oh sorry, i didn't realize the capital L before. I was looking at List and list as if they were the same.
I notice that the first error is line 10 template <typename T> class List;
that is something I have not seen before, line 10 and line 15/16 are the same lines of code, seems like the class is being declared twice.
I have a linked list header that I wrote and originally I had the node struct in the class and had to move it out of the class for my program to work. That is the only way I know.