Results 1 to 6 of 6

Thread: templated classes with friend functions

  1. #1

    templated classes with friend functions

    I am trying to overload the >> operator in a templated class. Now normally, you could make a friend function. But I have heard that friend functions in a templated class must be inlined. Is that true? If so, what would the syntax be. Something as simple as this?

    template <typename T>
    class test
    {
    private:
    //data variables

    public:
    inline istream& operator >> (istream& stream, test& var);
    };

    Is my thinking correct on this. Cause I did try originally and make it a friend function but it didn't work. Got alot of errors about istream and >>. Thanks guys
    "Good...Bad...I'm the guy with the gun"
    -- Army of Darkness --

    In case I forget, thanks STU!!

  2. #2

    Thought of one more question too

    Say I have a string d ="-*abc";
    and I want to loop through it, is there a way I can store the address location of d[i] via a pointer. Can I store it in a stack? I tried it but of course it didn't work.

    stack<char*> temp; //couldnt it be a char pointer since d[i] is a char?
    for (int i=0;i<d.length();i++)
    {
    char* var=d[i];
    temp.push(var);
    }

    Any ideas?
    "Good...Bad...I'm the guy with the gun"
    -- Army of Darkness --

    In case I forget, thanks STU!!

  3. #3
    Regular Member
    Join Date
    Aug 1999
    Posts
    341
    First question, first part. I've had friends in templates without having them defined as inline without any problems, so I doubt it's a rule.

    First question, second part. Remember that any member defined in the class declaration is, by default, inline. So:

    Code:
    template <typename T> 
    class test {
     
    private: 
    
      //data variables 
    
    public:
    
      // The following method is inline... 
      istream& operator >> (istream& stream, test& var){
    
        // Do something and return it...
    
      } 
    
    };
    Therefore, if you want it to be a friend and inline, define it in the class--like so.

    Code:
    template <typename T> 
    class test {
     
    private: 
    
      //data variables 
    
    public:
    
      // The following method is inline... 
      friend istream& operator >> (istream& stream, test& var){
    
        // Do something and return it...
    
      } 
    
    };
    Second question, a string is a type of container. So, you can use iterators. Like so:

    Code:
    #include <iostream>
    #include <string>
    
    int main(int argc, char **argv){
    
      string s("-*abc");    // String
      string::iterator it;  // Iterator for the string
    
      // Print one character per line...
      for (it = s.begin(); it != s.end(); it++)
        cout << *it << '\n' << ends;
    
      // Print empty line...
      cout << '\n' << ends;
    
      // Set the iterator to the first character...
      it = s.begin();
    
      // Move to the third character (two characters ahead)...
      it += 2;
    
      // Print the third character...
      cout << *it << '\n' << ends;
    
      return 0;
    
    }  // End main()

  4. #4

    I'm a little confused on second answer

    I understand that strings are containers and I can use iterators. But how could I use them if I wanted to keep track of the following in the string

    string s("-*abc");

    I'd want to keep track of chars - and * . I was thinking stack because if I want to run through again and look for - then do something like if (stack.top()==locationof-)
    do something

    How could I do something similiar if I don't know the string, I just want to keep track of all the operators
    "Good...Bad...I'm the guy with the gun"
    -- Army of Darkness --

    In case I forget, thanks STU!!

  5. #5
    Regular Member
    Join Date
    Aug 1999
    Posts
    341
    You just want the positions, or a iterator to the actual contents of the positions? For the former you could try something like this:

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    int main(int argc, char **argv){
    
      string s("-*abc");                // String
      string ops("+-*/%");              // Operators
      vector<string::size_type> v_pos;  // Vector for the positions
    
      // Loop through and get the positions...
      for(string::size_type pos = 0;
          (pos = s.find_first_of(ops, pos)) != string::npos;
          pos++)
        v_pos.push_back(pos);
    
      // Print the results...
      for(vector<string::size_type>::iterator it = v_pos.begin();
          it != v_pos.end();
          it++)
        cout << "[ "
             << s[*it]
             << " ] found at position "
             << *it
             << '\n'
             << ends;
    
    }  // End main()
    For the latter, something like this should do the trick:

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    int main(int argc, char **argv){
    
      string s("-*abc");                // String
      string ops("+-*/%");              // Operators
      vector<string::iterator> v_it;    // Vector for the iterators
    
      // Loop through and get the values as iterators...
      for(string::size_type pos = 0;
          (pos = s.find_first_of(ops, pos)) != string::npos;
          pos++){
    
        string::iterator temp_it = s.begin() + pos;
    
        v_it.push_back(temp_it);
    
      }  // End for
    
      // Print the results...
      for(vector<string::iterator>::iterator it = v_it.begin();
          it != v_it.end();
          it++)
        cout << "[ "
             << **it
             << " ] found at position "
             << *it - s.begin()
             << '\n'
             << ends;
    
    }  // End main()

  6. #6

    thnx a million stu

    I'll give these a shot!
    "Good...Bad...I'm the guy with the gun"
    -- Army of Darkness --

    In case I forget, thanks STU!!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •