Any way to overload [ ] for vector of structure?

  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    Structure Vector
AI Thread Summary
Overloading the `[]` operator for a vector of structures in C++ is possible, but the original code encounters issues due to incorrect initialization of the vector within the class. The vector should be declared without size in the class definition and initialized in the constructor, either by resizing it or using an initializer list. The operator overload must return an `int` type, matching the type of the data being accessed. Understanding the proper use of vector methods like `resize` and `at` is crucial for effective implementation. Properly addressing these points will enable successful operator overloading for the vector of structures.
  • #51
Filip Larsen said:
You are getting the error in line 40 because that friend function is not a member function, but more like a static function, and for those there are no this defined. Defining a friend function textually inside a class definition does no in itself mean its a member function; it means just the same as if the friend function was defined outside the lexical scope of the class. Also, contrast this with the member function you define in line 30 which do have a this defined, so name there implicitly refers to this->name.

As a minimal fix closest to your original intent I will suggest you just use rhs.name in line 40, or, since you overwrite mul2.name with Multi2 on the next line anyway, just use Multi2 in line 40 and skip the string copy. But if you get into more trouble fumbling around with those damn c-string you know what I am going to say :wink:.

I know you are right, I even try putting vec3 D(name, 0, 0, 0); inside main, it won't work. I tried using this->x inside the friend function and it doesn't work. So definitely what you said is true.

BUT I declare mul2 as object of Complex. Does it matter whether it's a member function or only a friend? I should be able to use the public member name[Nsize] for mul2. I still don't quite get what the reason.

Thanks
 
Last edited:
Technology news on Phys.org
  • #52
yungman said:
BUT I declare mul2 as object of Complex. Does it matter whether it's a member function or only a friend?

(I assume you still refer to your vec3 class in post #48 and not your new Complex class from post #50.)

As I replied in post #49, the friend operator overload is really considered to be just a function and not a member function with an implied object context (i.e. this). The fact that it in this case is declared in the lexical scope of the class does not change anything. Consider the example
C++:
#include <iostream>

using namespace std;

class Thing {
    int m_a;   
    friend void f(Thing& t) {
        t.m_a = 3;
    }
public:
    int a() const { return m_a; }
};

int main()
{
    Thing t;
    f(t);
    cout << "t.a = " << t.a();
    return 0;
}
this is really understood by the compiler as
C++:
#include <iostream>

using namespace std;

class Thing {
    int m_a;    
    friend void f(Thing& t);
public:
    int a() const { return m_a; }
};

void f(Thing& t) {
    t.m_a = 3;
}

int main() {
    Thing t;
    f(t);
    cout << "t.a = " << t.a();
    return 0;
}
That is, the function f is considered to "be outside" the Thing class even though it (in first example above) is define lexically inside the Thing class.

Also, I can see in your new Complex class you have defined Complex operator*(const Complex& lhs, const Complex& rhs), but since the first parameter is a Complex the normal practice is to define it as the overloaded member function Complex operator*(const Complex& rhs) const where left hand side inside the implementation of that member function is represented by this. The friend operator* notation/idiom is only required when the left-hand argument is NOT of the type in question, e.g. in your case if you want to have an operator so you can write (double expression)*(Complex expression).

yungman said:
I should be able to use the public member name[Nsize] for mul2. I still don't quite get what the reason.

In the context of your original question relating to this, yes, you can refer to it as mul2.name.
 
  • Like
Likes yungman
  • #53
Filip Larsen said:
(I assume you still refer to your vec3 class in post #48 and not your new Complex class from post #50.)

As I replied in post #49, the friend operator overload is really considered to be just a function and not a member function with an implied object context (i.e. this). The fact that it in this case is declared in the lexical scope of the class does not change anything. Consider the example
C++:
#include <iostream>

using namespace std;

class Thing {
    int m_a;
    friend void f(Thing& t) {
        t.m_a = 3;
    }
public:
    int a() const { return m_a; }
};

int main()
{
    Thing t;
    f(t);
    cout << "t.a = " << t.a();
    return 0;
}
this is really understood by the compiler as
C++:
#include <iostream>

using namespace std;

class Thing {
    int m_a; 
    friend void f(Thing& t);
public:
    int a() const { return m_a; }
};

void f(Thing& t) {
    t.m_a = 3;
}

int main() {
    Thing t;
    f(t);
    cout << "t.a = " << t.a();
    return 0;
}
That is, the function f is considered to "be outside" the Thing class even though it (in first example above) is define lexically inside the Thing class.

Also, I can see in your new Complex class you have defined Complex operator*(const Complex& lhs, const Complex& rhs), but since the first parameter is a Complex the normal practice is to define it as the overloaded member function Complex operator*(const Complex& rhs) const where left hand side inside the implementation of that member function is represented by this. The friend operator* notation/idiom is only required when the left-hand argument is NOT of the type in question, e.g. in your case if you want to have an operator so you can write (double expression)*(Complex expression).
In the context of your original question relating to this, yes, you can refer to it as mul2.name.
Thanks so much Filip, I am going to read through chapter 13 and 14 on classes. Old brain needs to refresh the memory concentrating on member data access and all that. It's so easy to just follow the syntax and do things without really understanding them, that's why I want to keep writing stupid codes like those to keep going through them to see anything slipped through the cracks.

I see what you mean use two explicit argument is not necessary for L and R are the same class object. I just want to practice that. That's new. Book does not elaborate on the streaming overload using friend.

Many thanks.
 
  • Like
Likes Filip Larsen
Back
Top