Template trouble

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

    #1

    Template trouble

    Hi - the following code compiled easily in VS.Net however using g++'s
    there are several errors relating to template (I'm using a list()) -
    using the latest ver. of g++
    Code:
    list <intmy_list2;
    int counter;
    const int size = 8;
    int *seq = new int[size];

    /* test values */
    my_list2.push_b ack(1);
    my_list2.push_b ack(2);
    my_list2.push_b ack(3);
    my_list2.push_b ack(1);
    my_list2.push_b ack(3);
    my_list2.push_b ack(2);
    my_list2.push_b ack(2);
    my_list2.push_b ack(3);

    list <int>::iterat or i;

    if (my_list2.empty ())
    cout << "List is empty " << endl;
    else
    {

    /* Adds integers to an array I need for 3rd lib. */
    for( i = my_list2.begin( ), count = 0; i != my_list2.end() && count <
    size; i++, count ++)
    {
    seq[count] = *i;
    }
    }

    /* printing out the values */
    for (int i = 0; i < size; i++)
    {
    cout << " test: " << seq[i] << endl;
    }

    Errors in g++:
    readSequence.c: In function 'int main()':
    readSequence.c: 561: error: expected primary-expression before
    'template'
    readSequence.c: 561: error: expected `;' before 'template'
    readSequence.c: 567: error: expected initializer before '*' token
    readSequence.c: 570: error: 'my_list2' was not declared in this
    scope
    readSequence.c: 584: error: overloaded function with no contextual type
    information
    readSequence.c: 584: error: parse error in template argument list
    readSequence.c: 584: error: expected `;' before ')' token
    readSequence.c: 586: error: 'seq' was not declared in this scope
    readSequence.c: 592: error: 'seq' was not declared in this scope
    make: *** [readSequence.o] Error 1

  • Alf P. Steinbach

    #2
    Re: Template trouble

    * idar.douglas.hi llgaar@gmail.co m:
    Hi - the following code
    Is not your actual code. Post a complete, minimal, actual example, and
    indicate where in that code the compiler reports an error.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Amadeus W. M.

      #3
      Re: Template trouble

      Errors in g++:
      readSequence.c: In function 'int main()':
      readSequence.c: 561: error: expected primary-expression before
      'template'
      So go to line 561 and see what the problem might be.
      Very likely fixing this might fix (some of) the subsequent
      errors.

      Comment

      • dougie

        #4
        Re: Template trouble

        The problem is at the for loop in the test code and my own code:

        for( i = my_list2.begin( ), count = 0; i != my_list2.end() && count <
        size; i++, count ++)
        where I get the following error:
        " overloaded function with no contextual type information"


        Alf P. Steinbach wrote:
        * idar.douglas.hi llgaar@gmail.co m:
        Hi - the following code
        >
        Is not your actual code. Post a complete, minimal, actual example, and
        indicate where in that code the compiler reports an error.
        >
        --
        A: Because it messes up the order in which people normally read text.
        Q: Why is it such a bad thing?
        A: Top-posting.
        Q: What is the most annoying thing on usenet and in e-mail?

        Comment

        • dougie

          #5
          Re: Template trouble

          solved!:

          list <int>::iterat or i; should be: list<int>::iter ator i;
          the problem was the whitespace


          dougie wrote:
          The problem is at the for loop in the test code and my own code:
          >
          for( i = my_list2.begin( ), count = 0; i != my_list2.end() && count <
          size; i++, count ++)
          where I get the following error:
          " overloaded function with no contextual type information"
          >
          >
          Alf P. Steinbach wrote:
          >
          * idar.douglas.hi llgaar@gmail.co m:
          Hi - the following code
          Is not your actual code. Post a complete, minimal, actual example, and
          indicate where in that code the compiler reports an error.

          --
          A: Because it messes up the order in which people normally read text.
          Q: Why is it such a bad thing?
          A: Top-posting.
          Q: What is the most annoying thing on usenet and in e-mail?

          Comment

          • Alf P. Steinbach

            #6
            Re: Template trouble

            * dougie:
            * dougie:
            >* Alf P. Steinbach wrote:
            >>* idar.douglas.hi llgaar@gmail.co m:
            >>>Hi - the following code
            >>>
            >>Is not your actual code. Post a complete, minimal, actual example, and
            >>indicate where in that code the compiler reports an error.
            >>
            >The problem is at the for loop in the test code and my own code:
            >>
            >for( i = my_list2.begin( ), count = 0; i != my_list2.end() && count <
            >size; i++, count ++)
            >where I get the following error:
            >" overloaded function with no contextual type information"
            >
            solved!:
            >
            list <int>::iterat or i; should be: list<int>::iter ator i;
            the problem was the whitespace
            Uh, good, problem solved, but bad, very bad!, that whitespace shouldn't
            really matter. Perhaps what's lacking is to write 'std::list'? And
            what about the 'count' (usage) versus 'counter' (declaration)?

            This compiles cleanly using MSVC 7.1 and g++ 3.4.4, and should compile
            cleanly on any conforming C++ implementation:

            #include <list>

            int main()
            {
            using namespace std;
            list <int>::iterat or i;
            }

            Btw., please don't top-post in this group.

            See the FAQ item "How can I find out about general netiquette so I don't
            embarrass myself?", currently at <url:
            http://www.parashift.c om/c++-faq-lite/how-to-post.html#faq-5.4>.

            --
            A: Because it messes up the order in which people normally read text.
            Q: Why is it such a bad thing?
            A: Top-posting.
            Q: What is the most annoying thing on usenet and in e-mail?

            Comment

            • Thomas J. Gritzan

              #7
              Re: Template trouble

              idar.douglas.hi llgaar@gmail.co m schrieb:
              Hi - the following code compiled easily in VS.Net however using g++'s
              there are several errors relating to template (I'm using a list()) -
              using the latest ver. of g++
              Code:
              list <intmy_list2;
              int counter;
              const int size = 8;
              int *seq = new int[size];
              [...]

              When using std::list for the list, why don't you use std::vector for the
              array? You can pass the address to the first element to the 3rd party
              library:

              std::vector seq;

              library_functio n(&seq[0], seq.size());
              list <int>::iterat or i;
              Please don't call an iterator "i". IMHO, i is the dogmatic name for an
              integer variable.
              I would call an iterator it or itor, or a better name depending on what
              the iterator is used for.

              --
              Thomas

              Comment

              Working...