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

标签 c++ error-handling compiler-errors

我正在尝试使用我正在调用的函数构建一个 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/

相关文章:

c++ - 在 scientific linux 6 上安装 gcc 4.8 时出错

c++ - 在没有 MFC 的 win32 中保存/打开通用对话框

c++ - 使用 HTTP 作为传输的消息传递中间件

Python 仅在范围内时才输入除外

Java方法调用不明确

syntax - Kotlin : Why is defining a field as “var” /“val” needed in constructor?

c++ - 将字符指针 (char*) 转换为结构

asp.net-mvc - 如何在ASP.NET MVC中使用[HandleError]属性?

javascript - Extjs 3.3.1没有显示错误

ios - 无法编译旧项目