c++ - 链接器 2001 错误和成员变量的未定义标识符

标签 c++ function linker-errors unresolved-external

几乎完成了这个程序,在移动到公共(public)计算机并更改了一些代码之后,我注意到我收到了一个链接器错误,我还决定询问另一个错误(以及为什么会发生)。

有问题的功能如下。出于某种原因,最后一行“ValidTlds[transferString]....”表明 VS 无法识别 ValidTLD,并且只有在我在其后面添加 TldPart::(它所在的文件的名称)时才会识别。这是否与其他名称冲突?

此外,我认为还涉及功能的更重要的错误是未解析的外部符号。确切的行:

Error   3   error LNK2001: unresolved external symbol "public: static class std::map<class String,class String,struct std::less<class String>,class std::allocator<struct std::pair<class String const ,class String> > > TldPart::ValidTlds" (?ValidTlds@TldPart@@2V?$map@VString@@V1@U?$less@VString@@@std@@V?$allocator@U?$pair@$$CBVString@@V1@@std@@@3@@std@@A)    V:\My Documents\Visual Studio 2010\Projects\EmailChecker_HW2\EmailChecker_HW2\TldPart.obj

我尝试阅读您关于外部符号的问答页面,并尝试了一些建议(最初我有两个),我相信通过在类外部声明静态函数,我设法将其减少为一个链接器错误。大家看看有什么问题吗?在 main.cpp 中,我将该函数引用为“TldPart::PreloadTLDs;”,但删除该行并没有消除错误(我在 main.cpp 文件的顶部有#include "TldPart.h") .这是函数,我将在下面发布 header 和 cpp 文件以供完整引用。整个项目非常广泛(我上次检查时接近 1100 行),所以我只包括了这些作为初学者。感谢您的帮助,我很感激。

static void PreloadTLDs()

static void PreloadTLDs()
{
    bool initialized = false;
    bool fileStatus = false;
    string tldTest = ""; // used for getline() as allowed.
    char * transferString = " "; // used to transfer chars from string to String

    ifstream infile;

    infile.open("ValidTLDs.txt");

    fileStatus = infile.good();

    if(fileStatus != true)
        cout << "Cannot read ValidTLD's file. Please check your file paths and try again.";
    else
    {
        while(!infile.eof())
        {
            getline (infile, tldTest); // sets the current TLD in the list to a string for comparision

            // converts TLD to lowercase for comparison.
            for(unsigned int x = 0; x<tldTest.length(); x++)
            {
                tldTest[x] = tolower(tldTest[x]);
                transferString[x] = tldTest[x];

                ValidTlds[transferString] = String(transferString);
            }
        }
    }
}

ma​​in.cpp(缩短)

#include <iostream>
#include <fstream>
#include "String.h"
#include <map>
#include <string> // used for the allowed getline command
#include "Email.h"
#include "TldPart.h"

using namespace std;

void main()
{
    string getlinetransfer; // helps transfer email from getline to c string to custom String
    double emailTotal = 0.0; // Used to provide a cool progress counter
    double emailCounter = 0.0; // Keeps track of how many emails have been verified.
    int x = 0; // used to set c-string values, counter for loop
    char * emailAddress = new char[getlinetransfer.size() + 1]; // c string used for getting info from getline.

    cout << "Welcome to email validation program!" << endl;
    cout << "Pre-Loading Valid TLD's..... \n" << endl;

    TldPart::PreloadTLDs;
}

TldPart.h

// TldPart.h - TldPart validation class declaration
// Written by ------

#pragma once

#include "String.h"
#include <map>
#include "SubdomainPart.h"
#include <string>
#include <fstream>
#include <iostream>

using namespace std;



class TldPart
{
public:
    // MUST HAVE a defualt constructor (because TldPart is a member of Domain)
    TldPart() {}

    // Takes the address and stores into the Address data member
    void Set(const String& address);

    static void PreloadTLDs();

    // Returns true when the Address is valid or false otherwise
    bool IsValid();

    static map<String, String> ValidTlds;
private:
    String Address; 
};

TldPart.cpp

// TldPart.cpp - TldPart validation class implementation
// Written by Max I. Fomitchev-Zamilov

#pragma once

#include "TldPart.h"
using namespace std;

void TldPart()
{

}

// Takes the address and stores into the Address data member
void TldPart::Set(const String& address)
{
    Address = address;
}

static void PreloadTLDs()
{
    bool initialized = false;
    bool fileStatus = false;
    string tldTest = ""; // used for getline() as allowed.
    char * transferString = " "; // used to transfer chars from string to String

    ifstream infile;

    infile.open("ValidTLDs.txt");

    fileStatus = infile.good();

    if(fileStatus != true)
        cout << "Cannot read ValidTLD's file. Please check your file paths and try again.";
    else
    {
        while(!infile.eof())
        {
            getline (infile, tldTest); // sets the current TLD in the list to a string for comparision

            // converts TLD to lowercase for comparison.
            for(unsigned int x = 0; x<tldTest.length(); x++)
            {
                tldTest[x] = tolower(tldTest[x]);
                transferString[x] = tldTest[x];

                ValidTlds[transferString] = String(transferString);
            }
        }
    }
}

// Returns true when the Address is valid or false otherwise
bool TldPart::IsValid()
{   
    bool tldFound = false;

    map<String, String>::iterator it;

    String TLDMatch;

    TLDMatch = TldPart::ValidTlds.find(Address)->first;
    it = TldPart::ValidTlds.find(Address);

    if(it == ValidTlds.end())
        tldFound == false;
    else
        tldFound == true;

    return tldFound;
}

最佳答案

此代码 promise 将在某处定义单个静态变量 TldPart::ValidTlds

class TldPart
{
    static map<String, String> ValidTlds;
};

将它添加到 TldPart.cpp 中,一切都会好起来的。

#include "TldPart.h"
using namespace std;

map<String, String> TldPart::ValidTlds;  // DECLARE YOUR STATIC VARIABLE

void TldPart()
{

}

关于c++ - 链接器 2001 错误和成员变量的未定义标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15376348/

相关文章:

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

c++ - 避免函数重载

c++ - 在没有代码重复的情况下将 C 函数包装在自动对象中

php - 获取 PHP 中最后一个查询的实际(绝对)执行时间(不包括网络延迟等)

C++更改函数的变量参数

JavaScript 函数数据处理

ios - 找不到 -llib 的库。 (当当 : error: linker command failed with exit code 1 (use -v to see invocation))

c++ - LNK1104 无法打开文件 '...lib.obj'

c++ - 如何在 SFML 中为 Sprite 制作动画

c++ - 一对一递归函数