CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 59
  1. #16
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by 2kaud View Post
    Try something like this. Note that variadic templates in c++11 are not yet implemented in MSVS2010 or 2012.

    Code:
    #include <iostream>
    using namespace std;
    
    void write()
    {
       cout << " ";
    }
    
    template <typename A, typename ...B>
    void write(A argHead, B... argTail)
    {
       cout << argHead << " ";
       write(argTail...);
    }
    
    
    int main(void)
    {
       write(1, 2, "qwerty", 4.45, "five", 6);
       return 0;
    }
    sorry, but i get more than 2 errors:
    1 - "error C2143: syntax error : missing ',' before '...' ";
    2 - "error C2061: syntax error : identifier 'B'"

    i use that library and the namespace. so what is wrong?

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

    Re: how can i convert write() procedure to cout?

    What compiler are you using - and does it support c++11 variadic templates? MSVS doesn't yet support these.
    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)

  3. #18
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by 2kaud View Post
    What compiler are you using - and does it support c++11 variadic templates? MSVS doesn't yet support these.
    i'm using Visual Studio 2010

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

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by Cambalinho View Post
    i'm using Visual Studio 2010
    Then thats why you get the compile errors. At present neither Visual Studio 2010 nor Visual Studio 2012 supports variadic templates.
    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. #20
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by 2kaud View Post
    Then thats why you get the compile errors. At present neither Visual Studio 2010 nor Visual Studio 2012 supports variadic templates.
    is there another way?

  6. #21
    Join Date
    Apr 1999
    Posts
    27,449

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by Cambalinho View Post
    that tutorial is limited, the 1st argument is the master and tell us, indirectly, how many arguments received
    It has nothing to do with the tutorial. That is how C++ works.

    The receiving function has no idea where your argument list stops when you use a varying number of arguments. So you have to tell that function in some way where or when the last parameter is reached.

    There are 3 general ways to specify where/when the last argument is reached.

    1) A format list or some sort of string telling the function how to parse the rest of the arguments. (printf() works this way)
    2) Some number stating the number of arguments that follow in the list (the examples you have work this way)
    3) Some "sentinel" or ending argument, signifying the end of the argument list (functions such as execvp() work this way).

    There may be other ways, but these are the first three that come to mind. But in no way can you just write a function that takes a varying number of arguments, not tell the receiving function anything, and then by magic the receiving function knows how many parameters you passed. At least not in C++ before C++ 11.
    i need use the write() like these:
    write(var1, var2, "hello world")
    the order i don't care, but i don't use strings or arguments for tell me the number of arguments.
    Then use another language, since C++ (again, before C++11) doesn't have this functionality.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 19th, 2013 at 02:23 PM.

  7. #22
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by Paul McKenzie View Post
    It has nothing to do with the tutorial. That is how C++ works.

    The receiving function has no idea where your argument list stops when you use a varying number of arguments. So you have to tell that function in some way where or when the last parameter is reached.

    There are 3 general ways to specify where/when the last argument is reached.

    1) A format list or some sort of string telling the function how to parse the rest of the arguments. (printf() works this way)
    2) Some number stating the number of arguments that follow in the list (the examples you have work this way)
    3) Some "sentinel" or ending argument, signifying the end of the argument list (functions such as execvp() work this way).

    There may be other ways, but these are the first three that come to mind. But in no way can you just write a function that takes a varying number of arguments, not tell the receiving function anything, and then by magic the receiving function knows how many parameters you passed. At least not in C++.
    Then use another language, since C++ doesn't have this functionality.

    Regards,

    Paul McKenzie
    hey the C\C++ is the most powerfull language(i think)

  8. #23
    Join Date
    Apr 1999
    Posts
    27,449

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by Cambalinho View Post
    hey the C\C++ is the most powerfull language(i think)
    It is powerful, but note that you can't find a function that does what you say you want it to do (at least not with C++ 98 or 03). Why do you think that is?

    Regards,

    Paul McKenzie

  9. #24
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by Paul McKenzie View Post
    It is powerful, but note that you can't find a function that does what you say you want it to do (at least not with C++ 98 or 03). Why do you think that is?

    Regards,

    Paul McKenzie
    i'm build these class for be more easy build my own programming language
    that's why i wanted 1 funtion in that way, but seems that i must use overloading functions for the number of arguments
    thanks for all

  10. #25
    Join Date
    Apr 1999
    Posts
    27,449

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by Cambalinho View Post
    i'm build these class for be more easy build my own programming language
    that's why i wanted 1 funtion in that way, but seems that i must use overloading functions for the number of arguments
    thanks for all
    The functions not only need to be overloaded, but they also need to be template functions.
    Code:
    template <typename arg1>
    void write(arg1 a1)
    {
    //...
    }
    
    template <typename arg1, typename arg2>
    void write(arg1 a1, arg2 a2)
    {
    //...
    }
    Regards,

    Paul McKenzie

  11. #26
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by Cambalinho
    i'm build these class for be more easy build my own programming language
    Hmm... I don't see how fiddling with C++ syntax is going to make it easier for you to write a compiler/interpreter for your own programming language. Maybe you want your compiler to output C++ code that is then compiled by a C++ compiler, but between this (your preferred syntax):
    Code:
    write(var1, var2, "hello world")
    and this (standard C++ formatted output for output stream syntax):
    Code:
    std::cout << var1 << var2 << "hello world"
    and this (example syntax of function call chaining):
    Code:
    write(var1).write(var2).write("hello world")
    I doubt you will face a problem with writing your compiler to output them either way, assuming you have the skill to implement for at least one of them. So, if my guess that you intend to use C++ as the output of your compiler is correct, then I suggest just using the standard formatted output.
    Last edited by laserlight; August 19th, 2013 at 10:29 PM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #27
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by laserlight View Post
    Hmm... I don't see how fiddling with C++ syntax is going to make it easier for you to write a compiler/interpreter for your own programming language. Maybe you want your compiler to output C++ code that is then compiled by a C++ compiler, but between this (your preferred syntax):
    Code:
    write(var1, var2, "hello world")
    and this (standard C++ formatted output for output stream syntax):
    Code:
    std::cout << var1 << var2 << "hello world"
    and this (example syntax of function call chaining):
    Code:
    write(var1).write(var2).write("hello world")
    I doubt you will face a problem with writing your compiler to output them either way, assuming you have the skill to implement for at least one of them. So, if my guess that you intend to use C++ as the output of your compiler is correct, then I suggest just using the standard formatted output.
    but:
    1 - how use 'Write' word instead 'cout'?;
    2 - can i add it to my class?

    (i just convert my new language to C++ and then i call the C++ compiler(it's free... well the Dev C++)

  13. #28
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by Cambalinho
    i just convert my new language to C++ and then i call the C++ compiler
    Yeah, that's what I meant by: "Maybe you want your compiler to output C++ code that is then compiled by a C++ compiler"

    Quote Originally Posted by Cambalinho
    but:
    1 - how use 'Write' word instead 'cout'?;
    2 - can i add it to my class?
    I'm saying: don't do that since it does not gain you anything. Your compiler can just as easily output the "cout version" as it can this "Write version" that you have in mind.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #29
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by laserlight View Post
    Yeah, that's what I meant by: "Maybe you want your compiler to output C++ code that is then compiled by a C++ compiler"


    I'm saying: don't do that since it does not gain you anything. Your compiler can just as easily output the "cout version" as it can this "Write version" that you have in mind.
    so instead include the Read()\Write in console class, i make them part of my own language(keywords\functions) is what you are advice me?

  15. #30
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by Cambalinho
    so instead include the Read()\Write in console class, i make them part of my own language(keywords\functions) is what you are advice me?
    It depends. I have no clue what is the syntax and semantics of this programming language that you are trying to design and implement. What I do know is that whatever the syntax, you can translate a write to standard output in that language to C++ code involving std::cout, without having to write some C++ function (template, or class) with the C++ syntax that you have in mind. In other words, concentrating on C++ seems like it is taking you off course. Rather, concentrate on the syntax and semantics of your language, and how you will be translating that into C++ without unnecessary frills.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Page 2 of 4 FirstFirst 1234 LastLast

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