未解决的外部,其他线程让我感到困惑
Unresolved externals, other threads left me confused
我有这个程序,用于从文件中读取学生姓名以及他们在哪个班级,我收到这个未解决的外部错误。我在网上找了一个小时,什么也找不到。
这是错误:
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
功能上,这可能为你指明了正确的方向。
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 在计时器或主线程外部的命令上销毁/替换线程
- Qt的GUI线程在从外部对象调用方法时是否会在引擎盖下生成线程?
- 未解决的外部,其他线程让我感到困惑
- 如何生成类函数的外部线程
- 当来自外部库的线程不可预测地崩溃我的应用程序时,我该怎么办
- 在 C++11 中从外部终止线程
- 外部进程与线程和数据处理
- 未解析的外部符号接收线程(这是_beginthread的入口点)
- 通过写入外部变量来取消 c++ 线程是否安全/有效
- 线程更改变量值并查看其外部的更改
- 如果由另一个 OpenMP 程序调用,则外部调用的 OpenMP 程序仅使用一个线程运行
- 在MFC中实现多线程,以更新外部函数可访问的内部字典
- 如何在不使用全局变量的情况下通知外部线程主线程状态
- 在阻塞调用之前,外部变量对线程不可用
- 全局变量外部声明后的线程专用指令
- 线程安全并发调用外部命令在c++中
- 什么是英特尔 IPP 库中的外部线程?