| | |
help (overload << in a template class )
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hi, anyone can tell me what's wrong with my code? the only part has problem is the function overload << operator, any help is welcome.
#include <iostream>
using namespace std;
template <typename T, int SIZE>
class array
{
T data_[SIZE];
array (const array& other);
const array& operator = (const array& other);
public:
array(){};
T& operator[](int i) {return data_[i];}
const int getSize() const{return SIZE;};
const T& get_elem (int i) const {return data_[i];}
void set_elem(int i, const T& value) {data_[i] = value;}
operator T*() {return data_;}
friend ostream& operator<< <T, SIZE>(ostream&, const array<T, SIZE>&);
};
ostream& operator<< <T, SIZE> (ostream& out, const array<T, SIZE>& ar)
{
out<<"< ";
for(int i=0;i<ar.getSize();i++)
out<<ar.get_elem(i)<<" ";
out<<" >";
return out;
}
int main(void)
{
array<int, 10> intArray;
for(int i=0;i<10;i++) intArray.set_elem(i, i+100);
//std::cout<<" element 0: "<<intArray.get_elem(0)<<std::endl;
//int firstElem = intArray.get_elem(0);
//int* begin = intArray;
cout<<intArray;
}
#include <iostream>
using namespace std;
template <typename T, int SIZE>
class array
{
T data_[SIZE];
array (const array& other);
const array& operator = (const array& other);
public:
array(){};
T& operator[](int i) {return data_[i];}
const int getSize() const{return SIZE;};
const T& get_elem (int i) const {return data_[i];}
void set_elem(int i, const T& value) {data_[i] = value;}
operator T*() {return data_;}
friend ostream& operator<< <T, SIZE>(ostream&, const array<T, SIZE>&);
};
ostream& operator<< <T, SIZE> (ostream& out, const array<T, SIZE>& ar)
{
out<<"< ";
for(int i=0;i<ar.getSize();i++)
out<<ar.get_elem(i)<<" ";
out<<" >";
return out;
}
int main(void)
{
array<int, 10> intArray;
for(int i=0;i<10;i++) intArray.set_elem(i, i+100);
//std::cout<<" element 0: "<<intArray.get_elem(0)<<std::endl;
//int firstElem = intArray.get_elem(0);
//int* begin = intArray;
cout<<intArray;
}
operator<< doesn't need to be a friend since you only use the public interface of array in it. A non-member function only needs to be a friend if it has to have access to a classes private or protected members.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; template <typename T, int SIZE> class array { T data_[SIZE]; array (const array& other); const array& operator = (const array& other); public: array(){}; T& operator[](int i) {return data_[i];} const int getSize() const{return SIZE;}; const T& get_elem (int i) const {return data_[i];} void set_elem(int i, const T& value) {data_[i] = value;} operator T*() {return data_;} }; template <typename T, int SIZE> ostream& operator<<(ostream& out, const array<T, SIZE>& ar) { out<<"< "; for(int i=0;i<ar.getSize();i++) out<<ar.get_elem(i)<<" "; out<<" >"; return out; } int main(void) { array<int, 10> intArray; for(int i=0;i<10;i++) intArray.set_elem(i, i+100); //std::cout<<" element 0: "<<intArray.get_elem(0)<<std::endl; //int firstElem = intArray.get_elem(0); //int* begin = intArray; cout<<intArray; }
Thanks a lot for the help! That solves my problem.
One more question (sorry i am messed by template), in case I don't have public interface and will access _data[] directly, then I will define << as a friend, how to write the code in that way? I always get non_template function warning when I format code as below.
/salute
One more question (sorry i am messed by template), in case I don't have public interface and will access _data[] directly, then I will define << as a friend, how to write the code in that way? I always get non_template function warning when I format code as below.
/salute
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; template <typename T, int SIZE> class array { T data_[SIZE]; array (const array& other); const array& operator = (const array& other); public: array(){}; T& operator[](int i) {return data_[i];} const int getSize() const{return SIZE;}; const T& get_elem (int i) const {return data_[i];} void set_elem(int i, const T& value) {data_[i] = value;} operator T*() {return data_;} friend ostream& operator<<(ostream&, const array<T, SIZE>&); }; template <typename T, int SIZE> ostream& operator<<(ostream& out, const array<T, SIZE>& ar) { out<<"< "; for(int i=0;i<SIZE;i++) out<<ar.data_[i]<<" "; out<<" >"; return out; } int main(void) { array<int, 10> intArray; for(int i=0;i<10;i++) intArray.set_elem(i, i+100); //std::cout<<" element 0: "<<intArray.get_elem(0)<<std::endl; //int firstElem = intArray.get_elem(0); //int* begin = intArray; cout<<intArray; }
write it this way and it works, but when I write function body of overload << out of class declaration
then I get error.
then I get error.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; template <typename T, int SIZE> class array { T data_[SIZE]; array (const array& other); const array& operator = (const array& other); public: array(){}; T& operator[](int i) {return data_[i];} const int getSize() const{return SIZE;}; const T& get_elem (int i) const {return data_[i];} void set_elem(int i, const T& value) {data_[i] = value;} operator T*() {return data_;} friend ostream& operator<<(ostream& out, const array<T, SIZE>& ar){ out<<"< "; for(int i=0;i<SIZE;i++) out<<ar.data_[i]<<" "; out<<" >"<<endl; return out; } }; int main(void) { array<int, 10> intArray; for(int i=0;i<10;i++) intArray.set_elem(i, i+10); //std::cout<<" element 0: "<<intArray.get_elem(0)<<std::endl; //int firstElem = intArray.get_elem(0); //int* begin = intArray; cout<<intArray; }
In that situation, you make the operator inline. The correct way to define it outside of your class is way too awkward to be practical:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; template <typename T, int SIZE> class array; template <typename Ty, int sz> ostream& operator<<(ostream& out, const array<Ty, sz>& ar); template <typename T, int SIZE> class array { T data_[SIZE]; array (const array& other); const array& operator = (const array& other); public: array(){}; T& operator[](int i) {return data_[i];} int getSize() {return SIZE;} const T& get_elem (int i) const {return data_[i];} void set_elem(int i, const T& value) {data_[i] = value;} operator T*() {return data_;} friend ostream& operator<< <>(ostream& out, const array& ar); }; template <typename T, int SIZE> ostream& operator<<(ostream& out, const array<T, SIZE>& ar) { out<<"< "; for(int i=0;i<SIZE;i++) out<<ar.data_[i]<<" "; out<<" >"; return out; } int main(void) { array<int, 10> intArray; for(int i=0;i<10;i++) intArray.set_elem(i, i+100); //std::cout<<" element 0: "<<intArray.get_elem(0)<<std::endl; //int firstElem = intArray.get_elem(0); //int* begin = intArray; cout<<intArray; }
•
•
Join Date: Feb 2008
Posts: 50
Reputation:
Solved Threads: 1
can you berifly explain what compiler understand
when we declare template function befpre the class and declare it as friend function inside class and define it outside the class
Another Question
what this function mean
thank you in advance
when we declare template function befpre the class and declare it as friend function inside class and define it outside the class
Another Question
what this function mean
C++ Syntax (Toggle Plain Text)
operator T*() {return data_;}
thank you in advance
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
It returns a pointer to the member variable data_ of the array class.
•
•
Join Date: Feb 2008
Posts: 50
Reputation:
Solved Threads: 1
what about
can you berifly explain what compiler understand
when we declare template function befpre the class and declare it as friend function inside class and define it outside the class
Another question
the function
gives an error if i replaced it by
althougth the [] operator is overloaded before
plz reply and explain
thanks alot
can you berifly explain what compiler understand
when we declare template function befpre the class and declare it as friend function inside class and define it outside the class
Another question
the function
C++ Syntax (Toggle Plain Text)
template <typename T, int SIZE> ostream& operator<<(ostream& out, const array<T, SIZE>& ar) { out<<"< "; for(int i=0;i<SIZE;i++) out<<ar.data_[i]<<" "; out<<" >"; return out; }
gives an error if i replaced it by
C++ Syntax (Toggle Plain Text)
template <typename T, int SIZE> ostream& operator<<(ostream& out, const array<T, SIZE>& ar) { out<<"< "; for(int i=0;i<SIZE;i++) out<<ar.data_[i]<<" "; out<<" >"; return out; }
althougth the [] operator is overloaded before
plz reply and explain
thanks alot
![]() |
Similar Threads
- Trying to overload + for class to add to string 'is illegal' (C)
- Nested template class (C)
- Template class (C)
- unresolved external symbol when using a hashtable class (C++)
Other Threads in the C++ Forum
- Previous Thread: Newbie programmer
- Next Thread: $ followed by input value 123.45 right aligned
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





