Thread: Vector Allocation

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    New York
    Posts
    9

    Vector Allocation

    I am trying to allocate a vector during the default constructor of an object I have.

    What I am trying to allocate is this:
    Code:
    vector<bin_index> m_table;
    What I want is a vector of size 4 to be allocated with each slot containing an empty 'bin_index' object.
    I have a default constructor for bin_index to set values to 0 but it is still not working. This is what I have tried so far:
    Code:
    //try number 1
    bin_index bi;
    m_table(4,bi);
    
    //try number 2
    m_table(4,bin_index());
    
    //try number 3
    m_table(4);
    I know you can allocate a vector initially just like an array but I am having some trouble with the syntax when it includes objects. I am allocating it initially because I am implementing a hash table so I need a vector instead of an array.


    Here is the basic error message I keep getting:
    Code:
    error: no match for call to ‘(std::vector<bin_index, std::allocator<bin_index> >) (int, bin_index&)’

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think you want to write:
    Code:
    vector<bin_index> m_table(4);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    This works:
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    struct bin_idx
    {
        int idx;
        bin_idx() : idx(0) {}
    };//bin_idx
    
    int main()
    {
        bin_idx bi;
        vector<bin_idx> v1(4);
        vector<bin_idx> v2(4, bi);
        vector<bin_idx> v3(4, bin_idx());
    
        return 0;
    }//main
    Post a similiar example that demonstrates your issue.

    gg

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Is this what you're trying to do?
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    class Object
    {
    public:
    	Object()
    	:	m_Vec(4) {}
    
    	size_t VecSize()
    	{
    		return m_Vec.size();
    	}
    
    private:
    	vector<string>  m_Vec;
    };
    
    
    int main()
    {
    	Object obj;
    	cout << "Size = " << obj.VecSize() << endl;
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2008
    Location
    New York
    Posts
    9
    Quote Originally Posted by laserlight View Post
    I think you want to write:
    Code:
    vector<bin_index> m_table(4);
    Yea this is what gave me that message.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Location
    New York
    Posts
    9
    Quote Originally Posted by cpjust View Post
    Is this what you're trying to do?
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    class Object
    {
    public:
    	Object()
    	:	m_Vec(4) {}
    
    	size_t VecSize()
    	{
    		return m_Vec.size();
    	}
    
    private:
    	vector<string>  m_Vec;
    };
    
    
    int main()
    {
    	Object obj;
    	cout << "Size = " << obj.VecSize() << endl;
    	return 0;
    }
    This is exactly what I am trying to do! But I have a vector of bin_index instead of string and it gives me that error message when I try to do this.

    I will try to put together an example like Codeplug did

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> it gives me that error message when I try to do this.

    Can you post the error message you get when you try that code? It should be different than the one you posted. That one looks like what you would get if you put the code in red inside the constructor instead of the initializer list. Notice that in your code example the problem is that you are attempting to call a constructor on an already constructed object.

Popular pages Recent additions subscribe to a feed