我如何从多维向量中提取数据
Posted
技术标签:
【中文标题】我如何从多维向量中提取数据【英文标题】:how can i pull data form a multi dimensional vector 【发布时间】:2019-11-21 03:30:05 【问题描述】:/tmp/ccQQwq6l.o: 在函数main':
hw4.cpp:(.text+0x7d): undefined reference to
writepass(std::vector >, std::vector, std::allocator > >&)'
collect2:错误:ld 返回 1 个退出状态
代码如下
#include<iostream>
#include<cstdlib>
#include<string>
#include<fstream>
#include<ctime>
#include<vector>
using namespace std;
//cout << "test1\n";
vector<pair<string,string> > readnames();
vector<string> readpasswords();
vector<string> randpasswords(vector<string>&);
int writepass(vector<string>, vector<pair<string,string> >&);
vector<string> passwords;
vector<pair<string,string> > names;
vector<string> randpass;
int main()
readnames();
readpasswords();
randpasswords(passwords);
writepass(randpass, names);
// writepass(randpasswords(readpasswords()), readnames());
return 0;
这会读取我 cpu 上文件的名称
vector<pair<string,string> > readnames()
vector<pair<string,string> > names;
ifstream indata;
indata.open("employees.txt");
while (true)
pair<string, string> name;
if (!(indata >> name.first >> name.second))
break;
names.push_back(name);
indata.close();
return names;
这会从我的 cpu 上的文件中读取密码
vector<string> readpasswords()
vector<string> passwords;
ifstream indata;
indata.open("passwords.txt");
while(!indata.eof())
string password;
if (!(indata >> password))
break;
passwords.push_back(password);
indata.close();
return passwords;
这个功能我还不关心
vector<string> randpasswords(vector<string>& passwords)
vector<string> randpass;
vector<string> rand;
srand (time(NULL));
return randpass;
这似乎产生了问题
int writepass(vector<string>& randpass, vector<pair<string,string> >& names)
ofstream outdata;
outdata.open("empPasswords.txt");
for(int i = 0; i < randpass.size();i++)
outdata << names[i].first << " " << names[i].second << " " << randpass[i] << endl;
outdata.close();
return 0;
【问题讨论】:
我想知道是什么导致了错误(退出条件) /tmp/cc8EdTNB.o: 在函数main': hw4.cpp:(.text+0x7d): undefined reference to
writepass(std::vector<:string std::allocator>>, std::vectorint writepass(vector<string>& randpass, vector<pair<string,string> >& names)
与声明int writepass(vector<string>, vector<pair<string,string> >&);
不一样。第一个参数是vector<string>&
,另一个是vector<string>
。
无关:阅读Why is iostream::eof inside a loop condition (i.e. while (!stream.eof())
) considered wrong?和srand() — why call it only once?
【参考方案1】:
请参阅用户 4581303 以获取答案,这是一个错字
【讨论】:
以上是关于我如何从多维向量中提取数据的主要内容,如果未能解决你的问题,请参考以下文章