Operator Overload >>

I am trying to read data out of my txt file
the text file got vector for example (-1,2,6)
heres what i am trying to do but its not working
1
2
3
4
5
vector<int> v1;
while (!myfile2.eof()){
myfile2 >> v1;
	cout<< v1 << endl ;
}

I am trying to overload my operator this way :
1
2
3
4
5
template <class T>
istream& operator>>(istream& in, vector<T>& vec){
	in>>ignore(1)>>vec.x>>ignore(1)>>vec.y>>ignore(1)>>vec.z;
	return in;
}

This is the error i am getting :
operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class vector<int> &)" (??5@YAAAV?$basic_istream@DU?
Non-overloaded vectors don't have elements called x, y, and z... are you confusing the STL vector with the mathematical vector?
http://cplusplus.com/reference/stl/vector/

-Albatross
Assuming this vector is your custom vector, then you should post the full code and error message. This error message (as far as I can see) only says the cryptic name of your function, it doesn't say anything about what is wrong with it.
Hi,
This is my vector class
1
2
3
4
5
6
7
8
9
10
11
12
template <class T>
class vector
{
public:
	vector(){vector(0,0,0);};
        vector(T ,T ,T );
        friend ostream& operator<< <> (ostream& output,vector<T>& );
	template <class U>
        vector<T> plus(const vector<U>& vec);
//private:////
    T x,y,z;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template <class T>
class vector
{
public:
	vector(){vector(0,0,0);};
    vector(T ,T ,T );
    friend ostream& operator<< <> (ostream& output,vector<T>& );
	friend istream& operator>>(istream& in, vector<T>&);
	//template <class U>
	//T operator+(const T &rhs);
	vector<T> operator -();
	template <class U>
    vector<T> plus(const vector<U>& vec);
	vector<T> minus(const vector<T>& vec);
	vector<T> cross(const vector<T>& vec);
	T dot(const vector<T>& vec);
	vector<T> times(const T&);
	T length();
//private:////
    T x,y,z;
};
What are the errors? I don't think you ever actually posted a full error message.
I did overcome the error , by doing this
1
2
3
4
5
istream& operator>>(istream& in, vector<T>& vec){
	
	in>> "(">>vec.x >>",">>vec.y>>",">>vec.z>>")";
	return in;
}


now i am getting something one the screen but its not right it goes into infinite
i know for sure i am doing something wrong with overloading
This is how i cin data from the text file , the above is the overload and remember this is how is the formate for my data in the text file (-3,4,-1)
1
2
3
4
	
vector<int> v1
myfile2 >> v1;
	cout<< v1<< endl ;
this is the error i am getting when i do the above:1>c:\users\elkhas\documents\visual studio 2010\projects\finalporject\finalporject\vector.h(38): error C2678: binary '>>' : no operator found


&std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &&,signed char *)'
ok heres something new happening , it seems to work now but with unexpected numbers
inside my txt file is this :
(4,3,-2) (-3,-4,3) (-5,4,10)
(2,3,9) (-4,7,-1) (-1,8,8)
(-6,-10,-1) (-2,2,-2) (-2,10,-5)
main :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void read(){
	fstream myfile2;
	myfile2.open ("input.txt");
	vector<double> v1,v2,v3;
	//char v1,v2,v3;
	if (!myfile2.is_open())
	cerr <<"Failed to open file";
	else{
	while (!myfile2.eof()){
		for(int i=0;i<=2;i++){
	myfile2.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}
	myfile2 >>v1 >>v2 >>v3;
	cout<< v1 <<v2<<v3<<endl ;
		}
	}

}


operator overload:
1
2
3
4
5
6
7
8
9
10
11
12
template <class T>
istream& operator>>(istream& in, vector<T>& vec){
	
	in.ignore(1);
	in>> vec.x;
	in.ignore(1);
	in>>vec.y;
	in.ignore(1);
	in>>vec.z;
	in.ignore(1);
	return in;
}


this is what i am getting on the screen:
(4,3,-2)(-9.25596e+061,-9.25596e+061,-9.25596e+061)(-9.25596e+061,-9.25596e+061,
-9.25596e+061)
the first vector is there the other two it seems like its reading something else
ok guys thanks alot
i got it working
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void read(){
	fstream myfile2;
	myfile2.open ("input.txt");
	vector<double> v1,v2,v3;
	//char v1,v2,v3;
	if (!myfile2.is_open())
	cerr <<"Failed to open file";
	else{
	while (!myfile2.eof()){
		for(int i=0;i<=2;i++){
	myfile2.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}
	myfile2 >>v1;
	myfile2.ignore(10);
	myfile2 >>v2;
	cout<< v1 <<v2<<endl ;
		}
	}
Topic archived. No new replies allowed.