C++ Inheritence Help

Subscribe to C++ Inheritence Help 23 posts

avatar for lSWATLLAMA lSWATLLAMA 593 posts
Flag Post

class Vector : public Point
{
    public:
        Vector(float X = 0, float Y = 0, float Z = 0);
        Vector operator +(Vector *p);
};

Vector::Vector(float X, float Y, float Z) : Point(X, Y, Z)
{
    // the function call basically did setX(X) and that stuff.
}

Vector Vector.operator + (Vector &p) : Point(X, Y, Z)
{
    Vector outV;
    outV.setX(getX() + p.getX());
    outV.setY(getY() + p.getY());
    outV.setZ(getZ() + p.getZ());
    return outV;
}

I’m following the 3DBuzz tutorials for this, but I’ve been confused about it all day. I’d declared the functions setX(), setY(), setZ() and their corresponding “get” functions in the parent class Point. Everything works fine, until the last function. The error message says “expected unqualified-id before ‘.’ token”.

I know that normally I’d not do dot notation for the last function, but when I used :: in place of ‘.’, it caused more errors.

 
avatar for JamesObscura JamesObscura 250 posts
Flag Post
 a -> b 

“member b of object pointed to by a”

I believe this is what you’re looking for.

 
avatar for stage_phrite stage_phrite 42 posts
Flag Post

JamesObscura is correct…..when using pointers or references you use an arrow(→) instead of a dot to access member functions and variables.

 
avatar for lSWATLLAMA lSWATLLAMA 593 posts
Flag Post

Ah yes, I had forgotten about that! The tutorial wanted me to do “Vector::operator” though, since it’s a function from a class.

I tried this, but the same error occurred. I hope I was right in replacing ‘.’ with ‘→’ and making that be the only change.

 
avatar for JamesObscura JamesObscura 250 posts
Flag Post

Sorry, that’s as far as my familiarity with C++ goes.

If I was going to take a guess though, I don’t think that’s how you format an operator overload.

I think it should be something like:

MyClass MyClass::operator + (Myclass &p)

Edit: Also return Vector(getX() + p→getX, getY() + p→getY, getZ() + p→getZ)?

 
avatar for lSWATLLAMA lSWATLLAMA 593 posts
Flag Post

Edit: Also return Vector(getX() + p→getX, getY() + p→getY, getZ() + p→getZ)?

Using dot notation there isn’t causing any of the problems.

The code you posted isn’t working either, and causes an entirely different error, though I’m guessing that if I did MyClass.operator, it wouldn’t even work, now that I think about it.

Thanks for trying to help at least – so far, no one has tried helping me on a forum that’s dedicated to C++ on a different site.

 
avatar for BobTheCoolGuy BobTheCoolGuy 3758 posts
Flag Post
Originally posted by JamesObscura:

Sorry, that’s as far as my familiarity with C++ goes.

If I was going to take a guess though, I don’t think that’s how you format an operator overload.

I think it should be something like:

MyClass MyClass::operator + (Myclass &p)

Edit: Also return Vector(getX() + p→getX, getY() + p→getY, getZ() + p→getZ)?

That looks like the right way to declare the function, although maybe no space between operator and + not sure. p.getX() is correct since p is a reference and not a pointer.

Llama, post whatever error messages / code you’re using if you still have errors.

 
avatar for JamesObscura JamesObscura 250 posts
Flag Post

I was just suggesting turning those 4 lines into 1.

And I’m like 95% sure you’re going to want the arrows and not the periods there. I’m also 90% sure I don’t know anything about C++. I’m also 1503% sure I’m just making up percentages.

 
avatar for lSWATLLAMA lSWATLLAMA 593 posts
Flag Post

class Vector : public Point
{
    public:
        Vector(float X = 0, float Y = 0, float Z = 0);
        Vector operator +(Vector *p);
};

Vector::Vector(float X, float Y, float Z) : Point(X, Y, Z)
{
    // the function call basically did setX(X) and that stuff.
}

