我正在尝试使用调用vector<Competition> CompPop()
的函数来构建 vector 。我想返回 vector 信息,它是vector<Competition>
类型。下面是我的代码,该函数返回 vector 和Competition
类的 header 。
我收到以下错误(我使用的是Visual Studio,并且错误消息非常基本,让我猜测我实际上在做什么错):
#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;
};
最佳答案
看来您没有在源文件中对#include
的 header 进行Competition
编码。
顺便说一句,看起来您的 header 中也包含using namespace std;
。 This is not a good practice。
根据更新的信息进行编辑:
这是一个循环依赖性问题。
如果只是简单地向前声明Competition
并在CompPop.h中声明CompPop
,然后将CompPop
的实现添加到CompPop.cpp中,那么您将中断循环。
因此,将CompPop.h更改为:
#pragma once
#include <vector>
struct Competition;
std::vector<Competition> CompPop();
关于c++ - 我想返回vector <Competition>的函数,但被告知未声明Competition,并且我使用的是未定义的类 'std::vector',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10359291/