2010-03-23 88 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