我正在尝试使用一个函数来构建一个向量,这个函数被我称为vector<Competition> CompPop()
。我想返回向量信息,这是一个类型的vector<Competition>
。下面是函数的代码,返回向量和我的Competition
类的头。
我得到了以下错误(我使用的是Visual,错误消息是非常基本的,让我猜测我实际上做错了什么):
-error C2065:“竞赛”:未声明的标识符
'CompPop‘使用未定义的类'std::vector’
“竞争”:未声明的标识符
错误C2133:'info‘:未知大小
错误C2512:‘std::向量’:没有适当的默认构造函数可用
错误C2065:“竞争”:未声明的标识符
错误C2146:语法错误:缺少“;”在标识符'temp‘之前
错误C3861:'temp':找不到标识符
错误C2678:二进制“[”:没有找到任何操作符,它接受‘std::向量’类型的左操作数(或者没有可接受的转换)
#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;
};
发布于 2012-04-28 00:05:59
看起来,您没有在源文件中使用#include
作为Competition
的头。
顺便说一句,看起来您还在头中执行using namespace std;
。This is not a good practice。
基于更新信息的编辑:
这是一个循环依赖问题。
如果您只是在CompPop.h中转发声明Competition
和声明CompPop
,并将CompPop
的实现添加到CompPop.cpp中,则会中断循环。
因此,将CompPop.h改为:
#pragma once
#include <vector>
struct Competition;
std::vector<Competition> CompPop();
https://stackoverflow.com/questions/10359291
复制相似问题