-
March 16th, 2011, 11:16 AM
#1
template
In vc++, If I comment out the lines in main, any error such as an extra letter in definition of "void array<T>::setArray", is ignored
by the compiler/linker. why?.
Code:
int main()
{
array< int > int_array(5);
int_array.setArray(0,1);
return 0;
}
#ifndef ARRAY_H_
#define ARRAY_H_
template< class T > class array {
private:
int size;
T *arrayA;
public:
array (int s) {
sz = s;
arrayA = new T [sz];
}
void setArray ( int index, T val);
};
template< class T >
void array<T>::setArray ( int index, T val) {
arrayA[index] = val;
}
#endif
-
March 16th, 2011, 11:23 AM
#2
Re: template
First, be aware that there is a std::array class which your class may conflict with. (Although that's a fixed-size array; you seem to be attempting to create a dynamic array class more similar to std::vector.)
To answer your question, compilers are not required to do anything at all with templates that are not instantiated. They may not be able to detect syntax errors in that case.
-
March 16th, 2011, 11:24 AM
#3
Re: template
Why is your main() program defined before the template?
Regards,
Paul McKenzie
-
March 16th, 2011, 11:33 AM
#4
Re: template
Originally Posted by BilJ0
In vc++, If I comment out the lines in main, any error such as an extra letter in definition of "void array<T>::setArray", is ignored
by the compiler/linker. why?.
Code:
int main()
{
array< int > int_array(5);
int_array.setArray(0,1);
return 0;
}
#ifndef ARRAY_H_
#define ARRAY_H_
template< class T > class array {
private:
int size;
T *arrayA;
public:
array (int s) {
sz = s;
arrayA = new T [sz];
}
void setArray ( int index, T val);
};
template< class T >
void array<T>::setArray ( int index, T val) {
arrayA[index] = val;
}
#endif
There are sometimes good reasons to do it, but these are rare occasions. So, here is my question... why are you trying to implement a vector like container class when you already have std::vector at you finger tips? Is it a learning exercise? If so, you can potentially learn quite a lot. If not, are you sure you really need to do it?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|