CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Jun 2011
    Posts
    2

    Question Newbie question, insertSort algorithm

    Hi guys, I'm trying to compile the following code in Visual Studio 2010 Express for a simple implementation of insertSort:

    InsertSort.h
    =========

    template <class T>
    void isort(const T& start, const T& end){

    for (T i=start; i<end; ++i){

    T::value_type value = *i;
    T dec = i;

    while(dec>start && *(dec-1) >= value){
    *dec = *(dec-1);
    --dec;
    }

    *dec = value;
    }
    }

    ISMain.cpp
    ========

    #include <iostream>
    #include "InsertSort.h"
    #include <vector>

    int main() {

    std::vector<double> vec(10, 0.0);
    for (int i=0;i<vec.size();i++) {
    vec[i] = rand();
    }

    isort(vec[0], vec[9]);
    std::cout << "vector sorted" << std::endl;
    return 0;
    }

    I'm getting a host of error messages:

    1>c:\users\hcm\documents\visual studio 2010\projects\insertsort\insertsort\ismain.cpp(3): warning C4067: unexpected tokens following preprocessor directive - expected a newline
    1>c:\users\hcm\documents\visual studio 2010\projects\insertsort\insertsort\ismain.cpp(7): error C2039: 'vector' : is not a member of 'std'
    1>c:\users\hcm\documents\visual studio 2010\projects\insertsort\insertsort\ismain.cpp(7): error C2065: 'vector' : undeclared identifier
    1>c:\users\hcm\documents\visual studio 2010\projects\insertsort\insertsort\ismain.cpp(7): error C2062: type 'double' unexpected
    1>c:\users\hcm\documents\visual studio 2010\projects\insertsort\insertsort\ismain.cpp(8): error C2065: 'x' : undeclared identifier
    1>c:\users\hcm\documents\visual studio 2010\projects\insertsort\insertsort\ismain.cpp(8): error C2228: left of '.size' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\hcm\documents\visual studio 2010\projects\insertsort\insertsort\ismain.cpp(9): error C2065: 'x' : undeclared identifier
    1>c:\users\hcm\documents\visual studio 2010\projects\insertsort\insertsort\ismain.cpp(9): error C2228: left of '.at' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\hcm\documents\visual studio 2010\projects\insertsort\insertsort\ismain.cpp(12): error C2065: 'x' : undeclared identifier
    1>c:\users\hcm\documents\visual studio 2010\projects\insertsort\insertsort\ismain.cpp(12): error C2065: 'x' : undeclared identifier
    1>c:\users\hcm\documents\visual studio 2010\projects\insertsort\insertsort\ismain.cpp(12): error C3861: 'isort': identifier not found

    I'm sure I'm making some obvious mistakes. Any help would be much appreciated!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Newbie question, insertSort algorithm

    Quote Originally Posted by MMagique View Post
    Hi guys, I'm trying to compile the following code in Visual Studio 2010 Express for a simple implementation of insertSort:
    Use code tags when posting code.
    Code:
    #include <vector>
    template <class T> 
    void isort(const T& start, const T& end)
    {
       typename T::value_type value = start; // < -- no such thing as double::value_type
    }
    
    int main()
    {
        std::vector<double> vec(10, 0.0);
        isort(vec[0], vec[9]);  // <--passing doubles here
    }
    This simple code when compiled shows the issue.

    You are passing double to the isort function as the T argument. There is no "value_type" for type double.

    Second, you must use the typename keyword as I have shown above for the code to even begin to compile.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jun 2011
    Posts
    2

    Re: Newbie question, insertSort algorithm

    Thanks Paul. Your suggestions were very helpful. I now realise that 'T' has to be type iterator and therefore doesn't need to be passed by reference. Code now compiles. Cheers!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured