CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2008
    Posts
    41

    invalid operands of types 'const char[13]' and 'char*' to binary 'operator+'

    I don't understand where the const char is coming from.
    As mentioned in the title, the error I'm getting is " invalid operands of types 'const char[13]' and 'char*' to binary 'operator+'".

    Am I using strcat incorrectly?
    (BTW I'm barely starting to learn C++, so I'm sure it's not as well written as it could be... just trying to understand the fundamentals)
    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    #include <sstream>
    #include <convert.h> //custom header I made, enables me to use 
    //"string mystring = stringify(double)" 
    
    char chQuit = 'n';
    using namespace std;
    
    
    char chDir;
    char* chNewLength;
    char* chCurrentLength;
    char* chLengthWhole;
    char* chUnitSingular;
    double doLength;
    double doResult;
    double doResultWhole = '0';
    string strResultWhole;
    
    //Converts Inches to Centimeters
    double fromInches (double Length) {
     double doReturn;
     doReturn = ((Length) * 2.54);
     chCurrentLength="Inches";
    chNewLength="Centimeters";
    chLengthWhole=" Meters";
    chUnitSingular="Meter";
    if (doReturn >= 100) {
    doResultWhole = (doReturn / 100);
    }
    else {doResultWhole = 0;
    }
     return (doReturn);
    }
    
    //Converts Centimeters to Inches
    double fromCentimeters (double Length) {
     double doReturn;
     doReturn = ((Length) / 2.54);
     chCurrentLength="Centimeters";
    chNewLength="Inches";
    chLengthWhole=" Feet";
    chUnitSingular="Foot";
    if (doReturn >=12) {
    doResultWhole = (doReturn / 12);
    }
    else { doResultWhole = 0;
    }
     return (doReturn);
    }
    int main(){
    
    chQuit = 'n';
    do{
    chQuit = 'n';
    system("cls");
    
    cout << "Are you converting FROM (I)nches or (C)entimeters?" <<endl;
    cout << "(I/C): ";
    cin >> chDir;
    
    if (chDir == 'I' || chDir == 'i') {
        chCurrentLength="Inches";
    }
    else if (chDir == 'C' || chDir == 'c') {
        chCurrentLength="Centimeters";
    }
    
    cout << "Enter length in " << chCurrentLength << endl;
    cin >> doLength;
    if (doResultWhole !=0) {
    strResultWhole = (stringify(doResultWhole));
    }
    
    
    
    
    
    else {strResultWhole= strchr("Less than a " + chUnitSingular);}
    //Problem is here ^
    
    
    
    
    if (chDir == 'I' || chDir == 'i') {
        cout << fromInches(doLength) << endl << "(" << strResultWhole << ")" << endl;
    }
    else if (chDir == 'C' || chDir == 'c') {
        cout << fromCentimeters(doLength) << endl << "(" << strResultWhole << ")" << endl;
    
    }
    cout << "Do you want to convert another length?" << endl;
    cout << "(Y/N)";
    cin >> ::chQuit;}
    while (chQuit == 'Y' || chQuit == 'y');
    return 0;
    }

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

    Re: invalid operands of types 'const char[13]' and 'char*' to binary 'operator+'

    Quote Originally Posted by SimbaSpirit
    I don't understand where the const char is coming from.
    As mentioned in the title, the error I'm getting is " invalid operands of types 'const char[13]' and 'char*' to binary 'operator+'".
    Code:
    strchr("Less than a " + chUnitSingular);}
    What are you intending to do with that + sign? Concatenation? Well, using raw C-style strings has no such thing.
    Am I using strcat incorrectly?
    Why are you learning strcat() if you're using C++? You should use std::string, as low-level C-style strings are not to be used unless you have a compelling reason to use them.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Sep 2008
    Posts
    41

    Re: invalid operands of types 'const char[13]' and 'char*' to binary 'operator+'

    Eh, I saw it in a forum... what should I put in its place? I tried using
    string = ("text" + string) and I got a similar error.

  4. #4
    Join Date
    Jul 2008
    Posts
    70

    Re: invalid operands of types 'const char[13]' and 'char*' to binary 'operator+'

    if you use std::string then you can use the + operator to concatenate strings. If you use char* or char[] you cannot.

    I recommend you start small and read up on std::string. Here is a simple example to get you started

    Code:
    #include <string>
    #include <iostream>
    int main() {
      std::string foo("hello"); 
      std::string bar("world");
    
      std::cout << foo << " " << bar << std::endl;
      std::cout << foo + " " + bar << std::endl;
      // last line doesn't work, do you know why now? Hint its not a std::string
      //std::cout << "Hello" + " " + "World" << std::endl
    }
    Hope that helps...

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

    Re: invalid operands of types 'const char[13]' and 'char*' to binary 'operator+'

    Quote Originally Posted by SimbaSpirit
    Eh, I saw it in a forum...
    Forums and websites are not the proper way to learn C++, as many of them are full of mistakes, errors, bad coding habits, etc. You need to read good books on C++ to get properly grounded in the language.

    For example, you included <string>, and that is not the correct header. The correct header is <cstring> for C-style strings, not <string>. As a matter of fact, <string> is the correct header to use if you're using std::string. So you've got everything backwards.
    . what should I put in its place?
    Start by rewriting that entire program to use std::string and leave the C-style strings by the wayside until there is a compelling and justifiable reason to use them.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Sep 2008
    Posts
    41

    Re: invalid operands of types 'const char[13]' and 'char*' to binary 'operator+'

    So you've got everything backwards.
    I wouldn't say I have everything backwards so much as I used the wrong command lol


    Anyways... thanks for the help

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