gpt4 book ai didi

c++ - 我想返回 vector 的函数,但被告知未声明 Competition 并且我正在使用未定义的类 'std::vector'

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:41 24 4
gpt4 key购买 nike

我正在尝试使用我正在调用的函数构建一个 vector vector<Competition> CompPop() .我想返回类型为 vector<Competition> 的 vector 信息.下面是我的函数代码,用于返回我的 Competition 的 vector 和 header 。类。

我收到以下错误(我使用的是 Visual Studio,错误消息非常基本,让我猜测我到底做错了什么):

-error C2065: 'Competition' : undeclared identifier

'CompPop' uses undefined class 'std::vector'

'Competition' : undeclared identifier

error C2133: 'info' : unknown size

error C2512: 'std::vector' : no appropriate default constructor available

error C2065: 'Competition' : undeclared identifier

error C2146: syntax error : missing ';' before identifier 'temp'

error C3861: 'temp': identifier not found

error C2678: binary '[' : no operator found which takes a left-hand operand of type 'std::vector' (or there is no acceptable conversion)


    #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;
};

最佳答案

看起来您没有在源文件中#includeCompetition header 。

顺便说一句,您似乎也在 header 中使用 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();

关于c++ - 我想返回 vector<Competition> 的函数,但被告知未声明 Competition 并且我正在使用未定义的类 'std::vector',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10359291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com