Vector Vector::operator + (Vector &p) : Point(X, Y, Z)
{
    Vector outV;
    outV.setX(getX() + p.getX());
    outV.setY(getY() + p.getY());
    outV.setZ(getZ() + p.getZ());
    return outV;
}

The error:


Line 149: error: prototype for 'Vector Vector::operator+(Vector*)'
Line 141: candidate is: Vector Vector::operator+(Vector*)

Line 149 refers to ‘Vector Vector::operator + (Vector &p) : Point(X, Y, Z)’.
Line 141 refers to ‘Vector operator +(Vector *p);’

I know I don’t need → there. I tried using them and it made no difference. If I were referring to the pointer, p, I think i’d need to use → instead of ‘.’.

 
avatar for Fourpio Fourpio 2 posts
Flag Post

The function prototype in the class definition doesn’t match its implementation (Vector* vs. Vector&). Additionally, non-constructors are not allowed to have member initializer lists (I’m not sure what the Point(X, Y, Z) is doing there anyway).

 
avatar for lSWATLLAMA lSWATLLAMA 593 posts
Flag Post

I didn’t include the unnecessary Point() class in the code I posted, since everything was working fine, including having Vector inherit stuff from Point until I included ‘Vector Vector::operator +(Vector &p) : Point(X, Y, Z).’

p is declared as a pointer and then referenced to with &.

I don’t understand by what you say with non-constructors not being able to have member initializer lists. The whole term is unfamiliar to me, and I may be missing something obvious.

 
avatar for BobTheCoolGuy BobTheCoolGuy 3758 posts
Flag Post
Originally posted by lSWATLLAMA:

I didn’t include the unnecessary Point() class in the code I posted, since everything was working fine, including having Vector inherit stuff from Point until I included ‘Vector Vector::operator +(Vector &p) : Point(X, Y, Z).’

p is declared as a pointer and then referenced to with &.

I don’t understand by what you say with non-constructors not being able to have member initializer lists. The whole term is unfamiliar to me, and I may be missing something obvious.

In your class, : public Point makes you extend the Point class. In your constructor, : Point(X,Y,Z) makes you call the constructor in the Point class. : Point(X,Y,Z) makes no sense in the operator function.

Secondly, your function header must match the prototype – so you can’t declare it with a pointer *p in one place and then &p in another place.

 
avatar for lSWATLLAMA lSWATLLAMA 593 posts
Flag Post

Taking the Point constructor from the disfunctional function (yes, idiotic pun intended) just produced the same errors.

 
avatar for BobTheCoolGuy BobTheCoolGuy 3758 posts
Flag Post
Originally posted by lSWATLLAMA:

Taking the Point constructor from the disfunctional function (yes, idiotic pun intended) just produced the same errors.

Change *p to &p at the top

 
avatar for lSWATLLAMA lSWATLLAMA 593 posts
Flag Post

Error: Only constructors take base initializers.
Error: X was not declared in this scope
Error: Y was not declared in this scope
Error: Z was not decalred in this scope

Now I tried doing what you said, Coolio, but then outside declaring Vector *p; that just produced the same errors as the previous ones.

 
avatar for BobTheCoolGuy BobTheCoolGuy 3758 posts
Flag Post
class Vector : public Point
{
    public:
        Vector(float X = 0, float Y = 0, float Z = 0);
        Vector operator+(Vector &p);
};

Vector::Vector(float X, float Y, float Z) : Point(X, Y, Z)
{
    // the function call basically did setX(X) and that stuff.
}

Vector Vector::operator+(Vector &p) 
{
    Vector outV;
    outV.setX(getX() + p.getX());
    outV.setY(getY() + p.getY());
    outV.setZ(getZ() + p.getZ());
    return outV;
}

So your code looks like this and you’re still getting errors?

 
avatar for lSWATLLAMA lSWATLLAMA 593 posts
Flag Post

