我想返回一个向量函数,<Competition>但被告知竞争是未声明的,并且我正在使用未定义的类'std::vector'
I want to return a function of vector<Competition> but is told that Competition is undeclared and that I am using undefined class 'std::vector'
我正在尝试使用我调用vector<Competition> CompPop()
的函数构建一个向量。我想返回矢量信息,这是一个类型vector<Competition>
。下面是我的函数代码,该函数返回我的Competition
类的向量和标头。
收到以下错误(我正在使用Visual Studio,错误消息非常基本,让我猜测我实际上做错了什么(:
<小时 />-错误 C2065:"竞争":未声明的标识符
"CompPop"使用未定义的类"std::vector">
"竞争":未声明的标识符
错误 C2133:"信息":未知大小
错误 C2512:"std::vector":没有可用的适当默认构造函数
错误 C2065:"竞争":未声明的标识符
错误C2146:语法错误:标识符"temp"之前缺少";">
错误 C3861:"temp":找不到标识符
错误 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;
};
看起来您没有#include
源文件中Competition
的标头。
顺便说一句,看起来你在标题中也做了using namespace std;
。 这不是一个好的做法。
根据更新的信息进行编辑:
这是一个循环依赖问题。
如果您只是在 CompPop.h 中转发声明Competition
和声明CompPop
,并将CompPop
的实现添加到 CompPop.cpp,您将打破循环。
因此,请将 CompPop.h 更改为:
#pragma once
#include <vector>
struct Competition;
std::vector<Competition> CompPop();
看起来您缺少包含语句或使用语句。
即要么缺少组件的标头,要么缺少 std::vector 的标头,或者两者兼而有之。
还要确保您有using namespace std;
或using std::vector;
如果不是这种情况,请提供更多代码,以便我们对其进行全面检查。
[编辑] 根据您的评论进行更新您是否使用内含防护装置?
- #ifdef和未声明的标识符
- 未声明的标识符编译暗黑破坏神 2 程序"muleview"
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- Google protobuf 时间戳未声明标识符,在 Windows 上具有C++
- 在气泡排序程序中未声明错误功能
- 我在范围内未声明的错误类有问题
- 使用未声明的标识符"sqrt"?
- 我包含了<winuser.h>,但编译器在MonitorFromWindow上抛出"未声明"
- 对类 (C++) 中的私有变量使用未声明的标识符
- 我的代码中有错误,未声明的标识符
- 如何在 C++ 中转发声明 std::set?
- 未定义的标识符/未声明
- 错误:在 C++ 'std'使用未声明的标识符
- 在编译器之前检测未声明的标识符
- 使用未声明的标识符'temp'
- 内存未释放 std::list<std::shared_ptr<std::string>> C++
- 使用未声明的标识符"yylex"和"yyin"?
- 尝试使用参数包绑定 std::function 时获取未声明的参数
- 我想返回一个向量函数,<Competition>但被告知竞争是未声明的,并且我正在使用未定义的类'std::vector'
- std::使用Visual C++2008映射未声明的标识符