首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我想返回一个vector<Competition>函数,但是有人告诉我,竞争是未声明的,而且我使用的是未定义的类'std::vector‘

我想返回一个vector<Competition>函数,但是有人告诉我,竞争是未声明的,而且我使用的是未定义的类'std::vector‘
EN

Stack Overflow用户
提问于 2012-04-27 23:55:49
回答 1查看 1.1K关注 0票数 1

我正在尝试使用一个函数来构建一个向量,这个函数被我称为vector<Competition> CompPop()。我想返回向量信息,这是一个类型的vector<Competition>。下面是函数的代码,返回向量和我的Competition类的头。

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

-error C2065:“竞赛”:未声明的标识符

'CompPop‘使用未定义的类'std::vector’

“竞争”:未声明的标识符

错误C2133:'info‘:未知大小

错误C2512:‘std::向量’:没有适当的默认构造函数可用

错误C2065:“竞争”:未声明的标识符

错误C2146:语法错误:缺少“;”在标识符'temp‘之前

错误C3861:'temp':找不到标识符

错误C2678:二进制“[”:没有找到任何操作符,它接受‘std::向量’类型的左操作数(或者没有可接受的转换)

代码语言:javascript
运行
复制
    #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;
    }

我的比赛标题:

代码语言:javascript
运行
复制
    #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;
     };
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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改为:

代码语言:javascript
运行
复制
#pragma once
#include <vector>
struct Competition;
std::vector<Competition> CompPop();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10359291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档