Template question: list<Bar<double>> ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • janzon@gmail.com

    #1

    Template question: list<Bar<double>> ...


    Hi

    I have a class Foo that I use both as a Foo<doubleand a Foo<float>. I
    want to put instances of this class in a list container. The list
    container itself is a template and need a type argument. The naive way
    below does not work.

    list<Bar<double >myList;

    It produces the error message: Error: "," expected instead of ">>". How
    do I solve this problem?


    Thanks in advance, Daniel

  • Thomas Tutone

    #2
    Re: Template question: list&lt;Bar&lt; double&gt;&gt; ...

    jan...@gmail.co m wrote:
    Hi
    >
    I have a class Foo that I use both as a Foo<doubleand a Foo<float>.
    Those are two _different_ classes, instantiated from the same class
    template.
    I want to put instances of this class in a list container.
    Since they are two different classes, you can't put both in a single
    list container without resorting to some tricks that probably would
    make this more complicated than it ought to be.
    The list
    container itself is a template and need a type argument. The naive way
    below does not work.
    >
    list<Bar<double >myList;
    Change the above line to:

    std::list<Bardo uble myList; // note space between the two ">"
    It produces the error message: Error: "," expected instead of ">>". How
    do I solve this problem?
    Well, that will solve _one_ of your problems.

    Best regards,

    Tom

    Comment

    • Michael

      #3
      Re: Template question: list&lt;Bar&lt; double&gt;&gt; ...

      list<Bar<double >myList;
      >
      It produces the error message: Error: "," expected instead of ">>". How
      do I solve this problem?
      The lexer is reading >as one token. You need to put a space between
      them:
      list<Bar<double myList;

      Michael

      Comment

      • Thomas Tutone

        #4
        Re: Template question: list&lt;Bar&lt; double&gt;&gt; ...

        Thomas Tutone wrote:
        list<Bar<double >myList;
        >
        Change the above line to:
        >
        std::list<Bardo uble myList; // note space between the two ">"
        Sorry, typo. Should be:

        std::list<Bar<d ouble myList; // note space between the two ">"

        Best regards,

        Tom

        Comment

        • janzon@gmail.com

          #5
          Re: Template question: list&lt;Bar&lt; double&gt;&gt; ...

          The list
          container itself is a template and need a type argument. The naive way
          below does not work.

          list<Bar<double >myList;
          >
          Change the above line to:
          >
          std::list<Bardo uble myList; // note space between the two ">"
          Adding the missing '<' it works perfectly. So one need to add a space
          not to confuse the compiler? I mean, so it doesn't think you want to
          use the >stream operator.
          It produces the error message: Error: "," expected instead of ">>". How
          do I solve this problem?
          >
          Well, that will solve _one_ of your problems.
          Well, the other is quite similar :)

          Comment

          • Pete Becker

            #6
            Re: Template question: list&lt;Bar&lt; double&gt;&gt; ...

            janzon@gmail.co m wrote:
            >
            I have a class Foo that I use both as a Foo<doubleand a Foo<float>.
            You have a template named Foo, from which you create two classes,
            Foo<doubleand Foo<float>.
            I
            want to put instances of this class in a list container. The list
            container itself is a template and need a type argument. The naive way
            below does not work.
            >
            list<Bar<double >myList;
            >
            It produces the error message: Error: "," expected instead of ">>". How
            do I solve this problem?
            >
            typedef Foo<doubledescr iptive_name;
            list<descriptiv e_namemyList;

            or:

            list< Foo<double myList;

            or wait for a C++0x compiler, which will be available sometime in the
            next ten years, and will accept:

            list<Foo<double >myList;

            --

            -- Pete

            Author of "The Standard C++ Library Extensions: a Tutorial and
            Reference." For more information about this book, see
            www.petebecker.com/tr1book.

            Comment

            • Marcus Kwok

              #7
              Re: Template question: list&lt;Bar&lt; double&gt;&gt; ...

              janzon@gmail.co m wrote:
              The list
              container itself is a template and need a type argument. The naive way
              below does not work.
              >
              list<Bar<double >myList;
              >>
              >Change the above line to:
              >>
              >std::list<Bard ouble myList; // note space between the two ">"
              >
              Adding the missing '<' it works perfectly. So one need to add a space
              not to confuse the compiler? I mean, so it doesn't think you want to
              use the >stream operator.
              As of now, yes. Usually, when it requires it, I will also add an extra
              space at the beginning for symmetry:

              std::list< Bar<double myList;

              IIRC, this will be fixed in C++0x.

              --
              Marcus Kwok
              Replace 'invalid' with 'net' to reply

              Comment

              Working...