为什么不';我的功能没有运行吗? 我是初学者,我想了解C++任务的诀窍。我应该读取一个文件并将其放入向量中,对其进行排序,然后在3个独立的非成员函数中将其输出到一个新文件中,但由于某些原因,它们不会运行。从来没有输出过新文件,我甚至把done cout放在那里测试函数是否在运行,但每次我都没有得到任何结果。如果我能得到一些帮助,我将不胜感激,谢谢 #include <iostream> #include <fstream> #include <vector> #include <string> #include <algorithm> #include "stdafx.h" using namespace std; void readtoVector(vector<string>& weblog) { string line; fstream myFile; myFile.open("weblog.txt"); if (myFile.fail()){ cerr << "File not available" << endl; } if (myFile.is_open()){ for (line; getline(myFile, line);){ weblog.push_back(line); } myFile.close(); } } void sortVector(vector<string>& weblog) { sort(weblog.begin(), weblog.end()); } void writeVector(const vector<string>& weblog) { ofstream myFile; myFile.open("newWeblog.txt"); for (int i = 0; i > weblog.size; ++i){ myFile << weblog[i] << endl; } myFile.close(); cout << "done"; } int main() { vector<string> weblog; readtoVector(weblog); sortVector(weblog); writeVector(weblog); system("pause"); return 0; } #包括 #包括 #包括 #包括 #包括 #包括“stdafx.h” 使用名称空间std; void readtoVector(vector和weblog) { 弦线; fstream-myFile; myFile.open(“weblog.txt”); if(myFile.fail()){ cerr

为什么不';我的功能没有运行吗? 我是初学者,我想了解C++任务的诀窍。我应该读取一个文件并将其放入向量中,对其进行排序,然后在3个独立的非成员函数中将其输出到一个新文件中,但由于某些原因,它们不会运行。从来没有输出过新文件,我甚至把done cout放在那里测试函数是否在运行,但每次我都没有得到任何结果。如果我能得到一些帮助,我将不胜感激,谢谢 #include <iostream> #include <fstream> #include <vector> #include <string> #include <algorithm> #include "stdafx.h" using namespace std; void readtoVector(vector<string>& weblog) { string line; fstream myFile; myFile.open("weblog.txt"); if (myFile.fail()){ cerr << "File not available" << endl; } if (myFile.is_open()){ for (line; getline(myFile, line);){ weblog.push_back(line); } myFile.close(); } } void sortVector(vector<string>& weblog) { sort(weblog.begin(), weblog.end()); } void writeVector(const vector<string>& weblog) { ofstream myFile; myFile.open("newWeblog.txt"); for (int i = 0; i > weblog.size; ++i){ myFile << weblog[i] << endl; } myFile.close(); cout << "done"; } int main() { vector<string> weblog; readtoVector(weblog); sortVector(weblog); writeVector(weblog); system("pause"); return 0; } #包括 #包括 #包括 #包括 #包括 #包括“stdafx.h” 使用名称空间std; void readtoVector(vector和weblog) { 弦线; fstream-myFile; myFile.open(“weblog.txt”); if(myFile.fail()){ cerr,c++,function,input,vector,output,C++,Function,Input,Vector,Output,您忘了对readtoVector的参数使用引用。该函数正在对该向量的本地副本进行操作;原始副本仍然为空。在void readtoVector(vector weblog)中将向量的副本发送到函数中,而不是实际的副本,然后在函数中填充容器。因此,当函数调用返回时,向量将返回到堆中 您应该像发送其他函数一样发送引用: void readtoVector(vector<string>& weblog) 在函数void writeVector()中请求了向量大小。您应该调用成员函数

您忘了对
readtoVector
的参数使用引用。该函数正在对该向量的本地副本进行操作;原始副本仍然为空。

void readtoVector(vector weblog)中
将向量的副本发送到函数中,而不是实际的副本,然后在函数中填充容器。因此,当函数调用返回时,向量将返回到堆中

您应该像发送其他函数一样发送引用:

void readtoVector(vector<string>& weblog)

在函数
void writeVector()
中请求了向量大小。您应该调用成员函数
size()
,而不是直接请求它。您的条件也不会计算为true。它应该是
writeVector()
中的以下行不应该编译:

for (int i = 0; i > weblog.size; ++i){
我想你想要这个:

for (int i = 0; i < weblog.size(); ++i){
for(int i=0;i

请注意,
size()
是一个成员函数,需要函数调用操作符,循环条件应该
小于

我修复了它,但仍然没有得到任何输出。我记得,包含
stdafx.h
应该是第一行代码。为什么要为这段小代码使用预编译头让我困惑,但如果内存服务是的,它不在正确的位置。当你使用调试器单步执行语句时,你发现了什么信息?我修复了它,但仍然没有得到任何输出。不幸的是,我认为应该编译。function->function pointer->
bool
->
int
,可以比较。@chris:我用g++和VS20进行了测试13,两者都会发出一个错误。一行中可能有太多的转换。我认为标准转换没有限制。不过很有趣。我还指成员函数(指针),这可能就是它不起作用的原因。我还意识到你不允许比较指针和整数。@chris:我只是在猜测。我不是一个足够的语言律师,不能引用这个标准(我太懒了)。
for (int i = 0; i > weblog.size; ++i){
for (int i = 0; i < weblog.size(); ++i){