我在博客中经常看到这个错误,并且有点慌张,因为我无法弄清楚为什么我会收到这个(问题标题)错误。链接器子系统设置为此控制台应用程序的控制台。
这是我的主要方法:
// 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;
}
}
这是它正在调用的 Hashtable 类:
#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);
}
}
}
}
};
}
添加了许多包含以尝试解决此问题。