0
我在博客中看到了很多這個錯誤,並且有點驚慌,我無法弄清楚爲什麼我得到這個問題標題錯誤。鏈接器子系統設置爲用於此控制檯應用程序的控制檯。LNK2019:無法解析的外部符號_main在函數中引用_tmainCRTStartup
這是我的主要方法:
// 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);
}
}
}
}
};
}
了很多,包括加來嘗試解決這個問題。
謝謝,但同樣的錯誤依然存在。 – deadEddie 2013-03-12 21:57:13
請拔出main()方法駐留在全局範圍內,而不是在HashTable名稱空間下 – 2013-03-12 22:17:15
嗯,這確實解決了我遇到的問題,所以我會接受你的答案。但是,出於某種原因,它不會讓我在主方法中實例化一個Hashtable的實例。我想它之前並不在乎,因爲畢竟它甚至看不到主要的方法。 – deadEddie 2013-03-12 22:29:17