我想返回一个向量函数,<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'

本文关键字:未声明 std vector 未定义 被告 返回 一个 向量 gt Competition 函数      更新时间:2023-10-16

我正在尝试使用我调用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;

如果不是这种情况,请提供更多代码,以便我们对其进行全面检查。

[编辑] 根据您的评论进行更新您是否使用内含防护装置?