1

呼び出している関数を使用してベクトルを構築しようとしていますvector<Competition> CompPop()。type である vector info を返したいです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;
     };
4

2 に答える 2

1

ソース ファイル#includeのヘッダーを ing していないようです。Competition

余談ですがusing namespace std;、ヘッダーでやっているようにも見えます。 これは良い習慣ではありません

更新された情報に基づいて編集:

これは循環依存の問題です。

CompPop.h で単純に前方宣言Competitionと宣言CompPopを行い、CompPop.cpp に実装を追加するとCompPop、サイクルが中断されます。

CompPop.h を次のように変更します。

#pragma once
#include <vector>
struct Competition;
std::vector<Competition> CompPop();
于 2012-04-28T00:05:59.033 に答える
0

includes または using ステートメントが欠落しているようです。

つまり、Component のヘッダー、std::vector のヘッダー、またはその両方が欠落しています。

また、次のいずれかがあることを確認してusing namespace std;くださいusing std::vector;

そうでない場合は、完全に調査できるようにコードをさらに提供してください。

[編集]コメントに基づく更新 インクルージョン ガードを使用していますか?

于 2012-04-28T00:12:21.867 に答える