retrieve class methode vector memebers

Hi,

I have a short question the class methods. I have written a class (a home-brew mathematical function) which has 4 private members and two public ones the two public ones are std::vector<double> type. The main method returns a std::vector<double> by filling up one of the public member vectors with data. the question is whether I have to overload the operator [] in order to retrieve the elements of this vector. So far I got this error message:

no match for ‘operator[]’ in ‘data[i]’


If I have to do that then how do you overload the [] operator to retrieve elements of the result of member method.

thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector> //std::vector
#include <stdexcept> //std::out_of_range
#include <iostream> //std::cout
class A
{
    public:
    std::vector<double> A_;
    A(){std::vector<double> A__ {1.2,3.4,5.6}; A_.swap(A__);} //Just initializing the vector;
    double operator[](unsigned index) //Returns a double, with parameter index
    {
        if(index >= (A_.size())) throw std::out_of_range("A::operator[]"); //If it's higer than vector size
        else return A_[index]; //else return expected result
    }
};

int main()
{
    A a; //New obejct
    std::cout << a[2]; //output: 5.6
}
Hi,

thanks for the answer. Are you using c++11 option when you compiled this code? I only have the -std=c++0x option. I get an error message when I compile this.
Hi again,

I tried to solve some of the errors but I still get a segmentation fault.
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
33
34
#include <vector>
#include <stdexcept>
#include <iostream>
class A
{
    private:
    int Y;

    public:
    std::vector<double> data;

    A(int y):Y(y){};
   ~A() {};

   std::vector<double> operator()(int Y){
       for (int i=0; i<Y; i++)
    data[i]=i;
    return data;
   }

    double operator[](int index)
    {
        return data[index];
    }
};

using namespace std;

int main()
{
    int Y = 200;
    A a = A(Y);
    std::cout << a[2] << "\n";
}


Do I have to define the cout in the class?
your vector is empty, yet you are trying to access the third element
I thought I filled with doubles from 0.0000 to 200.0000.

Why isnt it filled?
after some confusion I got some more confusion for myself.

if I compile this with gcc -std=c++0x

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <math.h>
#include <fstream>
#include <iostream>
#include <typeinfo>
#include <vector>
#include <sstream>
#include <stdlib.h>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <array>
#include <cassert>

class A
{
    private:
    int Y;

    public:
    std::vector<int> data;
    A();
    A(int y):Y(y){};
    ~A(){};
    
   std::vector<int> operator()(int Y){
       for (int i=0; i<Y; i++)
    data[i]=i;
    return data;
   }

double operator[](int index)
    {
        return data[index];
    }
};

using namespace std;

int main()
{
    int YY = 200;
    A a(YY);
    //a.setY(YY);
    //= A(Y);
    std::cout << a[2] << "\n";
}


The error is :



/tmp/cc3LZeAf.o: In function `main':
class_vector.cpp:(.text+0x41): undefined reference to `std::cout'
class_vector.cpp:(.text+0x46): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(double)'
class_vector.cpp:(.text+0x56): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/cc3LZeAf.o: In function `__static_initialization_and_destruction_0(int, int)':
class_vector.cpp:(.text+0xa5): undefined reference to `std::ios_base::Init::Init()'
class_vector.cpp:(.text+0xaa): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc3LZeAf.o: In function `__gnu_cxx::new_allocator<int>::deallocate(int*, unsigned int)':
class_vector.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE10deallocateEPij[__gnu_cxx::new_allocator<int>::deallocate(int*, unsigned int)]+0xd): undefined reference to `operator delete(void*)'
/tmp/cc3LZeAf.o:(.eh_frame+0x8b): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status



Hi,

I figured out the error problem it was caused by using the wrong compiler gcc instead of g++. but the vector is still empty.
> I thought I filled with doubles
¿where?

your operator(), which you never call, may access out of bounds too.
Hi,

I came up with yet another version in which I believe I do call for the operator A a(200)

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <math.h>
#include <fstream>
#include <iostream>
#include <typeinfo>
#include <vector>
#include <sstream>
#include <stdlib.h>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <array>
#include <cassert>

using namespace std;

class A
{
    private:
    int y;

    public:
     std::vector<int> data;
     A();
     A(int y){};
     ~A(){};

vector<int> operator()(int y){
       for (int i=0; i<y; i++)
    data.push_back(i);
    return data;
   }

double operator[](int index)
    {
        return data[index];
    }
double size(){

return data.size();
}

void getpoint()
        {
            for(int x=0; x<data.size(); x++)
            {
                cout<<data[x]<<" \n";
            };
            cout<<" \n";
        }


};

//using namespace std;

int main()
{
    int yy = 200;
    A a(yy); // here I fill up the vector from 0 to 200
    std::cout << a.size() << "\n";
}





Is this the way one calls the operator ()?
A a(yy); calls the constructor (that in your case does nothing)

There isn't really a difference between function calls and operators
1
2
3
4
int main(){
   A a(42); //constructing an object
   a(13); //calling the operator()
}



ps: learn to indent
Hi,

thanks a lot! Now I get it! it works now!

:) I know I should indent!

Thanks again!
Topic archived. No new replies allowed.