Question

Here's the code that I'm currently using

template <class T>
float Knapsack<T>::knapTable()
{
  const int MAXSIZE = 40000;
  int temps = nObjects - 1;
  float** memoize = new float*[MAXSIZE];

  for(int y = 0; y < weight[nObjects]; y++)
    memoize[nObjects][y] = 0;

  for(int y = weight[nObjects]; y < knapCap; y++)
    memoize[nObjects][y] = price[y];

  for(int i = temps; i >= 1; i--)
  {
    for(int y = weight[i]; y < knapCap; y++)
      memoize[i][y]= max(memoize[i+1][y], (memoize[i+1][y-weight[i]]+price[i]));
  }

  return memoize[1][nObjects];

}

For some reason I keep getting the error: knapsack.hpp:68:64: error: invalid types ‘float*[float]’ for array subscript.

That's this line: float** memoize = new float*[MAXSIZE];

For some reason the compiler seems to be recognizing MAXSIZE as a float, it's a const int.

Is there a way I can fix this?

Edited for more code header file #ifndef KNAPSACK_H #define KNAPSACK_H

#include <stdexcept>
#include <assert.h>
#include <iostream>
#include <limits.h>
using namespace std;

template <class T>
class Knapsack
{
  private:
    float knapPrice;
    int knapCap, nObjects;
    float weight[40000];
    float price[40000];
  public:

    Knapsack(): knapPrice(0), knapCap(0), nObjects(0) {}
    ~Knapsack() {knapPrice = 0; knapCap = 0;}

    float knapFull (int position, int currentCap);

    float knapTable ();

    float greedyKnap (int currentCap);

    float max(float noAdd,float addOb);

    void printPrice();
    //valueized and valued are modified versions of mergeSort and merge
    //designed to sort two arrays by a fraction of the two.
    void valueize(int ini, int last);

    void valued(int ini, int middle, int last);

    void fillWandP();

    void setNObjects(int n);

    void setKnapCap(int boom);
};
#include "knapsack.hpp"
#endif

Main function //Though I don't think this would affect it #include "sortClass.h" #include "knapsack.h" #include #include #include #include using namespace std;

//mergeSort main;
int main()
{
    Knapsack<float> a;
    float sacked = 0;

    int nO = 18;
    int cap = 700;
    a.setNObjects(nO);

a.setKnapCap(cap);

    a.fillWandP();

    for(int b = 0; b <3800000; b++)//for getting good times
  sacked = b;

    int startAll = clock()*1000000;
    sacked = a.knapFull(1, cap);
    int knapped = clock()*1000000;
    int boom = a.knapTable();
    int tabled = clock()*1000000;
    a.valueize(1, cap);
    int andDone = a.greedyKnap(cap);
    int greedified = clock()*1000000;
    cout<<startAll<<endl;

    greedified = greedified - tabled;
    tabled = tabled - knapped;
    knapped = knapped - startAll;
    cout<<"Recursion profit:"<<sacked<<" Time: "<<knapped<<endl;
    cout<<"Memoization profit:"<<boom<<" Time: "<<tabled<<endl;
    cout<<"Greedy profit: "<<andDone<<" Time: "<<greedified<<endl;



    return 0;
}
Was it helpful?

Solution

weight is declared as float weight[40000] in class Knapsack.

You then use an element of weight as an index into memoize in the knaptable() function:

memoize[i][y]= max(memoize[i+1][y], (memoize[i+1][y-weight[i]]+price[i]));
//                                                  ^^^^^^^^^

And for the record, that's the line that the error is produced for by g++ 4.6.1; it doesn't point to the line where memoize is declared.

OTHER TIPS

Not necessarily related, but you're not using your arrays/pointers correctly. You create your first level of pointers when you call float** memoize = new float*[MAXSIZE] but you then just have an array of pointers, not a double array. You need to initialize each of memoize[i] as an array as well.

That being said, it doesn't look like you should be allocating memory for your memoize array anyway. Just declare it as

float memoize[SIZE][SIZE];

That way, you won't have to worry about memory cleanup or anything, and it makes a lot more sense.

  for(int i = temps; i >= 1; i--)
  {
    for(int y = weight[i]; y < knapCap; y++)
      memoize[i][y]= max(memoize[i+1][y], (memoize[i+1][y-weight[i]]+price[i]));
  }

y-weight[i] is a float. This is your problem.

Once you fix this you will discover that you still have an issue, you're allocating an array of pointers but you also need to allocate the second dimension for each of those pointers before you can use that array.

Something along the lines of:

float** memoize = new float*[MAXSIZE];

for(size_t i = 0; i < MAXSIZE; ++i)
{
  memoize[i] = new float[MAXSIZE];
}

i think maybe you just need to allocate memory for the second pointer ,something like

float** memoize = new float*[MAXSIZE];
    memoize=(float**)malloc(sizeof(float*)*MAXSIZE);
    for(int i=0;i<MAXSIZE;i++)
    {
        memoize[i]=(float*)malloc(sizeof(float)*MAXSIZE);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top