未解决的外部,其他线程让我感到困惑

Unresolved externals, other threads left me confused

本文关键字:线程 外部 其他 未解决      更新时间:2023-10-16

我有这个程序,用于从文件中读取学生姓名以及他们在哪个班级,我收到这个未解决的外部错误。我在网上找了一个小时,什么也找不到。

这是错误:

Error   3   error LNK2019: unresolved external symbol "public: class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > __thiscall DList::splitLine(char * const,char)" (?splitLine@DList@@QAE?AV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QADD@Z) referenced in function _main  C:UsersAustin JulioDesktopCMPSC122AssignmentsAssignment 6Assignment 6Main.obj

这是我的.h,.cpp和main.cpp(大部分代码是我的教授给我的课):

DList.h

#include <iostream> 
#include <string> 
#include <vector>
using namespace std; 
#ifndef DLIST_H
#define DLIST_H
typedef string ElementType; 
class DList 
{ 
 public: 
 /***** Class constructor *****/ 
 DList(int maxSize = 1024); //default constructor; default value is 1024; 
 /***** Class destructor *****/ 
 ~DList(); 
 /***** Copy constructor *****/ 
 DList(const DList & origList); 
 /***** Assignment operator *****/ 
 DList & operator=(const DList & rightHandSide); 
 /***** Get length of list *****/ 
 int length(); 
 /***** empty operation *****/ 
 bool empty() const; 
 /***** insert and erase *****/ 
 void insert(ElementType item, int pos); 
 void erase(int pos); 
 /***** output *****/ 
 void display(ostream &out) const; 
 vector<string> splitLine(char [], char);

 private: 
 /******** Data Members ********/ 
 int mySize; // current size of list 
 int myCapacity; // capacity of array 
 ElementType * myArrayPtr; // pointer to dynamic array 

};
#endif

德利斯特.cpp

#include <iostream> 
#include "DList.h" 
#include <vector>
using namespace std; 
//--- Definition of default class constructor 
DList::DList(int maxSize) 
{ 
 this->mySize = 0; 
 this->myCapacity = maxSize; 
 myArrayPtr = new ElementType[maxSize]; 
} 
//--- Definition of class destructor 
DList::~DList() 
{ 
 delete [] myArrayPtr; 
} 
//--- Definition of copy constructor 
DList::DList(const DList & origList) 
{ 
 myCapacity = origList.myCapacity; 
 mySize = origList.mySize; 
 //--- Get new array for copy 
 myArrayPtr = new ElementType[myCapacity]; 
 //--- Copy origList's elements into this new array 
 for(int i = 0; i < mySize; i++) 
 myArrayPtr[i] = origList.myArrayPtr[i]; 
} 
//--- Definition of assignment operator 
DList & DList::operator=(const DList & rightHandSide) 
{ 
 if (this != &rightHandSide) // check that not self-assignment 
 { 
 //-- Allocate a new array if necessary 
 if (myCapacity != rightHandSide.myCapacity) 
 { 
 delete[] myArrayPtr; 
 myCapacity = rightHandSide.myCapacity; 
 myArrayPtr = new ElementType[myCapacity]; 
 } 
 //--- Copy rightHandSide's list elements into this new array 
 mySize = rightHandSide.mySize; 
 for(int i = 0; i < mySize; i++) 
 myArrayPtr[i] = rightHandSide.myArrayPtr[i]; 
 } 
 return *this; 
} 
//--- Definition of empty() 
bool DList::empty() const 
{ 
 return mySize == 0; 
} 
//get length of list 
int DList::length() 
{ 
 return mySize; 
} 
//--- Definition of display() 
void DList::display(ostream & out) const 
{ 
 for (int i = 0; i < mySize; i++) 
 out << myArrayPtr[i] << " "<<endl; 
} 
//--- Definition of insert() 
void DList::insert(ElementType item, int pos) 
{ 
 if (mySize == myCapacity) 
 exit(1); //exit out 
 if (pos < 0 || pos > mySize) 
 { 
 cerr << "*** Illegal location to insert -- " << pos 
 << ". List unchanged. ***n"; 
 return; 
 } 
 // First shift array elements right to make room for item 
 for(int i = mySize; i > pos; i--) 
 myArrayPtr[i] = myArrayPtr[i - 1]; 
 // Now insert item at position pos and increase list size 
 myArrayPtr[pos] = item; 
 mySize++; 
} 
//--- Definition of erase() 
void DList::erase(int pos) 
{ 
 if (mySize == 0) 
 { 
 cerr << "*** List is empty ***n"; 
 return; 
 } 
 if (pos < 0 || pos >= mySize) 
 { 
 cerr << "Illegal location to delete -- " << pos 
 << ". List unchanged. ***n"; 
 return; 
 } 
 // Shift array elements left to close the gap 
 for(int i = pos; i < mySize; i++) 
 myArrayPtr[i] = myArrayPtr[i + 1]; 
 // Decrease list size 
 mySize--; 
} 
vector<string> splitLine(char a_line[], char delimiter)
{
    int const line_length = strlen(a_line);
    int token_pos = 0;
    char *a_token;
    a_token = new char[line_length]; // make it to max size of a_line
    a_token[0] = '';               //initialize as an empty
    vector<string> tokens;
    for (unsigned i = 0; i < line_length; i++) {                
        //loop through entire line
        if (a_line[i] == delimiter || i == line_length - 1) {
            if (i == line_length - 1 ) 
            {
                a_token[token_pos] = a_line[i];
                token_pos++;
            }
            a_token[token_pos] = ''; //add a zero to end of token
            tokens.push_back(a_token); //save the current token to somewhere
            a_token = new char[line_length]; // allocate new memory for next token
            token_pos = 0;
        } else {
            a_token[token_pos] = a_line[i];
            token_pos++;
        }
   } 
   return tokens;
}

主.cpp

#include <iostream>
#include <vector>
#include <iomanip>
#include <string>
#include <fstream>
#include "Dlist.h"
#define FILENAME_MAX 1024
#define MAX_CHAR_LINE 500
using namespace std;
int main()
{
    DList a1;
    DList a2;
    char delimiter = ',';
    vector <string> linetokens;
    fstream infile;
    char readLine[MAX_CHAR_LINE];
    infile.open ("students.txt");
    if(infile.is_open())
    {
        for (;;)
        {
            infile.getline(readLine, sizeof(readLine));
            linetokens = a1.splitLine(readLine, delimiter);
        }
    }

}

我希望你能帮忙,因为我不知道该怎么做。谢谢!

在DList中.cpp你这样说:

vector<string> splitLine(char a_line[], char delimiter)

而不是这个

vector<string> DList::splitLine(char a_line[], char delimiter)

这看起来像是一个小疏忽,您在.cpp文件中很好地限定了其余功能的范围。有时需要一个小时才能找到最小的错别字:-)

为了将来参考,请尝试开始理解编译器错误(在本例中为 Linker),因为您的错误实际上具有很强的描述性:

__thiscall DList::splitLine(char * const,char)

应该提示你问题出在splitLine功能上,这可能为你指明了正确的方向。