Jump to content
  • Advertisement
Sign in to follow this  
Alpha_ProgDes

friends and templates

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Yeah I know I'm pushing it.
#ifndef INTERVAL_H
#define INTERVAL_H

#include <fstream>
using namespace std;

template <typename Component>
class Interval {
    
    public: 
        friend istream& operator>>(istream& in, Interval<Component>& interval); //this is line 11
        friend ostream& operator<<(ostream& out, const Interval<Component>& interval);
        int length () const;
        
    private:
        Component start;
        Component finish;
};



template<Component>
istream& operator>>(istream& in, Interval<Component>& interval)<>
   {
    
    in >> interval.start;
    in >> interval.finish;
    
    return in;
}

template<Component>
ostream& operator<<(ostream& out, const Interval<Component>& interval)<> {
    
    out << "Begin:\tFinish:\t Total:\t Cost:\n";
    out << interval.start; 
    out << "\t"; 
    out << interval.finish;
    out << " \t " << interval.length() << "\t";
    
    return out;
}

template<Component>
int Interval<Component>::length () const {
    
    int intervalLength = finish - start;
    
    return abs(intervalLength);
}
#endif

I'm getting errors of this kind:
11 C:\Bloodshed_Dev-C\Projects\Interval.h [Warning] friend declaration
 `std::istream&  operator>>(std::istream&, Interval<Component>&)'
 declares a non-template  
11 C:\Bloodshed_Dev-C\Projects\Interval.h [Warning] (if this is not what you
 intended, make sure the  function template has already been declared and add
 <> after the function  
Now I tried that and I still get the same errors.

Share this post


Link to post
Share on other sites
Advertisement

#ifndef INTERVAL_H
#define INTERVAL_H

#include <fstream>
using namespace std;

template <typename Component>
class Interval {

friend istream& operator>> <>(istream& in, Interval<Component>& interval); //this is line 11
friend ostream& operator<< <>(ostream& out, const Interval<Component>& interval);

public:
int length () const;

private:
Component start;
Component finish;
};



template<typename Component>
istream& operator>>(istream& in, Interval<Component>& interval)
{

in >> interval.start;
in >> interval.finish;

return in;
}

template<typename Component>
ostream& operator<<(ostream& out, const Interval<Component>& interval) {

out << "Begin:\tFinish:\t Total:\t Cost:\n";
out << interval.start;
out << "\t";
out << interval.finish;
out << " \t " << interval.length() << "\t";

return out;
}

template<typename Component>
int Interval<Component>::length () const {

int intervalLength = finish - start;

return abs(intervalLength);
}
#endif




The friend declaration has no idea that function is a template without the <> after the name (not in the function definition, but the friend declaration).

Perhaps C++ should have syntax like:

friend template <typename Component> istream& operator>> (etc...

but oh well. [smile] That probably creates problems that I can't foresee, thus the syntax is as it is in the code snippet.

Also, you don't need to put them in the public section (but I suppose it doesn't matter), and don't forget the typename in the function definition.

[Edited by - Robert F on August 14, 2005 10:32:22 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!