链接列表实现帮助 - Visual C ++

时间:2010-03-23 15:21:17

标签: visual-c++ linked-list

我正在尝试实现一个存储城市名称的链接列表(虽然你会看到这个被注释掉,因为我需要解决无法使用字符串的问题,并且需要使用原始数据类型。声明),经度,纬度,当然还有指向链中下一个节点的指针。我是Visual C ++环境的新手,今天连续几个小时编码后我的大脑有点乱,所以我想知道是否有人可以帮助解决我得到的2个错误(忽略#include语法,因为我必须更改它们以避免浏览器解释html!):

1>U08221.obj : error LNK2028: unresolved token (0A000298) "public: __thiscall Locations::Locations(void)" (??0Locations@@$$FQAE@XZ) referenced in function "int __clrcall main(cli::array^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)

1>U08221.obj : error LNK2019: unresolved external symbol "public: __thiscall Locations::Locations(void)" (??0Locations@@$$FQAE@XZ) referenced in function "int __clrcall main(cli::array^)" (?main@@$$HYMHP$01AP$AAVString@System@@@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缺乏经验,所以我做了一些愚蠢的错误,当你长时间盯着某些东西时,你却找不到这些错误。如果您有任何帮助,我们将不胜感激!

由于

1 个答案:

答案 0 :(得分:2)

您的代码至少有两个问题。

首先,链接器抱怨它无法找到Locations构造函数的实现(Locations :: Locations())。这是因为你在标题中声明了它,但实际上并没有在任何地方定义它。

将标题中的行更改为:

Locations() {}

将创建一个简单的无操作实现。您应该更改此项以包括您想要执行的默认构造。

其次,你不应该明确地调用Locations::Locations() - 你不需要像这样将构造函数作用于类,并且你没有将结果赋给任何对象。