سؤال

I have this program that is being used to read student names and which class they're in from a file and I'm getting this unresolved external error. I've looked for an hour online about it and can't find anything.

Here is the error:

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:\Users\Austin Julio\Desktop\CMPSC122\Assignments\Assignment 6\Assignment 6\Main.obj

Here is my .h, .cpp, and main.cpp (most of the code is what my professor gave my class):

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

DList.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] = '\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] = '\0'; //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;
}

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

    }


}

I hope you can help as I have no clue what to do. Thanks!

هل كانت مفيدة؟

المحلول

In DList.cpp you say this:

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

instead of this

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

It looks like a minor oversight, you are scoping the rest of the functions just fine in the .cpp file. Sometimes it takes an hour just to find the smallest typo :-)

For future reference, try to start understanding compiler errors (well, Linker in this case), because yours was actually quite descriptive:

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

Should tip you off that the issue was with the splitLine function, which might have pointed you in the right direction.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top