1

I am trying to build a vector using a function that I am calling vector<Competition> CompPop(). I want to return the vector info which is a type vector<Competition>. Below is my code for the function returning the vector and the header for my Competition class.

I'm getting the following errors (I'm using Visual Studio and the error message are very basic, leaving me guessing as to what I am actually doing wrong):

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

my Competition header:

    #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;
     };
Community
  • 1
  • 1
  • You did include `` and are using `std::vector` somehow, right? – chris Apr 27 '12 at 23:57
  • Do you have `#include `? Most if not all of these errors stem from the fact that it doesn't know about vector and that its a templated type that defines the operator `[]`. – jedwards Apr 27 '12 at 23:58
  • Yes, I did. Those were also the first things i checked :) Thanks for replying! –  Apr 28 '12 at 00:02
  • May we see the outside of the two blocks of code you've shown? It looks like the parts not shown are where the error most likely is. – chris Apr 28 '12 at 00:11
  • Hi, chris, I've edited my posts show they show the entire headers. Thanks! –  Apr 28 '12 at 00:20
  • You're not using the contents of `CompPop.h` anywhere in your `Competition.h`. I'd remove that include for `CompPop.h` – chris Apr 28 '12 at 00:25
  • Thank you so much! My errors are gone now! –  Apr 28 '12 at 00:29

2 Answers2

1

It looks like you're not #includeing the header for Competition in your source file.

As an aside, it also looks like you're doing using namespace std; in your header. This is not a good practice.

Edit based on updated info:

This is a circular dependency issue.

If you simply forward declare Competition and declare CompPop in CompPop.h, and add the implementation of CompPop to a CompPop.cpp, you'll break the cycle.

So change CompPop.h to:

#pragma once
#include <vector>
struct Competition;
std::vector<Competition> CompPop();
Community
  • 1
  • 1
Fraser
  • 74,704
  • 20
  • 238
  • 215
  • Actually I did have the include the competition header in my CompPop.h however, i also included CompPop.h in my competition header. I removed that and it seemed to do the trick. Does anyone know why that might have caused the issue?? Thanks! –  Apr 28 '12 at 00:10
  • It's not your headers that are the problem. The issue is that you need `#include "Competition.h"` in the source file you've shown us. – Fraser Apr 28 '12 at 00:14
  • Hi Fraser, the first is CompPop.h, I've edited my post above to show all my headers. Thanks! –  Apr 28 '12 at 00:19
0

It looks like you're missing either includes or using statements.

Namely either missing your header for Component, or the one for std::vector, or both.

Also ensure you have either using namespace std; or using std::vector;

If this isn't the case, please provide more of your code so we can examine it fully.

[Edit] Update based on your comments Are you using inclusion guards?

  • I've edited my posts to include my headers. As to inclusion guards, I don't think I am. I'll look it up now, but I don't know what they are. Thanks! –  Apr 28 '12 at 00:19
  • You were using inclusion guards, `#pragma once` is the simple way of doing it in Visual Studio. – reaper4334 Apr 28 '12 at 00:38