Using vector data member in a class

Started by
6 comments, last by Paulius Maruska 17 years, 5 months ago
Hi buddies I want to create a vecot object in an orbitary class objects.But i have an error.My cpp code is : ----------------------- #include <iostream> #include<vector> using namespace std; class Base{ public: vector<Base> myV(3); };
Advertisement
#include <iostream>#include<vector>using namespace std;class Base {  // Declare the member  vector<Base> myV;public:  // Initialize it  Base() : myV(3) {}};


Of course, creating one Base requires the creation of 3 Base objects first, so creating an object of type Base will throw you into an infinite loop.
thx dude,
But i can't get what's my wrong .In the first code i changed vector <Base> to vector <int> and i declared it with 3 int members (something like array ,for exam i can write int a[5]; for declaring an array with five elements,But why i can't do that with vectors?)
thx

#include <iostream>
#include<vector>
using namespace std;
class Base{
public:
vector<int> myV(3);
};
You simply cant say that a Base consists of three Bases, because each of these threee Bases would have to be made up of three other Basis etc. ad infinitum.

You can however store Pointers to other Bases in the vector.
Quote:Original post by bigmantana
thx dude,
But i can't get what's my wrong .In the first code i changed vector <Base> to vector <int> and i declared it with 3 int members (something like array ,for exam i can write int a[5]; for declaring an array with five elements,But why i can't do that with vectors?)
thx

#include <iostream>
#include<vector>
using namespace std;
class Base{
public:
vector<int> myV(3);
};

Solution was already given to you. std::vector is a class that wraps a dynamic array. Saying std::vector< int > myV(3) calls vector constructor with a single argument, that says what the initial size of the vector should be. When declaring your own class - you can't call any constructor. Constructors of the data members can only be called in constructor of your own class. Therefor, the correct way of doing this is:
#include <vector>class Base {public:    Base () : myV(3) // <= calls constructor for your vector    {        // fill the vector with values    }private:    std::vector< int > myV;};

On the other hand, if you only need three elements, and you know that you will never need to have more - you should probably use the static array:
class Base {public:    Base ()    {        // fill the array with values    }private:    int myV[3];};

[EDIT - Man, I'm slow this morning [smile]]

Quote:Original post by bigmantana
thx dude,
But i can't get what's my wrong .In the first code i changed vector <Base> to vector <int> and i declared it with 3 int members (something like array ,for exam i can write int a[5]; for declaring an array with five elements,But why i can't do that with vectors?)
thx

#include <iostream>
#include<vector>
using namespace std;
class Base{
public:
vector<int> myV(3);
};


ToohrVyk has already posted what is wrong with that. You can't supply the constructor argument to a class member when you declare it like that. You have to supply it in the initialiser list of the class constructor:

#include <iostream>#include <vector>using namespace std;class Base{public:    vector<int> myV;    Base() : myV(3) { }   };
Thx more than 1 million time Paulius Maruska .I got it .I just didn't see this notation ( Base () : myV(3))
can you explain it a bit more .
thx

[Edited by - bigmantana on November 25, 2006 5:37:15 AM]
Recommended reading:
C++ FAQ Lite - you should go there, every time you have a question. Only after looking there, you should go to a forum to get some clarifications or explanations...

For your current problem, you should read about constructors and initialization lists.

Note: bookmark the faq - You can't imagine how many times it helped me, it will help you, too!

This topic is closed to new replies.

Advertisement