[RESOLVED] how can i convert write() procedure to cout? - Page 4
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 4 of 4 FirstFirst 1234
Results 46 to 59 of 59

Thread: [RESOLVED] how can i convert write() procedure to cout?

  1. #46
    Join Date
    Apr 1999
    Posts
    27,423

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

    Quote Originally Posted by Cambalinho View Post
    (the error stills be when i use these function)?
    What is the exact error?

    Second, the cout is overloaded to take a null-terminated char*. Why not write a simple program to show this?
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       char *p = "Hello World";
       cout << p;
    }
    Code:
    Output:
    Hello World
    So what are you trying to achieve? The streams are overloaded already for char*, and if <string> is included, for std::string and std::wstring.

    Regards,

    Paul McKenzie

  2. #47
    Join Date
    Apr 2009
    Posts
    605

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

    Quote Originally Posted by Paul McKenzie View Post
    What is the exact error?

    Second, the cout is overloaded to take a null-terminated char*. Why not write a simple program to show this?
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       char *p = "Hello World";
       cout << p;
    }
    Code:
    Output:
    Hello World
    So what are you trying to achieve? The streams are overloaded already for char*, and if <string> is included, for std::string and std::wstring.

    Regards,

    Paul McKenzie
    my objective is my function write() accept '+' for concat the literal strings\variables(directly in function). like these(without any function adicional):

    Code:
    Console a;
    a.write("hello " + "world");
    and

    Code:
    Console a;
    string b="world";
    a.write("hello " + b);
    like you see, i don't use any function inside of my function
    is my objective.
    the error message that i recive:
    "C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|10|error: invalid operands of types 'const char [7]' and 'const char [6]' to binary 'operator+'|"
    (you see the '7' and '6', i know that depends in char array size(the '\0' is ignored in size))
    Last edited by Cambalinho; September 10th, 2013 at 02:10 PM.

  3. #48
    Join Date
    Apr 1999
    Posts
    27,423

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

    Quote Originally Posted by Cambalinho View Post
    my objective is my function write() accept '+' for concat the literal strings\variables(directly in function). like these(without any function adicional):
    Good luck, since no one else in the C++ world has done this.

    There is a reason why you cannot concatentate string literals -- it is because they are pointers. You cannot take two string literals and concatenate them with "+".

    The "+" when it sees a pointer on the left and right hand side does what it always did, even back in the days of K&R C. It takes the pointer value and adds them up, giving a pointer value.

    Again, if you need proof, forget about all of the C++ 11 syntax and write a simple C++ program that tries to do what you say you want to do. You will easily see that it is impossible.
    Code:
    #include <string>
    
    void foo(std::string& s)
    {
    }
    
    int main()
    {
       foo("hello" + "world");
    }
    So, go ahead and try and make the call to foo() work.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 10th, 2013 at 02:13 PM.

  4. #49
    Join Date
    Apr 2009
    Posts
    605

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

    Quote Originally Posted by Paul McKenzie View Post
    Good luck, since no one else in the C++ world has done this.

    There is a reason why you cannot concatentate string literals -- it is because they are pointers. You cannot take two string literals and concatenate them with "+".

    The "+" when it sees a pointer on the left and right hand side does what it always did, even back in the days of K&R C. It takes the pointer value and adds them up, giving a pointer value.

    Again, if you need proof, forget about all of the C++ 11 syntax and write a simple C++ program that tries to do what you say you want to do. You will easily see that it is impossible.

    Regards,

    Paul McKenzie
    thanks for the information

  5. #50
    Join Date
    Apr 1999
    Posts
    27,423

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

    Quote Originally Posted by Cambalinho View Post
    my objective is my function write() accept '+' for concat the literal strings\variables(directly in function). like these(without any function adicional):
    Also, if this is for your custom "language" that you say you are trying to write, then again, trying to get C++ to look like your custom language is not possible in these cases.

    The binary + operator cannot be overloaded if two pointers exist on both sides of the operator, plain and simple.

    Regards,

    Paul McKenzie

  6. #51
    Join Date
    Apr 2009
    Posts
    605

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

    Quote Originally Posted by Paul McKenzie View Post
    Also, if this is for your custom "language" that you say you are trying to write, then again, trying to get C++ to look like your custom language is not possible in these cases.

    The binary + operator cannot be overloaded if two pointers exist on both sides of the operator, plain and simple.

    Regards,

    Paul McKenzie
    but i can convert:

    a.write("hello " + "world")

    for:

    a.write((string)"hello " + "world");

    thanks for all

  7. #52
    Join Date
    Apr 1999
    Posts
    27,423

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

    Quote Originally Posted by Cambalinho View Post
    but i can convert:

    a.write("hello " + "world")

    for:

    a.write((string)"hello " + "world");

    thanks for all
    That is because the item on the left-hand side of the + is no longer a string-literal. It is now a std::string.

    Regards,

    Paul McKenzie

  8. #53
    Join Date
    Apr 2009
    Posts
    605

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

    Quote Originally Posted by Paul McKenzie View Post
    That is because the item on the left-hand side of the + is no longer a string-literal. It is now a std::string.

    Regards,

    Paul McKenzie
    yah. but why i can't do these in my parameters list?

  9. #54
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,265

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

    why i can't do these:
    Code:
     template <typename A, typename ...B>
        void write(char *argHead, B... argTail2)
        {
    
            cout <<(string) argHead;
            write(argTail2...);
        }
    (the error stills be when i use these function)
    What happens if you change the write function definiton to
    Code:
     template <typename A, typename ...B>
        void write(const char *argHead, B... argTail2)
        {
            cout << argHead;
            write(argTail2...);
        }
    then doesn't
    Code:
    a.write((string("hello ") + "world").c_str());
    work?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #55
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,265

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

    If you use string like this for concatenation

    Code:
    a.write((string("hello ") + "world");
    then doesn't your original write template function work without overloading the function template for the case of const char * or char*?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  11. #56
    Join Date
    Apr 2009
    Posts
    605

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

    Quote Originally Posted by 2kaud View Post
    What happens if you change the write function definiton to
    Code:
     template <typename A, typename ...B>
        void write(const char *argHead, B... argTail2)
        {
            cout << argHead;
            write(argTail2...);
        }
    then doesn't
    Code:
    a.write((string("hello ") + "world").c_str());
    work?
    sorry about my english
    (i understand that code works... but i mean anotherthing)
    see these way that i want's:
    Code:
    a.write("hello " + "world")
    like you see, i'm using the '+' operator for concat the char *. but the char * can't be concat directly that's why we must use casting or other functions for convert to string type. but can i change the parameter function for accept the '+' operator?

  12. #57
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,265

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

    You don't need to change the paramater function. Using the original definitions for write, then

    Code:
    a.write((string("hello ") + "world");
    should work as you are passing a type string which cout understands.

    Note that if you are using 2 or more literal strings, then
    "hello " + "world" can be written "hello ""world" which will be treated as "hello world" - but only for literal strings!

    For your 'language', it might be easier for you to always 'convert' c-style char * strings to type string. ie

    Code:
    a.write((string("hello ") + string("world"));
    Last edited by 2kaud; September 10th, 2013 at 04:27 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  13. #58
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,265

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

    Why bother with concatenation at all with your write template function? why not just do

    Code:
    a.write("Hello ", "world");
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #59
    Join Date
    Apr 2009
    Posts
    605

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

    Quote Originally Posted by 2kaud View Post
    Why bother with concatenation at all with your write template function? why not just do

    Code:
    a.write("Hello ", "world");
    you have right. thanks to all. thanks for all.. thanks

Page 4 of 4 FirstFirst 1234

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center