Question

I see this error a lot in the blogs and am a little bit flustered that I cannot figure out why I'm getting this (question title) error. Linker subsystem is set to console for this console application.

this is my main method:

// HashTable.cpp : main project file.

#include "stdafx.h"
#include "HashTable.h"
#include <stdio.h>
#include <iostream>

using namespace System;
using namespace std;

namespace HashTable
{
int main(array<System::String ^> ^args)
{
    Hashtable<String^, int>^ table = gcnew Hashtable<String^, int>(100);

    // create dictionary with random keys and values to insert into the hashtable

    table->Insert("one", 1);
    table->Insert("two", 2);

    // print hashtable keys and values
    table->PrintKeyValuePairs();

    Console::ReadLine();
    return 0;
}
}

and here is the Hashtable class that it is calling:

#pragma once

#include "stdafx.h"
#include <iostream>


using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;
using namespace System::Text;
using namespace std;

namespace HashTable
{
generic <typename T1, typename T2>
public ref class Hashtable
{

public: 
    // Property to monitor size of table
    int Size;

    // public members
    array<Dictionary<T1, T2>^>^ table;


public:
    Hashtable(int tableSize)
    {
        table = gcnew array<Dictionary<T1, T2>^>(tableSize);
    }

    void Insert(T1 key, T2 value)
    {
        Dictionary<T1, T2>^ newList;
        unsigned int slot = getHashValue(key, table->Length);
        if (table[slot] == nullptr)
        {
            newList = gcnew Dictionary<T1, T2>();
            newList->Add(key, (T2)value);
            table[slot] = newList;
            Size ++;
        }
        else 
        {
            if (!table[slot]->ContainsKey(key))
            {
                table[slot]->Add((T1)key, (T2)value);
            }

            else
                Console::WriteLine(L"Key " + key + " was not added to the HashTable: Duplicate keys are not permtted.");
        }
    }

    T2 GetValue(T1 key)
    { 
        int slot = getHashValue(key, table->Length);
        if (table[slot]->ContainsKey(key))
            return table[slot][key];
        else
            return (T2)"The item requested could not be found.";
    }

private:

    unsigned int getHashValue(T1 key, int m)
    {
        // convert string to arrayo of bytes
        array<Byte>^ byteArray = gcnew array<Byte>(key->ToString()->Length);
        int i = 0;
        for each ( char c in key->ToString()->ToCharArray(0, key->ToString()->Length))
        {
            byteArray[i] = (Byte)c;
            i++;
        }

        // get the unsigned int value of the bytes
        unsigned int byteTotal = 0;
        for (int i = 0; i < byteArray->Length; i++)
            byteTotal += byteArray[i];
        return byteTotal % m;
    }

    // method to retrieve a list of key value pairs in hashtable
public:
    void PrintKeyValuePairs()
    {
        for (int i = 0; i < table->Length; i++)
        {
            if(table[i] != nullptr)
            {
                for each(KeyValuePair<T1, T2> kvp in table[i])
                {
                    T1 key = kvp.Key;
                    T2 value = kvp.Value;
                    Console::WriteLine("Key = {0}, Value = {1}", key, value);
                }
            }
        }
    }
};
}

A lot of the includes were added to try to solve this problem.

Was it helpful?

Solution

Please take a look at this , your main() function must include native types I guess the "classic" int main(int argc, char* argv[]) will work the magic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top