2013-03-13 19 views
0

几乎完成此程序,并且在移动到公用计算机并更改了一些代码后,我注意到我收到链接器错误,并且我还决定询问另一个错误(以及它发生的原因)。链接器2001成员变量的错误和未定义标识符

有问题的功能如下。出于某种原因,最后一行“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" ([email protected]@@[email protected]@@[email protected][email protected]@@@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@A) V:\My Documents\Visual Studio 2010\Projects\EmailChecker_HW2\EmailChecker_HW2\TldPart.obj 

我试图读取Q &你对外部符号页,并尝试了一些建议(原来我有两个),我设法将其降低到一个连接错误,我相信通过在类之外声明静态函数。你们看到了什么问题?在main.cpp中,我引用了函数TldPart :: PreloadTLDs;但删除该行并没有删除错误(并且在main.cpp文件的顶部有#include“TldPart.h”) 。下面是这个函数,我将在下面发布头文件和cpp文件以供参考。整个项目相当广泛(最后我检查了近1100条线),所以我只将这些包括在内。感谢您的帮助,我很感激。

静态无效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); 
      } 
     } 
    } 
} 

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

回答

2

此代码承诺,单一的静态变量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() 
{ 

} 
+0

太棒了,这帮助了地图STL的奇怪命名空间问题。任何关于未解决的外部错误的想法? – Reciever80 2013-03-13 03:15:33

+0

我应该把它放在cpp文件中?有三个函数,一个默认构造函数或全局。 – Reciever80 2013-03-13 03:17:57

+0

它仍然给我2001链接器错误与1无法解析的外部。它可能与静态的事实有关吗? – Reciever80 2013-03-13 03:30:43