Yes, exactly like that, and I get the same exact error. If I had the code exactly as you do right now, but added : Public(X, Y, Z) to Vector Vector::operator+(Vector &p) it gives many more errors.

 
avatar for jonathanasdf jonathanasdf 3910 posts
Flag Post
#include <iostream>

class Point
{
  float x, y, z;
  public:
      Point(float X = 0, float Y = 0, float Z = 0) : x(X), y(Y), z(Z) {}
      float getX() { return x; }
      float getY() { return y; }
      float getZ() { return z; }
      void setX(float X) { x = X; }
      void setY(float Y) { y = Y; }
      void setZ(float Z) { z = Z; }
};

class Vector : public Point
{
    public:
        Vector(float X = 0, float Y = 0, float Z = 0);
        Vector operator+(Vector &p);
};

Vector::Vector(float X, float Y, float Z) : Point(X, Y, Z)
{
    // the function call basically did setX(X) and that stuff.
}

Vector Vector::operator+(Vector &p)
{
    Vector outV;
    outV.setX(getX() + p.getX());
    outV.setY(getY() + p.getY());
    outV.setZ(getZ() + p.getZ());
    return outV;
}

int main() {
  Vector a(1,2,3), b(4,5,6);
  Vector c = a+b;
  std::cout << c.getX() << " " << c.getY() << " " << c.getZ() << std::endl;
  return 0;
}

works perfectly for me :<

 
avatar for lSWATLLAMA lSWATLLAMA 593 posts
Flag Post

Well, I don’t really want to use your Point class at all, but the rest of the code works perfectly for me.
Could you explain how you figured it out and how you made some of it work? I don’t get why Vector &p is working here but not in my original code.

Either way, you saved my ass bro, now I can finish up that inheritance tutorial… I’ll wait 12 hours though – 12:30 AM here.

 
avatar for EndlessSporadic EndlessSporadic 10478 posts
Flag Post
Vector Vector::operator+(Vector &p) 
{
    Vector outV;
    outV.setX(getX() + p.getX());
    outV.setY(getY() + p.getY());
    outV.setZ(getZ() + p.getZ());
    return outV;
}

Quick question about this. I read that you should not return a local variable in C++ since it would be returning a variable that goes out of scope (thereby giving you an error). Is this only for references and pointers or is it for everything?

 
avatar for lSWATLLAMA lSWATLLAMA 593 posts
Flag Post

I’ve never read or heard anything about not returning local variables. In functions, it’s frequently necessary to return locals. Probably only applies to pointers, though I’ve never heard anything about that either.

 
avatar for stage_phrite stage_phrite 42 posts
Flag Post
Originally posted by EndlessSporadic:

Vector Vector::operator+(Vector &p)
{
Vector outV;
outV.setX(getX() + p.getX());
outV.setY(getY() + p.getY());
outV.setZ(getZ() + p.getZ());
return outV;
}

Quick question about this. I read that you should not return a local variable in C++ since it would be returning a variable that goes out of scope (thereby giving you an error). Is this only for references and pointers or is it for everything?

That is only for references and pointers. With standard variables when returning it makes a copy…….with pointers and references it sends a position in memory(which could contain data when returned but then be empty when actually used)

 
avatar for EndlessSporadic EndlessSporadic 10478 posts
Flag Post
Originally posted by lSWATLLAMA:

I’ve never read or heard anything about not returning local variables. In functions, it’s frequently necessary to return locals. Probably only applies to pointers, though I’ve never heard anything about that either.

From the book I am reading:

Although returning a reference can be an efficient way to send information back to a calling function, you have to be careful not to return a reference to an out-of-scope object — an object that ceases to exist. For example, the following function returns a reference to a string object that no longer exists after the function ends — and that’s illegal.

string& badReference()
{
    string local = "item";
    return local;
}

So my question is if this extends to pointers as well as references. I kind of answered my own question in my mind why this isn’t a problem with passing by value (since it passes a copy back).

EDIT:

That is only for references and pointers. With standard variables when returning it makes a copy…….with pointers and references it sends a position in memory(which could contain data when returned but then be empty when actually used)

Thanks!