user119653
user119653

Reputation:

compiling error with vector in c++

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

Upvotes: 1

Views: 2248

Answers (4)

robert
robert

Reputation: 34458

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

Upvotes: 0

jpalecek
jpalecek

Reputation: 47770

It should be

  std::vector<int>V;

Upvotes: 1

zvrba
zvrba

Reputation: 24584

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.

Upvotes: 0

Marcelo Cantos
Marcelo Cantos

Reputation: 186118

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.

Upvotes: 4

Related Questions