0

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
4
  • 4
    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? Commented Nov 4, 2010 at 1:16
  • This appears to be an extension of his previous question: stackoverflow.com/questions/4092747/… And I believe a full answer of what he really needs only makes sense in the context there... Commented Nov 4, 2010 at 1: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 Commented Nov 4, 2010 at 1:47
  • 1
    What do you want setting a LookupTable to NULL to do? Commented Nov 4, 2010 at 2:22

4 Answers 4

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;

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.