overloading operators problem

Hi guys I have this assignment for class, thinking it was going to be pretty easy turns out I can't figure a thing out. My main problem is with overloading operators multiple times and overloading the index operator. Im almost ready to pull my hair out trying to do this damn assignment, especially since we right our programs in UNIX. Here is a link to the assignment page

http://turing.cs.niu.edu/~t90kjm1/CS241/Assign/as05_cs241_sp10.html

Im not sure what could be wrong with it, it wont even let me see any debugging since I think there is something wrong with the operators I overloaded. Also I am not really looking for a direct answer maybe just a nudge in the right direction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef VECTOR3_H
#define VECTOR3_H

class Vector3
        {
        friend ostream & operator<<(ostream &, const Vector3 &);
        friend float & operator*(const float &, const Vector3 &);

        private:
                float x;
                float y;
                float z;
        public:
                Vector3(float newX=0, float newY=0, float newZ=0);
                Vector3 operator+(const Vector3 &) const;
                Vector3 operator-(const Vector3 &) const;
                float operator*(const Vector3 &) const;
                Vector3 operator*(const float &) const;
                Vector3&  operator[](int) const;
                const Vector3& operator[](int &) const;
                bool operator==(const Vector3 &) const;
        };
#endif /* VECTOR3_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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "Vector3.h"
#include <iostream>

using namespace std;

Vector3::Vector3(flost newX, float newY, float newZ)
{
        x = newX;
        y = newY;
        z = newZ;
}

Vector3 Vector3::operator+(const Vector3 &rightObj) const
{
        Vector3 temp;
        temp.x = x + rightObj.x;
        temp.y = y + rightObj.y;
        temp.z = z + rightObj.z;

        return temp;
}

Vector3 Vector3::operator-(const Vector3 &rightObj) const
{
        Vector3 temp;   temp.x = x - rightObj.x;
        temp.y = y - rightObj.y;
        temp.z = z - rightObj.z;

        return temp;
}

float Vector3::operator*(const Vector3 &rightObj) const
{
        Vector3 temp;
        float temp2;

        temp.x = x * rightObj.x;
        temp.y = y * rightObj.y;
        temp.z = z * rightObj.z;

        temp2 = temp.x + temp.y + temp.z;

        return temp2;
}

Vector3 Vector3::operator*(const float &multX)
{
        Vector3 temp;

        temp.x = x * multX;
        temp.y = y * multY;
        temp.z = z * multZ;

        return temp;
}

float operator*(const float &multX, const Vector3 &rightObj)
{
        Vector3 temp;

        temp.x = multX * rightObj.x;
        temp.y = multX * rightObj.y;
        temp.z = multX * rightObj.z;

        return temp;
}

bool Vector3::operator==(const Vector3 &rightObj) const
{
        return x == rightObj.x && y == rightObj.y && z == rightObj.z;
}

Vector3& Vector3::operator[](int &index)
{
        if(index == 0)
                return x;
        else if(index == 1)
                return y;
        else if(index == 2)
                return z;
}

const float& Vector3::operator[](int index) const
{
        if(index == 0)
                return x;
        else if(index == 1)
                return y;
        else if(index == 2)
                return z;
}

ostream& operator<<(ostream &out, const Vector3 &rightObj)
{
        out << "(" << rightObj.x << ", " << rightObj.y << ", " << rightObj.z << ")";

        return out;
}


Also the make file I made to run the program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#Compiler variables
CCFLAGS = -ansi -Wall

#Rule to link object code files to create executable file
assign5: assign5.o Vector3.o
        g++ $(CCFLAGS) -o assign5 assign5.o Vector3.o

#Rules to compile source code files to object code
assign5.o: assign5.cpp Vector3.h
        g++ $(CCFLAGS) -c assign5.cpp

Vector3.o: Vector3.cpp Vector3.h
        g++ $(CCFLAGS) -c Vector3.cpp

#Psuedo-target to remove object code and executable files
clean:
        -rm *.o assign5

Last edited on
What problems are you seeing? What does your main() function look like?

The only obvious things wrong are the declarations of your operator[] functions.
They should be:

1
2
                const Vector3&  operator[](int) const;
                Vector3& operator[](int);


(EDIT: I think. I'm not sure what operator[] on your type does. Vector3 seems
to me to be more like Point3D.)

(EDIT AGAIN: Ok, I read the assignment, and your operator[] functions should
be declared as follows:)

1
2
                float  operator[](int) const;
                float& operator[](int);


NOT as I wrote above.
Last edited on
Thanks for the quick reply jsmith, but the problems I see are unreadable. The screen fills up with a lot of junk. I mainly see std::operator<< but I swear that looked fine to me. I tried the code below but I still receive the huge amount of text. Also sorry to make you read the whole assignment.

1
2
 float  operator[](int) const;
float& operator[](int);


Heres a blurb of the stuff I see

1
2
3
4
5
6
7
8
9
 _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/ostream:195: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/ostream:204: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/ostream:208: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/ostream:213: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/ostream:217: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/ostream:225: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/ostream:229: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/bits/ostream



EDIT:At the end it says this

make: *** [assign5.o] Error 1

ANOTHER EDIT: I realized the flost up there in the constructor function but still didnt work when fixed.

ANOTHER EDIT:
1
2
3
4
5
6
7
8
9
10
Vector3 Vector3::operator*(const float &multX)
{
        Vector3 temp;

        temp.x = x * multX;
        temp.y = y * multY;
        temp.z = z * multZ;

        return temp;
}


Fixed that but still not working getting all that garbage.
Last edited on
You have to post more of the compile error. The excerpt above isn't useful. You also have to post the code that
is causing the compile error.
Sorry I didnt know what was causing it until I asked my professor today. She said that it was due to not #include <iostream> and using namespace std; in the header file. So I got it all figured out but too late to hand in. Thanks for the help though.
Topic archived. No new replies allowed.