C++读取txt文件出现无法解析的外部命令、符号

Hi_Ayn 2017-02-09 02:37:39
问题:
1. LNK2019 无法解析的外部符号 "int __cdecl getInputData(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?getInputData@@YAHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HHV12@1@Z),该符号在函数 _main 中被引用 Read-0208 D:\Visual Studio 2015 - My data\Read-0208\Read-0208\Read-0208.obj 1

2. LNK1120 1 个无法解析的外部命令 Read-0208 D:\Visual Studio 2015 - My data\Read-0208\Debug\Read-0208.exe 1

求助求助~


#include <fstream>// file stream
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void main()
{
int getFileRows(const string fileName);
int getFileColumns(const string fileName);
int getInputData(const string fileName, int rowNum, int colNum, string X, string Y);

string filename;
string location;
filename = "test.txt";
location = "D:\Visual Studio 2015 - My data\Read-0208\Read-0208" + filename;

int R = getFileRows(filename);
int C = getFileColumns(filename);

string *Y = new string[R];
string *X = new string[R*(C - 1)];

int F = getInputData(filename, R, C, *X, *Y);

cout << "FileRows = " << R << "\n";
cout << "FileColumns = " << C << "\n";
cout << F << endl;

delete[] Y, X;
}

/***********************************获取矩阵行数**************************************************/
int getFileRows(const string fileName)//const防止被意外修改
{
ifstream fileStream;
string tmp;
int count = 0;//行数计数器
fileStream.open(fileName,ios::in); //ios::in表示只读
if (fileStream.fail())
{
return 0;
}
else//如果文件存在
{
while (getline(fileStream, tmp, '\n'))//读取一行
{
if (tmp.size() > 0)
count++;
}
fileStream.close();
return count;
}
}

/***********************************获取矩阵列数**************************************************/
int getFileColumns(const string fileName)
{
ifstream fileStream;
fileStream.open(fileName, ios::in);

string tmp;
int count = 0;
char c;
c = fileStream.peek();

while (('\n' != c) && (!fileStream.eof()))
{
fileStream >> tmp;
//tmp << fileStream.rdbuf();//把文件流中的字符输入到字符串流中
//getline(fileStream, tmp, ' ');
//if (' ' != c)
//{
++count;
//}
c = fileStream.peek();
//cout << c << endl;
}

fileStream.close();
return count;
}

/***********************************获取矩阵数据**************************************************/
int getInputData(const string fileName, int rowNum, int colNum, string *X[], string *Y[])
{
ifstream fileStream;
string oneLine = "";
//char tmp;//当前位置上的数值
int rowCount;//行数计数器
int colCount;//列数计数器
int index = 0;
int maxIndex = rowNum*(colNum - 1) - 1;

//打开文件
fileStream.open(fileName, ios::in);
if (fileStream.fail())//文件打开失败,返回0
{
cout << "文件不存在!" << endl;
fileStream.close();
system("pause");
return 0;

}
else//文件存在
{
//读入数据
rowCount = 0;//初始化当前行数为0
colCount = 0;//初始化当前列数为0
string tmp;//当前读入的值
while (!fileStream.eof())
{
fileStream >> tmp;
cout << tmp << endl;//测试句

if (colCount == 0)
{
if (rowCount < rowNum)//越界检查,第一列是Y
{
*Y[rowCount] = tmp;
cout << *Y[rowCount] << endl;
}
else
{
cout << "计算出的行数与输入数据不符,请检查数据(如文件末尾不能有空行)." << endl;
fileStream.close();
system("pause");
return 0;
}
}
else//非第一列是数据X
{
index = rowCount*(colNum - 1) + colCount - 1;
if (index <= maxIndex)
{
*X[index] = tmp;
cout << *X[index] << tmp;
}
else
{
cout << "X[]下标超出数组范围,可能是文件中某行的列数>第一行的列数,退出!" << endl;
fileStream.close();
system("pause");
return 0;
}
}
if ('\n' != fileStream.peek())
{
++colCount;
}
else
{
if ((colCount + 1 != colNum))
{
cout << "第" << rowCount + 1 << "行,输入的列数与文件列数不一致,请检查数据." << endl;
fileStream.close();
system("pause");
return 0;
}
else
{
rowCount++;
colCount = 0;
}
}
}
fileStream.close();
return 1;
}
}
...全文
203 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 版主 2017-02-09
  • 打赏
  • 举报
回复
此外: location = "D:\Visual Studio 2015 - My data\Read-0208\Read-0208" + filename; 改成: location = "D:\\Visual Studio 2015 - My data\\Read-0208\\Read-0208" + filename;
paschen 版主 2017-02-09
  • 打赏
  • 举报
回复
getInputData 声明是: int getInputData(const string fileName, int rowNum, int colNum, string X, string Y); 实现是: int getInputData(const string fileName, int rowNum, int colNum, string *X[], string *Y[]) 不一致
  • 打赏
  • 举报
回复
把getInputData那个函数的参数string *X[] 换成string x[];函数体中*x变成x
未狂 2017-02-09
  • 打赏
  • 举报
回复
int getInputData(const string fileName, int rowNum, int colNum, string X, string Y); int getInputData(const string fileName, int rowNum, int colNum, string *X[], string *Y[]) 参数

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