我正在尝试使用我正在调用的函数来构建向量vector<Competition> CompPop()
。我想返回一个类型的向量信息vector<Competition>
。下面是我为我的Competition
类返回向量和标题的函数的代码。
我收到以下错误(我正在使用 Visual Studio 并且错误消息非常基本,让我猜测我实际上做错了什么):
-error C2065:“竞争”:未声明的标识符
'CompPop' 使用未定义的类 'std::vector'
“竞争”:未声明的标识符
错误 C2133:“信息”:未知大小
错误 C2512:“std::vector”:没有合适的默认构造函数可用
错误 C2065:“竞争”:未声明的标识符
错误 C2146:语法错误:缺少“;” 在标识符“temp”之前
错误 C3861:“临时”:找不到标识符
错误 C2678:二进制“[”:未找到采用“std::vector”类型的左操作数的运算符(或没有可接受的转换)
#pragma once
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <vector>
#include "LogIn.h"
#include "Registration.h"
#include "Tree.h"
#include "PriorityQueue.h"
#include "Events.h"
#include "Competition.h"
using namespace std;
vector<Competition> CompPop()
{
ifstream myfile("Results.txt");
string line, tcomp, tleader, tfollower, tevents, tplacement;
vector<Competition> info;
istringstream instream;
if(myfile.is_open())
{
int i = 0; // finds first line
int n = 0; // current vector index
int space;
while(!myfile.eof())
{
getline(myfile,line);
if(line[i] == '*')
{
space = line.find_first_of(" ");
tleader = line.substr(0+1, space);
tfollower = line.substr(space + 1, line.size());
}
else
{
if(line[i] == '-')
{
tcomp = line.substr(1, line.size());
Competition temp(tcomp, tleader, tfollower);
info[n] = temp;
}
else
{
if(!line.empty())
{
line = line;
space = line.find_first_of(",");
tevents = line.substr(0, space);
tplacement = line.substr(space + 2, line.size());
info[n].pushEvents(tevents,tplacement);
}
if(line.empty())
{
n++;
}
}
}
}
}
else
{
cout << "Unable to open file";
}
myfile.close();
return info;
}
我的比赛标题:
#pragma once
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <vector>
#include "LogIn.h"
#include "Registration.h"
#include "Tree.h"
#include "PriorityQueue.h"
#include "Events.h"
#include "CompPop.h"
using namespace std;
struct Competition
{
public:
Competition(string compName, string lead, string follow)
{
Name = compName;
Leader = lead;
Follower = follow;
}
void pushEvents(string name, string place)
{
Events one(name, place);
Eventrandom.push_back(one);
}
string GetName()
{
return Name;
}
string GetLeader()
{
return Leader;
}
string GetFollow()
{
return Follower;
}
string GetEvent()
{
return Event;
}
string GetScore()
{
return Score;
}
~Competition();
private:
string Name, Leader, Follower, Event, Score;
vector<Events> Eventrandom;
};