Error: invalid types '<unknown type>[int]' for array s

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 6231 Views - Last Post: 11 October 2012 - 02:18 PM Rate Topic: -----

#1 crapmyster   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 12-October 11

Error: invalid types '<unknown type>[int]' for array s

Posted 10 October 2012 - 03:52 PM

Hello,

I've been trying to work out what I'm doing wrong for ages now. Could someone give me an idea what I'm doing wrong and the way to get round this. What I've got is basically an array of vectors (HashTable) and after inserting integers into the HashTable I want to print them. The error I'm getting is on the 'cout << HashTable[i].at[i] << " " << endl;' line in the print method.

Mark

#include <iostream>
#include <vector>
using namespace std;
const int numOfBuckets = 100;

class HashTable
{
	public: void initialise();
		int hashFunction(int data);
		void insert(int data);
		void deleteData();
		void find();
		void size();
		void print();
        public: vector<int>HashTable[100];
};

int HashTable::hashFunction(int data)
{
    return data%numOfBuckets;
}

void HashTable::insert(int data)
{
        int index;
        index = hashFunction(data);
        HashTable[index].push_back(data);
}

void HashTable::deleteData()
{	
}

void HashTable::find()
{
}

void HashTable::size()
{
}

void HashTable::print()
{   
    int j = 0;
    while(!HashTable[j].empty() && j < HashTable[j].size())
    {
        for (int i = 0; i < HashTable[i].size(); i++)
        {
                cout << HashTable[i].at[i] << "  " << endl;
        }  
        cout << endl;
        j++;
    }
}

class Menu
{ 
	public:  void menu();
                 void insert();
        private: HashTable ht;
};

