Template Argument Problem
-
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
-
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
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
-
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
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
-842150451Just 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
-
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
-842150451Just 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
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.
-
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.
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 adouble
. This results in the stream going "bad" when it encounters the period. The program works as expected in VS2008 but not in MSVC6.Steve