3

I have simple class in a header file: a.hh

#ifndef a_hh
#define a_hh

class a
{
public: 
    int i;
    a()
    {
        i = 0;
    }

};
#endif

Then i have a file:b.cc

#include <iostream> 
#include "a.hh"

using namespace std;

int main(int argc, char** argv)
{

    a obj;
    obj.i = 10;
    cout << obj.i << endl;
    return 0;
}
> 

Till this point everything is fine. I compile the code and it compiles fine. But as soon as i add a vector in the class:

#ifndef a_hh
#define a_hh

class a
{
public: 
    int i;
    vector < int > x;
    a()
    {
        i = 0;
    }

};
#endif

I get a compilation error as below:

> CC b.cc
"a.hh", line 7: Error: A class template name was expected instead of vector.
1 Error(s) detected.

What is the problem with declaring a vector here as a member?

Vijay
  • 65,327
  • 90
  • 227
  • 319

3 Answers3

5

You need to #include <vector> and use the qualified name std::vector<int> x;:

#ifndef a_hh
#define a_hh

#include <vector>

class a{
public:
    int i;
    std::vector<int> x;
    a()             // or using initializer list: a() : i(0) {}
    {
        i=0;
    }
};

#endif 

Other points:

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • @EitanT, I did actually compile it with no problems. But good spot and correct.ed – hmjd Sep 03 '12 at 07:36
  • Just out of curiosity, how did it compile then? – Eitan T Sep 03 '12 at 07:37
  • @EitanT, it (VC2010) seemed to ignore the superfluous qualification but gcc does not http://ideone.com/Iruvc. – hmjd Sep 03 '12 at 07:39
  • That damn MSVC does it again. – Eitan T Sep 03 '12 at 07:41
  • Not a big deal here, but it may teach good practices to use the initializer list for `i` instead of the delayed assignment. – ereOn Sep 03 '12 at 07:47
  • @ereOn, I did mention it in the actual code snippet as a comment. – hmjd Sep 03 '12 at 07:50
  • @EitanT it's a known "extension" in visual studio. – jcoder Sep 03 '12 at 08:06
  • @J99, it seems then that the disable extensions compiler switch (`/Za`) does really disable all. I compile with it when possible. – hmjd Sep 03 '12 at 08:13
  • @hmjd: I am trying to create vector of any type (I mean generic vector) of length 10. Please See the code http://ideone.com/c9UY48 . What's wrong in it? Why it fails in compilation ? – Destructor Apr 29 '16 at 06:19
1

declaring a vector as a class member:

#include <iostream>
#include <vector>
using namespace std;

class class_object 
{
    public:
            class_object() : vector_class_member() {};

        void class_object::add_element(int a)
        {   
            vector_class_member.push_back(a);
        }

        void class_object::get_element()
        {
            for(int x=0; x<vector_class_member.size(); x++) 
            {
                cout<<vector_class_member[x]<<" \n";
            };
            cout<<" \n";
        }

        private:
            vector<int> vector_class_member;
            vector<int>::iterator Iter;
};

int main()
{
    class_object class_object_instance;

    class_object_instance.add_element(3);
    class_object_instance.add_element(6);
    class_object_instance.add_element(9);

    class_object_instance.get_element();

    return 0;
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
0

1.You need to #include <vector> and using namespace std, then a.hh just like below:

#ifndef a_hh
#define a_hh

#include <vector>
using namespace std;

class a
{
public: 
    int i;
    vector <int> x;
    a()
    {
        i = 0;
    }

};
#endif

2. If you don't want to only use std namespace in all your code, you can specified the namespace before type, just like std::vector<int> x;

hupantingxue
  • 2,134
  • 3
  • 19
  • 24
  • 1
    It does not matter, if you use the namespace in all you code. – hupantingxue Sep 03 '12 at 07:56
  • 1
    It does matter. It pollutes the global namespace often causes problem. – Nawaz Sep 03 '12 at 08:49
  • The issue is whether you write your headers for use by anyone who might benefit from your class (in which case your header shouldn't drop the entire contents of `std` into the global namespace). Nawaz has given you -1 in effect for assuming that your header will be used only under the condition that everyone always uses `namespace std`. – Steve Jessop Sep 03 '12 at 09:00