CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Join Date
    May 2015
    Posts
    10

    Vector of Templates Problem

    I want to create a vector of templates but I'm getting 2 linker errors.
    I am doing the following :

    template<typename T>
    int main()
    {
    vector<T> exp;
    return 0;
    }

    I really need to store elements of different types in the same vector. Please help thank you.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Vector of Templates Problem

    Victor Nijegorodov

  3. #3
    Join Date
    May 2015
    Posts
    10

    Re: Vector of Templates Problem

    I don't know anything about boost so can u provide code on how to use it to make vector which can hold any data type using the code I mentioned in the post

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Vector of Templates Problem

    I really need to store elements of different types in the same vector.
    Why? What are you trying to accomplish? c++ is a strongly typed language so mixing types within the same variable (whether a container or basic type) is not really supported by the languauge.

    If these different types are related - eg different derived classes from a base class then this is fairly simple. If the types are not related then it is a whole different scenario. The easiest way to do this is to use Boost any or Boost variant. If you're not using Boost then you'll need to 'roll your own'. This is doable but non-trivial.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Vector of Templates Problem

    Quote Originally Posted by Fateslayer View Post
    I don't know anything about boost ...
    I don't it either...
    The main point is you cannot "make vector which can hold any data type". Period.
    You could, however, make vector which can hold pointers to "any data type" if you cast these pointers to something like void*
    Victor Nijegorodov

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Vector of Templates Problem

    Quote Originally Posted by VictorN View Post
    I don't it either...
    The main point is you cannot "make vector which can hold any data type". Period.
    You could, however, make vector which can hold pointers to "any data type" if you cast these pointers to something like void*
    and some way of knowing to what data type to re-cast these void* pointers when they are needed to be used.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    May 2015
    Posts
    10

    Re: Vector of Templates Problem

    Quote Originally Posted by 2kaud View Post
    and some way of knowing to what data type to re-cast these void* pointers when they are needed to be used.
    Tell me how please with the code example

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Vector of Templates Problem

    Quote Originally Posted by Fateslayer View Post
    Tell me how please with the code example
    Possibly in the next couple of days when I have some time I'll take a look.

    But you haven't answered my question in post #4. Why do you want to this? What are you trying to accomplish?
    Last edited by 2kaud; May 25th, 2015 at 11:03 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Vector of Templates Problem

    Quote Originally Posted by Fateslayer View Post
    template<typename T>
    int main()
    {
    vector<T> exp;
    return 0;
    }
    templatizing main() ? ? ?

  10. #10
    Join Date
    May 2015
    Posts
    10

    Re: Vector of Templates Problem

    Quote Originally Posted by OReubens View Post
    templatizing main() ? ? ?
    That's how we create templates I guess but I need a vector which supports all data types and can store them

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Vector of Templates Problem

    Quote Originally Posted by Fateslayer View Post
    ... I need a vector which supports all data types and can store them
    Then, please, explain why you need "a vector which supports all data types".
    What are these types?

    And, BTW, what linker errors did you get?
    Victor Nijegorodov

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Vector of Templates Problem

    Quote Originally Posted by Fateslayer View Post
    That's how we create templates I guess but I need a vector which supports all data types and can store them
    main() cannot be used with a template - hence the errors!

    but you still haven't explained why and what you are trying to achieve.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    Join Date
    May 2015
    Posts
    10

    Re: Vector of Templates Problem

    Quote Originally Posted by 2kaud View Post
    main() cannot be used with a template - hence the errors!

    but you still haven't explained why and what you are trying to achieve.
    I think it's okay to use templates with main() function. I don't know if there is any limitations on where I can use it.
    Anyways I'm trying to read a mathematical expression into a vector. First I take the expression into a string for example
    (5+5)*9
    As you can see there are two data types in this expression the first is double or int for numbers and second is char for operators like +, -, %, (,/ etc. Copying the string into vector is not a problem nor converting the string numbers into double. The problem is vector can only hold data of same type hence I was trying to use templates with vector so that I can use it to store both numbers and characters. That's all I wanna do to store elements of different data types into single vector

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Vector of Templates Problem

    What you are trying to do is parse an expression and then evaluate it. There are various ways of doing this in c++ but none require a vector that holds different types.

    Have a look at
    http://stackoverflow.com/questions/6...pressions-in-c
    http://stackoverflow.com/questions/1...ression-in-c-c
    http://stackoverflow.com/questions/1...ression-parser
    http://www.speqmath.com/tutorials/ex...cpp/index.html

    and associated links

    There are also numerous other internet sites that explain expression parsing and evaluation - just do a search.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    Join Date
    May 2015
    Posts
    10

    Re: Vector of Templates Problem

    Quote Originally Posted by 2kaud View Post
    What you are trying to do is parse an expression and then evaluate it. There are various ways of doing this in c++ but none require a vector that holds different types.

    Have a look at
    http://stackoverflow.com/questions/6...pressions-in-c
    http://stackoverflow.com/questions/1...ression-in-c-c
    http://stackoverflow.com/questions/1...ression-parser
    http://www.speqmath.com/tutorials/ex...cpp/index.html

    and associated links

    There are also numerous other internet sites that explain expression parsing and evaluation - just do a search.
    Thank you so much. I really appreciate it so that means vector which can hold any data type will maybe come in the future versions of c++.

Page 1 of 2 12 LastLast

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