Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Template Argument Problem

Template Argument Problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++phpcomdata-structureshelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sarath C
    wrote on last edited by
    #1

    I've created function as follows

    template void BubbleSort(int nArraySize)
    {
    // Reading Data
    T* pArray = new T[nArraySize];
    int nIdx;
    cout<<"Enter the items to be sorted"<<endl;
    for ( nIdx = 0;nIdx < nArraySize;nIdx++)
    {
    cin>>pArray[nIdx];
    }
    ....
    }

    and called this function as follows

    int main()
    {
    int x;
    cin >> x;
    switch( x )
    {
    case 1:
    BubbleSort( 3 );
    break;
    case 2:
    BubbleSort ( 3 );
    default:
    break;
    }
    }

    This code works fine with Visual C++ 2008 and Dev C++ compiler. cin>>Array[nIdx]; breaks when I compile and execute the code using Visual C++ 6.0 Is there any work around for this problem? or something missing with my usage?

    -Sarath. "Great hopes make everything great possible" - Benjamin Franklin

    My blog - Sharing My Thoughts, An Article - Understanding Statepattern

    S 1 Reply Last reply
    0
    • S Sarath C

      I've created function as follows

      template void BubbleSort(int nArraySize)
      {
      // Reading Data
      T* pArray = new T[nArraySize];
      int nIdx;
      cout<<"Enter the items to be sorted"<<endl;
      for ( nIdx = 0;nIdx < nArraySize;nIdx++)
      {
      cin>>pArray[nIdx];
      }
      ....
      }

      and called this function as follows

      int main()
      {
      int x;
      cin >> x;
      switch( x )
      {
      case 1:
      BubbleSort( 3 );
      break;
      case 2:
      BubbleSort ( 3 );
      default:
      break;
      }
      }

      This code works fine with Visual C++ 2008 and Dev C++ compiler. cin>>Array[nIdx]; breaks when I compile and execute the code using Visual C++ 6.0 Is there any work around for this problem? or something missing with my usage?

      -Sarath. "Great hopes make everything great possible" - Benjamin Franklin

      My blog - Sharing My Thoughts, An Article - Understanding Statepattern

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      I can guess what you mean by this:

      Sarath. wrote:

      template void BubbleSort(int nArraySize)

      but it would be nice if you took a little care to ensure your post is readable in its entirety. Also, did you forget break here?

      Sarath. wrote:

      case 2: BubbleSort ( 3 ); default:

      Sarath. wrote:

      cin>>Array[nIdx]; breaks when I compile and execute the code using Visual C++ 6.0

      What do you mean by "breaks"? Does it compile? If not what's the error message? If it compiles in what way is it not working as you expect?

      Steve

      S 1 Reply Last reply
      0
      • S Stephen Hewitt

        I can guess what you mean by this:

        Sarath. wrote:

        template void BubbleSort(int nArraySize)

        but it would be nice if you took a little care to ensure your post is readable in its entirety. Also, did you forget break here?

        Sarath. wrote:

        case 2: BubbleSort ( 3 ); default:

        Sarath. wrote:

        cin>>Array[nIdx]; breaks when I compile and execute the code using Visual C++ 6.0

        What do you mean by "breaks"? Does it compile? If not what's the error message? If it compiles in what way is it not working as you expect?

        Steve

        S Offline
        S Offline
        Sarath C
        wrote on last edited by
        #3

        I'm really sorry for not formatting the code well. Ok let me talk more about this issue Let's take simple sample

        template <class T>
        void Foo(int nArraySize)
        {
        T* pArray = new T[nArraySize];
        int nIdx;
        cout<<"Enter the items"<<endl;
        for ( nIdx = 0;nIdx < nArraySize;nIdx++)
        {
        cin>>pArray[nIdx];
        }

        cout<"The array contains"<<endl;
        for ( nIdx = 0; nIdx < nArraySize;nIdx++)
        {
            cout<<pArray\[nIdx\]<<endl;
        }
        

        }

        int main()
        {
        int x;
        cin >> x;
        switch( x )
        {
        case 1:
        Foo<float >( 3 );
        break;
        case 2:
        Foo<int> ( 3 );
        break;
        default:
        break;
        }
        }

        ; The program exits prematurely when we try to enter with float option Foo<flo> has been executed The output was as follows

        1
        Enter the items
        2.3
        The array contains
        2
        -842150451
        -842150451

        Just after entering the first value, the execution has break from the for loop. (the behavior we see when we try to read an int and entering invalid characters. I dont know the exact word for that). In my understanding, cin deducing the type of data as the last type used to invoke this function. in this case Foo<int>() For further analysis, I tried to analyze using char as template type case 3: Foo<char> ( 3 ); break; and I executed float option (not the char version of template function) the output was as follows 1 Enter the items 123.23 The array contains 1 2 3 The above are the ouput of VC6. (note that the for loop was not completely executed in this case. it has exited after entering the first element Is it clear now?

        -Sarath. "Great hopes make everything great possible" - Benjamin Franklin

        My blog - Sharing My Thoughts, An Article - Understanding Statepattern

        P 1 Reply Last reply
        0
        • S Sarath C

          I'm really sorry for not formatting the code well. Ok let me talk more about this issue Let's take simple sample

          template <class T>
          void Foo(int nArraySize)
          {
          T* pArray = new T[nArraySize];
          int nIdx;
          cout<<"Enter the items"<<endl;
          for ( nIdx = 0;nIdx < nArraySize;nIdx++)
          {
          cin>>pArray[nIdx];
          }

          cout<"The array contains"<<endl;
          for ( nIdx = 0; nIdx < nArraySize;nIdx++)
          {
              cout<<pArray\[nIdx\]<<endl;
          }
          

          }

          int main()
          {
          int x;
          cin >> x;
          switch( x )
          {
          case 1:
          Foo<float >( 3 );
          break;
          case 2:
          Foo<int> ( 3 );
          break;
          default:
          break;
          }
          }

          ; The program exits prematurely when we try to enter with float option Foo<flo> has been executed The output was as follows

          1
          Enter the items
          2.3
          The array contains
          2
          -842150451
          -842150451

          Just after entering the first value, the execution has break from the for loop. (the behavior we see when we try to read an int and entering invalid characters. I dont know the exact word for that). In my understanding, cin deducing the type of data as the last type used to invoke this function. in this case Foo<int>() For further analysis, I tried to analyze using char as template type case 3: Foo<char> ( 3 ); break; and I executed float option (not the char version of template function) the output was as follows 1 Enter the items 123.23 The array contains 1 2 3 The above are the ouput of VC6. (note that the for loop was not completely executed in this case. it has exited after entering the first element Is it clear now?

          -Sarath. "Great hopes make everything great possible" - Benjamin Franklin

          My blog - Sharing My Thoughts, An Article - Understanding Statepattern

          P Offline
          P Offline
          prasad_som
          wrote on last edited by
          #4

          VC6 compiler is notorious for not being C++ standards complient. If you see it call _Myt& operator>>(int& _X) version instead of _Myt& operator>>(double& _X) version of function. Which Microsoft fixed in later verions of compiler.

          S 1 Reply Last reply
          0
          • P prasad_som

            VC6 compiler is notorious for not being C++ standards complient. If you see it call _Myt& operator>>(int& _X) version instead of _Myt& operator>>(double& _X) version of function. Which Microsoft fixed in later verions of compiler.

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            The following call stack shows what's going on:

            std::basic_istream<char,std::char_traits<char> >::operator>>(int & 0xcdcdcdcd) line 103
            Foo(int 0x00000003) line 17
            main() line 34 + 7 bytes
            mainCRTStartup() line 206 + 25 bytes
            KERNEL32! 7c816fd7()

            As you can see operator>>(int& _X) is being called instead of correct operator for a double. This results in the stream going "bad" when it encounters the period. The program works as expected in VS2008 but not in MSVC6.

            Steve

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups