views:

96

answers:

4

I'm stuck! I have this very simple test code and I can't get it to compile! I have used the same code many times before but now it won't work!
I have this simple program

#include <vector>
#include <iostream>
#include "Rswap.h"
using namespace std;
int main(){
Rswap test();
    cin.get();
return 0;}

And then the rswap.cpp...

#include <vector>
#include "Rswap.h"
Rswap::Rswap(){
    V.push_back(9);
};

And then the rswap.h...

#ifndef Rswap_h
#define Rswap_h


class Rswap{
public:
  vector<int>V;
  Rswap();
};
  #endif

I'm using Visual studio 2008. Is there something wrong that is obvious and I'm missing or what could it be! As I said I have used this snippet on several differnet occassions and I can't find any difference between this and those that work... And i have tried both vector < int > V; and vector <int> V; without any luck

I have stared at this blindly now for some while so I thought it's better to ask here!

rswap.h(7) : error C2143: syntax error : missing ';' before '<'
    rswap.h(7) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    rswap.h(7) : error C2238: unexpected token(s) preceding ';'
+5  A: 

At the point you #include "Rswap.h", you haven't declared using namespace std; yet, so the reference to vector in Rswap.h must be qualified with a namespace. Simply declare the vector with a namespace prefix.

class Rswap{
public:
  std::vector<int>V;
  Rswap();
};

Also, I suggest you #include <vector> from Rswap.h rather than relying on whoever uses Rswap.h to get the #include order right.

Marcelo Cantos
This fixes the problem But I know I have done this without the std:: prefix!
mrbuxley
And that's because I have declared namespace std before that in the earlier versions just like You said! Thanks... I new it was something simple!
mrbuxley
Just to be clear, header files should never have namespace declarations, and they probably shouldn't be included after namespace declarations either.
Marcelo Cantos
OK,I did not know that! Have some code to fix...(=
mrbuxley
+1  A: 

Ah, your using namespace std comes too late - it should be inserted BEFORE the rswap.h include. In any case, it's MUCH better to write std::vector instead.

zvrba
Yep!, that's it!
mrbuxley
+2  A: 

It should be

  std::vector<int>V;
jpalecek
A: 

You need to #include <vector> in rswap.h.

robert