0

I have a mysterious problem. I keep getting a ‘vector’ does not name a type error when trying to define a variable in the tour class. The library seems installed correctly, since I can define vectors in main. According to many posts here I have checked for the right vector includes and std namespace. Still I get the error.

main.cpp

#include <vector>
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <cmath>
#include <cstdlib>
#include "tour.h"
using namespace std;

int main () {

        //vector declaration works here
return 0;
}

tour.h

#ifndef TOUR_H_
#define TOUR_H_

#include<vector>
using namespace std;

class tour
{
    public:
      //variables
      int teamID;
      vector<int> tourseq; //this is where the error occurs

      //functions
      tour(int);

};

#endif /* TOUR_H_ */

tour.cpp

#include<vector>
#include "tour.h"
using namespace std;

tour::tour(int id){
teamID = id;
}

What could be wrong here?

dorien
  • 5,265
  • 10
  • 57
  • 116
  • 7
    Doesn't look like real code. One tip, though: Stop using `using namespace`. Just say no. I bet that your problems will go away in the process. – Kerrek SB Sep 11 '13 at 14:50
  • Use constructor initializer lists. – chris Sep 11 '13 at 14:53
  • What do you mean no real code, I stripped it to the minimum relevant part as I thought was the best practice on this site? Anyway, I did not use to have namespace std at first, added it according to a post I saw. Removing it does not fix the problem. – dorien Sep 11 '13 at 14:53
  • The problem is that your code doesn't seem to have seem to have anything that would give you the errors you claim to see, so could it be that you removed the problem in the stripping down process? – juanchopanza Sep 11 '13 at 14:55
  • 1
    @dorien: Are you sure this code produces the error? It doesn't for me: http://ideone.com/wuLdnE – Mike Seymour Sep 11 '13 at 14:55
  • 1
    @dorien It isn't _the_ real code (http://coliru.stacked-crooked.com/a/38d18f89a6c7cbfb). It's "real" code, alright. Real includes, e.g. :) – sehe Sep 11 '13 at 14:56
  • BTW, concerning `using namespace std`, the post you want to read it [this one](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – juanchopanza Sep 11 '13 at 14:56
  • @Mike Seymous. Yes, strange he. Because I did it this way in my other projects. Starting to think it's an eclipse setup issue. – dorien Sep 11 '13 at 14:57
  • Could I be doing something wrong with the seperated files? – dorien Sep 11 '13 at 14:59
  • Thanks @sehe, didn't know that. Interesting post. – dorien Sep 11 '13 at 14:59

1 Answers1

2

Instead of writing using namespace std; and vector<int> tourseq; consider writing std::vector<int> tourseq;.

You probably shouldn't put using namespace std; in your code anyway.

utnapistim
  • 26,809
  • 3
  • 46
  • 82
  • 10
    Sorry, this is good advice, but given the question it makes no sense as an answer. – sehe Sep 11 '13 at 14:51
  • 1
    I would remove the "probably" entirely, to make this even better advice. – juanchopanza Sep 11 '13 at 14:53
  • Yeah, I tried everything. I did not have namespace there at the beginning, but did it because I read it somewhere. Removing it has no effect. – dorien Sep 11 '13 at 14:55
  • Thanks. I said "probably" because I figured there may be cases when it makes sense (even though I can't think of any except for dirty prototyping code - and even then I wouldn't use it) – utnapistim Sep 11 '13 at 14:57
  • @dorien, can you just pre-process the compilation unit? what compiler are you using? – utnapistim Sep 11 '13 at 15:01
  • Totally strange. I waited a while, did std:: and it started working. Bizar! But thanks very much! – dorien Sep 11 '13 at 15:16