std::vector operator[] problem

Hi!

I'm getting this error and I don't understand why. I hope you can help me.


Matrix3f.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef MATRIX3F_H
#define MATRIX3F_H
#include <vector>


class Matrix3f
{
    public:
        Matrix3f();

        Matrix3f (const std::vector<float>& content);

        Matrix3f(const Matrix3f& other);

        Matrix3f& operator=(const Matrix3f& other);


        std::vector<float> m_elements;
    protected:
    private:
};

#endif // MATRIX3F_H


Matrix3f.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "Matrix3f.h"
Matrix3f::Matrix3f()
{}


Matrix3f::Matrix3f(const std::vector<float>& content)
{
    for(auto i =  m_elements.begin(); i!= m_elements.end(); i++)
        {
           m_elements[i] = content[i];
        }
}

Matrix3f::Matrix3f(const Matrix3f& other)
{
    for(auto i = other.m_elements.begin(); i!=other.m_elements.end(); i++)
        {
            m_elements[i] = other.m_elements.[i];
        }
}

Matrix3f& Matrix3f::operator=(const Matrix3f& other)
{
    if (this == &other) return *this;
    for(auto i = other.m_elements.begin(); i!=other.m_elements.end(); i++)
        {
            m_elements[i] = other.m_elements.[i];
        }

    return *this;
}


||=== Build: Debug in Math (compiler: GNU GCC Compiler) ===|

E:\Math\src\Matrix3f.cpp||In constructor 'Matrix3f::Matrix3f(const std::vector<float>&)':|
E:\Math\src\Matrix3f.cpp|6|warning: 'Matrix3f::m_elements' should be initialized in the member initialization list [-Weffc++]|
E:\Math\src\Matrix3f.cpp|10|error: no match for 'operator[]' in '((Matrix3f*)this)->Matrix3f::m_elements[i]'|
E:\Math\src\Matrix3f.cpp|10|note: candidates are:|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|750|note: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) [with _Tp = float; _Alloc = std::allocator<float>; std::vector<_Tp, _Alloc>::reference = float&; std::vector<_Tp, _Alloc>::size_type = unsigned int]|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|750|note:   no known conversion for argument 1 from '__gnu_cxx::__normal_iterator<float*, std::vector<float> >' to 'std::vector<float>::size_type {aka unsigned int}'|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|765|note: std::vector<_Tp, _Alloc>::const_reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) const [with _Tp = float; _Alloc = std::allocator<float>; std::vector<_Tp, _Alloc>::const_reference = const float&; std::vector<_Tp, _Alloc>::size_type = unsigned int]|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|765|note:   no known conversion for argument 1 from '__gnu_cxx::__normal_iterator<float*, std::vector<float> >' to 'std::vector<float>::size_type {aka unsigned int}'|
E:\Math\src\Matrix3f.cpp|10|error: no match for 'operator[]' in 'content[i]'|
E:\Math\src\Matrix3f.cpp|10|note: candidates are:|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|750|note: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) [with _Tp = float; _Alloc = std::allocator<float>; std::vector<_Tp, _Alloc>::reference = float&; std::vector<_Tp, _Alloc>::size_type = unsigned int]|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|750|note:   no known conversion for argument 1 from '__gnu_cxx::__normal_iterator<float*, std::vector<float> >' to 'std::vector<float>::size_type {aka unsigned int}'|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|765|note: std::vector<_Tp, _Alloc>::const_reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) const [with _Tp = float; _Alloc = std::allocator<float>; std::vector<_Tp, _Alloc>::const_reference = const float&; std::vector<_Tp, _Alloc>::size_type = unsigned int]|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|765|note:   no known conversion for argument 1 from '__gnu_cxx::__normal_iterator<float*, std::vector<float> >' to 'std::vector<float>::size_type {aka unsigned int}'|
E:\Math\src\Matrix3f.cpp|14|warning: 'Matrix3f::m_elements' should be initialized in the member initialization list [-Weffc++]|
E:\Math\src\Matrix3f.cpp|18|error: no match for 'operator[]' in '((Matrix3f*)this)->Matrix3f::m_elements[i]'|
E:\Math\src\Matrix3f.cpp|18|note: candidates are:|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|750|note: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) [with _Tp = float; _Alloc = std::allocator<float>; std::vector<_Tp, _Alloc>::reference = float&; std::vector<_Tp, _Alloc>::size_type = unsigned int]|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|750|note:   no known conversion for argument 1 from '__gnu_cxx::__normal_iterator<const float*, std::vector<float> >' to 'std::vector<float>::size_type {aka unsigned int}'|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|765|note: std::vector<_Tp, _Alloc>::const_reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) const [with _Tp = float; _Alloc = std::allocator<float>; std::vector<_Tp, _Alloc>::const_reference = const float&; std::vector<_Tp, _Alloc>::size_type = unsigned int]|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|765|note:   no known conversion for argument 1 from '__gnu_cxx::__normal_iterator<const float*, std::vector<float> >' to 'std::vector<float>::size_type {aka unsigned int}'|
E:\Math\src\Matrix3f.cpp|18|error: expected unqualified-id before '[' token|
E:\Math\src\Matrix3f.cpp||In member function 'Matrix3f& Matrix3f::operator=(const Matrix3f&)':|
E:\Math\src\Matrix3f.cpp|27|error: no match for 'operator[]' in '((Matrix3f*)this)->Matrix3f::m_elements[i]'|
E:\Math\src\Matrix3f.cpp|27|note: candidates are:|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|750|note: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) [with _Tp = float; _Alloc = std::allocator<float>; std::vector<_Tp, _Alloc>::reference = float&; std::vector<_Tp, _Alloc>::size_type = unsigned int]|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|750|note:   no known conversion for argument 1 from '__gnu_cxx::__normal_iterator<const float*, std::vector<float> >' to 'std::vector<float>::size_type {aka unsigned int}'|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|765|note: std::vector<_Tp, _Alloc>::const_reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) const [with _Tp = float; _Alloc = std::allocator<float>; std::vector<_Tp, _Alloc>::const_reference = const float&; std::vector<_Tp, _Alloc>::size_type = unsigned int]|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_vector.h|765|note:   no known conversion for argument 1 from '__gnu_cxx::__normal_iterator<const float*, std::vector<float> >' to 'std::vector<float>::size_type {aka unsigned int}'|
E:\Math\src\Matrix3f.cpp|27|error: expected unqualified-id before '[' token|
||=== Build failed: 6 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|


Thanks!

Asus.
Last edited on
Your vector has no size.
Thanks for the answer!

Even if a give it a size with
m_elements.reserve(20000);

I'm still getting the same error.


Asus.
reserve() doesn't change the size, it changes the capacity.
use vector.push_back(). Your vector has to have a size if its to be used with a subscript. So instead of m_elements[i]=data[i], do m_elements.push_back(data[i])
Right. What should I do then?

I tried this:
1
2
3
4
5
6
7
Matrix3f::Matrix3f(const std::vector<float>& content):m_elements(9)
{
    for(auto i =  m_elements.begin(); i!= m_elements.end(); i++)
        {
           m_elements[i] = content[i];
        }
}


But I'm still getting the same error.

EDIT: I just read your messege Cody. That worked, thanks.
Last edited on
The vector is already defined. You would have to use the resize method to adjust the size of the vector.

Look at the different methods for the vector class

http://www.cplusplus.com/reference/vector/vector/
Topic archived. No new replies allowed.