내 프로그램에는 두 개의 입력 파일이 있는데, 그 중 하나는 명령 목록이다. 파일의 각 줄은 새로운 명령이므로이 파일을 배열로 구문 분석하려고합니다. 필자는 동적으로 커지는 벡터를 사용하여 파일의 줄 수를 계산 한 다음 해당 데이터를 사용하여 배열을 만드는 함수가 필요 없다고 전했습니다. 여기 벡터에 관한이 오류가 왜 나는지 잘 모르겠다.
내 프로그램이 지금 무엇이다 :#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
//Prototypes
int parseLines(ifstream& countfile);
vector<string> parseLines(ifstream& countfile)
//counts number of lines in file passed to function
{
string line;
vector<string> lines;
int numberOfLines;
//reads through each line until end of file putting each line in a sperate entry of the vector
while(getline(countfile, line))
{
lines.push_back(line);
}
return lines;
}
int main (int argc, char* argv[])
{
int i;
if(argc != 3) cout << "Usage: calendar.out datafile inputfile";
//Create input streams to both files
ifstream apptsfp;
ifstream inputfp;
//Open streams to both files
apptsfp.open(argv[1]);
inputfp.open(argv[2]);
if(!inputfp.is_open())
{
cerr << "failed to open input file\n";
return 1;
}
vector<string> inputVector;
vector<string> apptsVector;
inputVector = parseLines(inputfp);
for(i=0; i<= inputVector.size(); i++)
{
cout << "Input " << i << ": " << inputVector[i] << endl;
}
return 0;
}
그리고 내가 컴파일 할 때 내가이 긴 오류가 발생하고 있고 그나마 내가 확실히 이해하는 것이 : 나는 '내가 돈 같은 느낌
$ g++ calendar.cpp
calendar.cpp: In function ‘std::vector<std::basic_string<char> > parseLines(std::ifstream&)’:
calendar.cpp:17:46: error: new declaration ‘std::vector<std::basic_string<char> > parseLines(std::ifstream&)’
calendar.cpp:14:5: error: ambiguates old declaration ‘int parseLines(std::ifstream&)’
calendar.cpp: In function ‘int main(int, char**)’:
calendar.cpp:58:37: error: no match for ‘operator=’ in ‘inputVector = parseLines((* & inputfp))’
calendar.cpp:58:37: note: candidate is:
In file included from /usr/include/c++/4.7/vector:70:0,
from calendar.cpp:9:
/usr/include/c++/4.7/bits/vector.tcc:161:5: note: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = std::basic_string<char>; _Alloc = std::allocator<std::basic_string<char> >]
/usr/include/c++/4.7/bits/vector.tcc:161:5: note: no known conversion for argument 1 from ‘int’ to ‘const std::vector<std::basic_string<char> >&’
벡터를 사용하는 방법을 잘 알고 있습니다.
당신이 반환 유형에 따라 과부하 할 수 있어야한다
입니다. –