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



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

所讨论的函数如下。由于某种原因,最后一行"validtld [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 DocumentsVisual Studio 2010ProjectsEmailChecker_HW2EmailChecker_HW2TldPart.obj

我试着阅读Q&A页你有外部符号,并尝试了一些建议(最初我有两个),我设法减少它到一个链接器错误,我相信通过在类外声明静态函数。你们知道怎么了吗?在main.cpp中,我将该函数引用为"TldPart:: preloadtld;",但删除该行并没有删除错误(并且我在main.cpp文件的顶部有#include"TldPart.h")。这是函数,我将在下面发布头文件和cpp文件以供完整参考。整个项目相当广泛(接近1100行,最后我检查),所以我只包括这些初学者。谢谢你的帮助,我很感激。

static void preloadtld ()

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);
            }
        }
    }
}

main.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;
}

这段代码承诺将在某处定义单个静态变量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()
{
}

最新更新