CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    setget function in a template class

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    template <class T, int element>
    class Array
    {
    public:
      void putElement(T x, int index);
      T getElement(int index);
      int &setgetInteger();
    
    private:
      T item[element];
      int integer;
    };
    
    template <class T, int element>
    void Array<T, element>::putElement(T x, int index)
    {
      item[index] = x;
    }
    
    template <class T, int element>
    T Array<T, element>::getElement(int index)
    {
      return item[index];
    }
    
    template <class T, int element>
    int Array<T, element>::setgetInteger()
    {
      return integer;
    }
    
    int main()
    {
      char *name[] = {"Meier", "Schulze", "Lehmann", "Aust"};
      double contents[]= {2200.5, 1300.5, 1700.30, 4888.80};
    
      Array<char *, 4> nameArray;
      Array<double, 4> contentsArray;
    
      nameArray.setgetInteger() = 56;
    
      for(int i = 0; i < 4; i++)
      {
        nameArray.putElement(name[i], i);
        contentsArray.putElement(contents[i], i);
      }
    
      for(i = 0; i < 4; i++)
      {
        cout << setiosflags(ios::left) << setw(10) << nameArray.getElement(i) 
             << resetiosflags(ios::left) << setw(8) << contentsArray.getElement(i) << endl;
      }
    
      cout << "Integer output: " << nameArray.setgetInteger() << endl;
    
      return 0;
    }
    This doesn't compile because of the setget function that I put in. My question is why can't I make it work with a specific value being returned? Is this a limitation of template classes or something? Sorry for the silly question, I'm just not familiar with this.
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  2. #2
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238
    I've tried something like this ...
    Code:
    template <class T, int element, int integer>
    ... and making all of the appropriate changes along the way, but that still didn't work .
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  3. #3
    Join Date
    Mar 2003
    Location
    Germany, K-Town
    Posts
    578
    Maybe you tell us what the compiler error is first
    By the way:
    nameArray.setgetInteger() = 56;
    This doesn't do anything but initializing an anonymous temporary variable with 56.

    I think what you meant is that (note the reference type):
    Code:
    template <class T, int element>
    int & Array<T, element>::setgetInteger()
    {
      return integer;
    }
    Just as a side note.
    - Matthias

    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." - Bjarne Stroustrup

  4. #4
    Join Date
    Mar 2003
    Location
    Germany, K-Town
    Posts
    578
    I just recognized your declaration and definition of setgetInteger() don't fit. In the definition you return an int, in the declaration an int ref.
    - Matthias

    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." - Bjarne Stroustrup

  5. #5
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238
    Originally posted by matthias_k
    Maybe you tell us what the compiler error is first
    By the way:

    This doesn't do anything but initializing an anonymous temporary variable with 56.

    I think what you meant is that (note the reference type):
    Code:
    template <class T, int element>
    int & Array<T, element>::setgetInteger()
    {
      return integer;
    }
    Just as a side note.
    You son of a biscuit . That did the trick :sigh: .
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  6. #6
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238
    Originally posted by matthias_k
    I just recognized your declaration and definition of setgetInteger() don't fit. In the definition you return an int, in the declaration an int ref.
    Yeh, fixed it. I need to get home asap, this is killing me to be so long abroad .
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  7. #7
    Join Date
    Mar 2003
    Location
    Germany, K-Town
    Posts
    578
    That has happened to me too many times as well... don't be ashamed
    - Matthias

    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." - Bjarne Stroustrup

  8. #8
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238
    Originally posted by matthias_k
    That has happened to me too many times as well... don't be ashamed
    Ja, aber hast du irgendwann diese Fehler in CodeGuru geposted?
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured