Thread: Why the example C++ code in MSDN is wrong?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Why the example C++ code in MSDN is wrong?

    I copied the exact code from MSDN here http://msdn.microsoft.com/en-us/library/f1b2td24.aspx
    Code:
    // template_friend1.cpp
    // compile with: /EHsc
    
    #include <iostream>
    using namespace std;
    
    template <class T> class Array {
       T* array;
       int size;
    
    public:
       Array(int sz): size(sz) {
          array = new T[size];
          memset(array, 0, size * sizeof(T));
       }
    
       Array(const Array& a) {
          size = a.size;
          array = new T[size];
          memcpy_s(array, a.array, sizeof(T));
       }
    
       T& operator[](int i) {
          return *(array + i);
       }
    
       int Length() { return size; }
    
       void print() {
          for (int i = 0; i < size; i++)      
             cout << *(array + i) << " ";
    
          cout << endl;
       }
    
       template<class T>
       friend Array<T>* combine(Array<T>& a1, Array<T>& a2);
    };
    
    template<class T>
    Array<T>* combine(Array<T>& a1, Array<T>& a2) {
       Array<T>* a = new Array<T>(a1.size + a2.size);
       for (int i = 0; i < a1.size; i++)
          (*a)[i] = *(a1.array + i);
    
       for (int i = 0; i < a2.size; i++)
          (*a)[i + a1.size] = *(a2.array + i);
    
       return a;
    }
    
    int main() {
       Array<char> alpha1(26);
       for (int i = 0 ; i < alpha1.Length() ; i++)
          alpha1[i] = 'A' + i;
    
       alpha1.print();
    
       Array<char> alpha2(26);
       for (int i = 0 ; i < alpha2.Length() ; i++)
          alpha2[i] = 'a' + i;
    
       alpha2.print();
       Array<char>*alpha3 = combine(alpha1, alpha2);
       alpha3->print();
       delete alpha3;
    }
    Why the complier complains :
    g++ -c -g main.cc -o main.o
    main.cc:33: error: declaration of `class T'
    main.cc:4: error: shadows template parm `class T'
    make: *** [main.o] Error 1
    Anything wrong?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    When I compile it with VC++ 2008 I get these warnings:
    Code:
    1>..\Main.cpp(55) : error C2220: warning treated as error - no 'object' file generated
    1>..\Main.cpp(55) : warning C4244: '=' : conversion from 'int' to 'char', possible loss of data
    1>..\Main.cpp(61) : warning C4244: '=' : conversion from 'int' to 'char', possible loss of data
    Maybe they're using a compiler extension that gcc doesn't use?
    BTW, they also forgot to #include <cstring>

    The MSDN isn't really known for their high quality code.

    The Comeau compiler agrees with gcc.
    I got it to work by changing this:
    Code:
       template<class T>
       friend Array<T>* combine(Array<T>& a1, Array<T>& a2);
    to this:
    Code:
       template<class T1>
       friend Array<T1>* combine(Array<T1>& a1, Array<T1>& a2);
    Last edited by cpjust; 11-20-2008 at 09:37 PM.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by cpjust View Post
    The MSDN isn't really known for their high quality code.
    I agree here. That code is utterly, horrible, crap. *(array + i)? Isn't that exactly what array[i] is for? Also, don't use this with a class as type. The memset will completely ruin every class. Great job microsoft, once again...
    Beats me what the memset is all about anyway.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Just because the programmer preferred *(array+i) doesn't mean that it's bad code.

    Although MSDN code isn't perfect, usually very vague sometimes.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  5. #5
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Also, using Visual C++ it compiles and runs.
    So the source is correct within the context it is supplied for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. What's wrong with this code?
    By Luciferek in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2008, 12:02 PM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM