I'm trying to implement a SmartPtr class in my code that used to use normal pointers. I've looked at other sof questions and their solutions seem to be what I'm doing, so I'm not sure what's wrong. I had to define my global in graph.h because a function parameter, shown, uses it.
./graph.h:14:1: error: unknown type name 'null_adj'
null_adj.id = -1;
^
./graph.h:14:9: error: cannot use dot operator on a type
null_adj.id = -1;
^
2 errors generated.
I define it in graph.h:
#include "node.h"
#include "SmartPtr.cpp"
using namespace std;
Adjacency null_adj;
null_adj.id = -1;
SmartPtr<Adjacency> null(&null_adj);
class Graph { ...
void insert_and_delete(stuff, SmartPtr<Adjacency> second_insert = null); ...
This is node.h:
#include "SmartPtr.cpp"
#include <vector>
using namespace std;
struct Adjacency{
public:
int id;
char letter;
int type;
};
class Node { ...
SmartPtr.cpp:
#ifndef SMARTPTR_CPP
#define SMARTPTR_CPP
#include<iostream>
using namespace std;
// A generic smart pointer class
template <class T>
class SmartPtr
{
T *ptr; // Actual pointer
public:
// Constructor
explicit SmartPtr(T *p = NULL) { ptr = p; }
// Destructor
~SmartPtr() { delete(ptr); }
// Overloading dereferncing operator
T & operator * () { return *ptr; }
// Overloding arrow operator so that members of T can be accessed
// like a pointer (useful if T represents a class or struct or
// union type)
T * operator -> () { return ptr; }
};
#endif
What is wrong ??? Edit: look at the follow up in the little box below.