1

how would i set this to null;

LookupTable<Product *> table;

everything i try says

1 IntelliSense: no suitable constructor exists to convert from "int" to "LookupTable"

Here is what Lookup table is:

#ifndef LOOKUPTABLE_H
#define LOOKUPTABLE_H

#include <iostream>
#include <string>

using namespace std;

#define MAXRANGE 10

template <class T>
class LookupTable
{
private:
    T *aptr[MAXRANGE];
    int rangeStart[MAXRANGE];
    int rangeEnd[MAXRANGE];
    int numRanges;

public:
    T defaultValue;
    bool failedRangeCheck;
    std::string failReason;


    // Constructor
    LookupTable() 
    {   
        numRanges = 0; 
        defaultValue = T();
    }      

    void addRange(int start, int end)
    {
        std::cout << "Created a new range...  Start: " << start << " / End: " << end << endl;
        failedRangeCheck = false;

        //lines omitted because not working anyway

        if ( !failedRangeCheck )
        {
            //set ranges
            rangeStart[numRanges] = start;
            rangeEnd[numRanges] = end;

            //build new generic array with end-start+1 positions
            //set pointer to point to it
            aptr[numRanges] = new T[ end - start + 1 ];
            numRanges++;
        }
        else
        {
            std::cout << "Range overlapped another range." << endl;
            std::cout << failReason << endl;
        }
    }

    T &operator[](int value)     // Overloaded [] operator
    {
        for ( int i = 0; i < numRanges; i++ )
        {
            if ( (value >= rangeStart[i]) && (value <= rangeEnd[i]) )
            {
                return aptr[i][value - rangeStart[i]];
            }
        }

        return defaultValue;
    }

    ~LookupTable()
    {
         delete[] aptr;
         numRanges = 0;     
    }

};
#endif
JuggernautDad
  • 1,135
  • 2
  • 13
  • 28
  • 3
    Set *what* to null? You have an object of some unknown template type `LookupTable`, which nobody ever heard about before. How do you expect anyone to know how to "set" this object "to null"? And even what "set to null" is supposed to mean when applied to this object? – AnT stands with Russia Nov 04 '10 at 01:16
  • This appears to be an extension of his previous question: http://stackoverflow.com/questions/4092747/c-pointers-to-objects-type-checking And I believe a full answer of what he really needs only makes sense in the context there... – TheUndeadFish Nov 04 '10 at 01:43
  • it is, because i was never given an answer that worked or made sense for my situation... I was pointed in the right direction, but need a little more help – JuggernautDad Nov 04 '10 at 01:47
  • What do you want setting a LookupTable to NULL to do? – Crashworks Nov 04 '10 at 02:22

4 Answers4

4

In your example, table isn't a pointer. It is an object of type LookupTable, where LookupTable is a template class. In this case, you happen to be making a LookupTable specialized for Product *s, but you would get the same error if you did LookupTable< int > table = 0;

Crashworks
  • 40,496
  • 12
  • 101
  • 170
0

You're declaring table as a type of LookUpTable with the meta type of pointer to a Product.

Chad
  • 2,938
  • 3
  • 27
  • 38
0

That's a table of pointers, not a pointer to a table (of pointers). Did you mean LookupTable<Product *> *table; ?

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
0

Here's how you set it to NULL - you define it as a different type:

LookupTable<Product *>* table = NULL;

If you are used to dealing with C#, then you might be able to think of classes as "value types".

This might also behave differently than you think. In C++:

LookupTable<Product *> table1;
LookupTable<Product *> table2 = table;

When you edit table2, table1 will not change.

And:

void SomeFunction(LookupTable<Product *> t)
{
    // do something with t
}

// ...

LookupTable<Product *> table;
SomeFunction(table);

If SomeFunction edits t, then table doesn't change.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183