2010-03-23 158 views
2

我試圖實現一個存儲城市名稱的鏈接列表(雖然你會看到這個註釋掉,因爲我需要解決不能使用字符串和需要使用的問題一個原始數據類型,而不是聲明中),經度,緯度,當然還有一個指向鏈中下一個節點的指針。我是Visual C++環境的新手,今天編了幾個小時之後,我的大腦有些混亂,所以我想知道是否有人可以幫助解決我得到的兩個錯誤(忽略#include語法,因爲我必須更改它們以避免瀏覽器解釋HTML):鏈接列表實現幫助 - Visual C++

 
1>U08221.obj : error LNK2028: unresolved token (0A000298) "public: __thiscall Locations::Locations(void)" ([email protected]@[email protected]) referenced in function "int __clrcall main(cli::array^)" ([email protected]@[email protected]@@@Z) 

1>U08221.obj : error LNK2019: unresolved external symbol "public: __thiscall Locations::Locations(void)" ([email protected]@[email protected]) referenced in function "int __clrcall main(cli::array^)" ([email protected]@[email protected]@@@Z) 

我的頭文件中的代碼是在這裏:

#include <string> 

struct locationNode 
{ 
    //char[10] nodeCityName; 
    double nodeLati; 
    double nodeLongi; 
    locationNode* Next; 
}; 

class Locations 
{ 
private: 
    int size; 
public: 
    Locations(); // constructor for the class 
    locationNode* Head; 
    int Add(locationNode* Item); 
}; 

,這裏是包含main方法的文件中的代碼:

// U08221.cpp : main project file. 

#include "stdafx.h" 

#include "Locations.h" 
#include <iostream> 
#include <string> 

using namespace std; 

int n = 0;  
int x;  
string cityNameInput;  
bool acceptedInput = false; 

int Locations::Add(locationNode *NewItem) 
{ 
    locationNode *Sample = new locationNode; 

    Sample = NewItem; 
    Sample->Next = Head; 
    Head = Sample; 
    return size++; 
} 

void CorrectCase(string name) // Correct upper and lower case letters of input 
{ 
    x = name.size(); 
    int firstLetVal = name[0], letVal; 
    n = 1; // variable for name index from second letter onwards 

    if((name[0] >90) && (name[0] < 123)) // First letter is lower case 
    { 
     firstLetVal = firstLetVal - 32; // Capitalise first letter 
     name[0] = firstLetVal; 
    } 

    while(n <= x - 1) 
    { 
     if((name[n] >= 65) && (name[n] <= 90)) 
     { 
      letVal = name[n] + 32; 
      name[n] = letVal; 
     } 
     n++; 
    } 
    cityNameInput = name; 
} 


void nameValidation(string name) 
{ 
    n = 0; // start from first letter 
    x = name.size(); 
    while(!acceptedInput) 
    { 
     if((name[n] >= 65) && (name[n] <= 122)) // is in the range of letters 
     { 
      while(n <= x - 1) 
      { 
       while((name[n] >=91) && (name[n] <=97)) // ERROR!! 
       { 
        cout << "Please enter a valid city name" << endl; 
        cin >> name; 
       } 
       n++; 
      } 
     } 
     else { 
      cout << "Please enter a valid city name" << endl; 
      cin >> name; 
     } 
     if(n <= x - 1) 
     { 
      acceptedInput = true; 
     } 
    } 
    cityNameInput = name; 
} 

int main(array<System::String ^> ^args) 
{ 
    cout << "Enter a city name" << endl; 
    cin >> cityNameInput; 

    nameValidation(cityNameInput); // check is made up of valid characters 
    CorrectCase(cityNameInput); // corrects name to standard format of capitalised first letter, and lower case subsequent letters 
    cout << cityNameInput; 
    cin >> cityNameInput; 

    Locations::Locations(); 

    Locations *Parts = new Locations(); 
    locationNode *Part; 
    Part = new locationNode; 
    //Part->nodeCityName = "London"; 
    Part->nodeLati = 87; 
    Part->nodeLongi = 80; 
    Parts->Add(Part); 
} 

我對這些概念很熟悉,但對OOP有點不熟悉,所以我犯了一些愚蠢的錯誤,當你盯着太長的東西時你永遠找不到。任何幫助,您可以提供將不勝感激!

謝謝

+0

這個問題爲什麼不使用'std :: list'和'std :: string'? – 2010-03-23 15:56:35

+0

我們不允許在這個特殊的實例中使用庫定義的函數來處理面向對象的方面,它必須被自己硬編碼。 – ComethTheNerd 2010-03-23 15:59:21

+0

不允許由誰?僅供參考,如果這是功課,習慣使用「家庭作業」標籤。 (當然,我們並不希望新來的人知道這些事情。)你會發現這裏的人會問這樣的問題,這很好解釋前面的問題。除此之外,你做對了:提供足夠的信息,提出明確的問題,展示你的工作,並監測問題。歡迎。 – 2010-03-23 16:40:02

回答

2

您的代碼至少有兩個問題。

首先,鏈接器抱怨它無法找到您的位置構造函數(Locations :: Locations())的實現。這是因爲你在標題中聲明瞭它,但實際上並沒有在任何地方定義它。

更改在標題行至:

Locations() {} 

將創建一個簡單的什麼都不做的實現。你應該改變這個以包含你想要執行的默認構造。

其次,您不應該明確地調用Locations::Locations() - 您不需要像這樣將構造函數作用於類,並且不會將結果分配給任何對象。

+0

非常感謝,它現在編譯,所以我非常感謝! – ComethTheNerd 2010-03-23 15:53:48