void Menu::menu()
{
	int temp=0;
	cout << "" << endl;
	cout << "*** Chained HashTable Assignment ***" << endl;
	cout << "Enter a Choice" << endl;
	cout << "Press 'i' to insert an element into the hash table" << endl;
	cout << "Press 'd' to delete an element from the hash table" << endl;
	cout << "Press 'l' to look up an element in the hash table" << endl;
	cout << "Press 's' to obtain the size of the table" << endl;
	cout << "Press 'p' to print the current table" << endl;
        cout << "Press 'e' to exit from the program" << endl;
	cout << "" << endl;
	char input;
	cin >> input;
	while(temp = 1){
		switch(input)
		{
			case 'i': cout << "Type the integer you wish to enter into the hashTable:" << endl;
                                  cout << "" << endl;
                                  int data;
                                  cin >> data;
                                  ht.insert(data);	
                                  menu();
				break;
			case 'd': ht.deleteData();
		        	break;
			case 'f': ht.find();
		        	break;
			case 's': ht.size();
				break;
			case 'p': ht.print();
				break;
                        case 'e': 
                                break;
			default:
				cout << "Error: Wrong selection, choose again" << endl;
				temp = 1;
				break;
		}
	}	
}
int main()
{
	Menu m;
	m.menu();
	return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Error: invalid types '<unknown type>[int]' for array s

#2 jimblumberg   User is online

  • member icon

Reputation: 5864
  • View blog
  • Posts: 17,837
  • Joined: 25-December 09

Re: Error: invalid types '<unknown type>[int]' for array s

Posted 10 October 2012 - 05:56 PM

In future please post the complete error messages exactly as they appear in your development environment. These messages have important information embedded within them to aid in locating and fixing the errors.

In the following line:
                cout << HashTable[i].at[i] << "  " << endl;

There are two problems, the first one is that at is a function so it would use the parentheses not brackets. Second and more important you are trying to combine array notation HashTable[i] with the at() function, you only use one or the other not both. I recommend just using the array notation HashTable[i] instead of the at() function HashTable.at(i).

There is another problem in your class definition the following line is incorrect:

        public: vector<int>HashTable[100];


You are trying to declare an instance of a vector<int>, you don't use the array notation:

public:
   vector<int> HashTable;




Also this line:
        HashTable[index].push_back(data);


You shouldn't be using array notation when you try to push_back() your data.

        HashTable.push_back(data);


I really think you need to review how vectors work, be sure to click the links to explain the constructor, push_back() and any other vector member function you plan to use.


Jim
Was This Post Helpful? 1
  • +
  • -

#3 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Error: invalid types '<unknown type>[int]' for array s

Posted 10 October 2012 - 06:06 PM

Quote

You are trying to declare an instance of a vector<int>, you don't use the array notation

You shouldn't be using array notation when you try to push_back() your data.

I believe he's creating an array of vectors to deal with collisions, it's called separate chaining

You might want to look into quadratic probing to reduce collisions. Using this addressing scheme, you can (but don't quote me) guarantee a unique hash index for every value as long as your table stays half empty. Once the table becomes greater than half of it's capacity, resize the table to twice it's capacity and rehash all of the values to the new hash table.

Here's a link on quadratic probing: http://en.wikipedia....adratic_probing

This post has been edited by jjl: 10 October 2012 - 06:12 PM

Was This Post Helpful? 1
  • +
  • -

#4 jimblumberg   User is online

  • member icon

Reputation: 5864
  • View blog
  • Posts: 17,837
  • Joined: 25-December 09

Re: Error: invalid types '<unknown type>[int]' for array s

Posted 10 October 2012 - 06:18 PM

Quote

I believe he's creating an array of vectors to deal with collisions, it's called separate chaining

Then I recommend a vector of vectors instead of arrays....

Jim
Was This Post Helpful? 0
  • +
  • -

#5 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Error: invalid types '<unknown type>[int]' for array s

Posted 10 October 2012 - 06:26 PM

Quote

Then I recommend a vector of vectors instead of arrays....

Creating a dynamic array for separate chaining isn't a good idea. Every time the size of the array changes, then every value would need to be rehashed. The point of separate chaining is so you never have to resize your hash table, it's fixed, you just create lists of values that map to the same hash index.

This post has been edited by jjl: 10 October 2012 - 06:31 PM

Was This Post Helpful? 0
  • +
  • -

#6 crapmyster   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 12-October 11

Re: Error: invalid types '<unknown type>[int]' for array s

Posted 11 October 2012 - 12:50 AM

Hello,

Just to clear it up I'm creating an array of vectors. I fixed the problem, it was because I was doing this:

cout << HashTable[i].at[i] << "  " << endl;



instead of:

cout << HashTable[i].at(i) << "  " << endl;



Even though the error is disappeared, nothing is being printed out once added elements to it. Must be something to do with the add method any ideas?

And JJ I will be looking into the Quadratic Probing, Thanks,

Mark
Was This Post Helpful? 0
  • +
  • -

#7 crapmyster   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 12-October 11

Re: Error: invalid types '<unknown type>[int]' for array s

Posted 11 October 2012 - 06:20 AM

I have created an array of vectors. I want to be able to print the added elements. I've looked over the logic of the function but can't see what is wrong. There are no errors just that it doesn't print anything. The insert function works correctly. Any help to put me in the right direction would be grateful.

Thanks, Mark

void HashTable::print()
{ 
    for(int i = 0; i < HashTable[i].size(); i++)
    {
        if(HashTable[i].empty())
        {
                cout << HashTable[i].at(i) << "  " << endl;
                cout << endl;
        }
        else
        {
            cout << "There is noting to be printed in the HashTable" << endl;
        }
               
    }
}


Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,995
  • Joined: 08-August 08

Re: Error: invalid types '<unknown type>[int]' for array s

Posted 11 October 2012 - 06:25 AM

If it's empty you shouldn't try to print it.
Was This Post Helpful? 1
  • +
  • -

#9 crapmyster   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 12-October 11

Re: Error: invalid types '<unknown type>[int]' for array s

Posted 11 October 2012 - 06:50 AM

Opps typo. Forgot the !


void HashTable::print()
{ 
    for(int i = 0; i < HashTable[i].size(); i++)
    {
        if(!HashTable[i].empty())
        {
                cout << HashTable[i].at(i) << "  " << endl;
                cout << endl;
        }
        else
        {
            cout << "There is noting to be printed in the HashTable" << endl;
        }
               
    }
}




The method still doesn't work. Any ideas?

This post has been edited by crapmyster: 11 October 2012 - 06:51 AM

Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,995
  • Joined: 08-August 08

Re: Error: invalid types '<unknown type>[int]' for array s

Posted 11 October 2012 - 07:05 AM

You can do HashTable[i] or HashTable.at(i) but not both.

hashtable[i].at(i) would be an attempt to traverse the diagonal of a two dimensional vector.
Was This Post Helpful? 1
  • +
  • -

#11 crapmyster   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 12-October 11

Re: Error: invalid types '<unknown type>[int]' for array s

Posted 11 October 2012 - 07:08 AM

If I do one or the other they both come up with errors!

Mark
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,995
  • Joined: 08-August 08

Re: Error: invalid types '<unknown type>[int]' for array s

Posted 11 October 2012 - 07:15 AM

Is Hashtable a global vector or property of the object? If not you need to pass it to the function. I'd do it by reference.
void HashTable::print(vector <thetype> &Hashtable)


Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg   User is online

  • member icon

Reputation: 5864
  • View blog
  • Posts: 17,837
  • Joined: 25-December 09

Re: Error: invalid types '<unknown type>[int]' for array s

Posted 11 October 2012 - 07:16 AM

Post the actual code and the complete error messages exactly as they appear in your development environment.

Edit: Also since this is very related to your other topic I'm going to merge the two.

Jim

This post has been edited by jimblumberg: 11 October 2012 - 07:17 AM

Was This Post Helpful? 0
  • +
  • -

#14 crapmyster   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 12-October 11

Re: Error: invalid types '<unknown type>[int]' for array s

Posted 11 October 2012 - 07:24 AM

The full code is below. As I have said before there are no errors, just the fact that when I go to print nothing is printed.

#include <iostream>
#include <vector>
using namespace std;
const int numOfBuckets = 100;

class HashTable
{
	public: void initialise();
		int hashFunction(int data);
		void insertion(int data);
		void deleteData(int in);
		void find();
		void size();
		void print();
       public:  vector<int>HashTable[100];
};

int HashTable::hashFunction(int data)
{
    return data%numOfBuckets;
}

void HashTable::insertion(int data)
{
        int index;
        index = hashFunction(data);
        HashTable[index].push_back(data);
}

void HashTable::deleteData(int in)
{	
    int index = hashFunction(in);
    for(int i = 0; i < HashTable[index].size(); ++i)
    {
        if(HashTable[index].at(i) == in)
        {
            HashTable[index].at(i) = HashTable[index].back();
            HashTable[index].pop_back();
            return;
        }
    }
}

void HashTable::find()
{
}

void HashTable::size()
{
}

void HashTable::print()
{ 
    for(int i = 0; i < HashTable[i].size(); i++)
    {
        if(!HashTable[i].empty())
        {
                cout << HashTable[i].at(i) << "  " << endl;
                cout << endl;
        }
        else
        {
            cout << "There is noting to be printed in the HashTable" << endl;
        }         
    }
}

class Menu
{ 
	public:  void menu();
                 void insert();
        private: HashTable ht;
};

void Menu::menu()
{
	int temp=0;
	cout << "" << endl;
	cout << "*** Chained HashTable Assignment ***" << endl;
	cout << "Enter a Choice" << endl;
	cout << "Press 'i' to insert an element into the hash table" << endl;
	cout << "Press 'd' to delete an element from the hash table" << endl;
	cout << "Press 'l' to look up an element in the hash table" << endl;
	cout << "Press 's' to obtain the size of the table" << endl;
	cout << "Press 'p' to print the current table" << endl;
        cout << "Press 'e' to exit from the program" << endl;
	cout << "" << endl;
	char input;
	cin >> input;
	do{
		switch(input)
		{
			case 'i': cout << "Type the integer you wish to enter into the hashTable:" << endl;
                                  cout << "" << endl;
                                  int data;
                                  cin >> data;
                                  ht.insertion(data);	
                                  menu();
				  break;
			case 'd': cout << "Type the integer you wish to delete from the hashTable:" << endl;
                                  cout << "" << endl;
                                  int in;
                                  cin >> in;
                                  ht.deleteData(in);
		        	  break;
			case 'f': ht.find();
		        	break;
			case 's': ht.size();
				break;
			case 'p': ht.print();
				break;
                        case 'e': 
                                break;
			default:
				cout << "Error: Wrong selection, choose again" << endl;
				temp = 0;
				break;
		}
	}while(temp = 1);	
}
int main()
{
	Menu m;
	m.menu();
	return 0;
}



Was This Post Helpful? 0
  • +
  • -

#15 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7456
  • View blog
  • Posts: 25,097
  • Joined: 05-May 12

Re: Error: invalid types '<unknown type>[int]' for array s

Posted 11 October 2012 - 07:31 AM

Look at your line 58. Why are you using at(i)?

Let's say the i == 25. You know that HashTable[25] on line 54 is not empty, but how do you know that the vector at HashTable[25] has at least 26 elements?
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2