String Addition Problem...

This is a discussion on String Addition Problem... within the C++ Programming forums, part of the General Programming Boards category; I'm trying to add two variables (both strings) in Dev-C++, and it tells me that there's no matching operator+. Every ...

  1. #1
    Marxist-Trotskyist
    Join Date
    May 2005
    Location
    Many, many miles from the center of the Earth.
    Posts
    31

    String Addition Problem...

    I'm trying to add two variables (both strings) in Dev-C++, and it tells me that there's no matching operator+. Every tutorial and example I've found on the net says that the syntax I'm using is just fine, so I don't understand why I'm getting this error. If anyone could help, that would be great. Thanks in advance. Here's the code, rather short. The declarations have values other than NULL, but I didn't think it was particularly important.

    Code:
    //DECLARED AT THE GLOBAL SCOPE
    string MOD_DIR = NULL;
    
    //DECLARE AT THE FUNCTIONAL SCOPE
    string filename = NULL;
    int config_Type
    
    switch(config_Type)
    {
        case 1:
            filename = MOD_DIR + filename + ".ini";
            break;
    }
    If a=b, and b=c, then a=c, except where void and prohibited by law...

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    #include <iostream>
    #include <string>
    int main()
    {
    	std::string buf("RETR ");
    	std::string res("test");
    	std::string res2("test2");
    	std::string res3 = buf + res + res2;
    	std::cout << res3;
    	return 0;
    }
    /*
    My output
    RETP testtest2
    */
    so I think the problem lies somethere in the initialization or declaration, not in the + itself
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Marxist-Trotskyist
    Join Date
    May 2005
    Location
    Many, many miles from the center of the Earth.
    Posts
    31
    Thanks for the reply. I was wondering if it might have anything do with something preceding it in the code, but everything else seems to work fine (I took out the prior functions, one by one, to see if they were causing the error... To no avail, of course). Here's the context, if that's any help. Thanks again...

    Code:
    string MOD_DIR = NULL;
    
    //GENERIC CONFIG LOADING FUNCTION
    bool Load_ConfigFile(int config_Type, string *filename, string entry_Sect, 
                         int propmax)
    {
        //TEMP PROP ARRAY
        string *prop_Array[propmax];
        
        //PROPERLY FORMAT FILENAME (DEPENDENT ON config_Type)
        switch(config_Type)
        {
            case 1:
                filename = MOD_DIR + filename + ".ini";
                break;
            case 2:
                filename = MOD_DIR + filename + "_Ulist.ini";
                break;
            case 3:
                filename = MOD_DIR + "\GFX\UNITS" + filename + ".ini";
                break;
            case 4:
                filename = MOD_DIR + "\GFX\STRUCTS" + filename + ".ini";
                break;
        }
    
    ...
    
    }
    If a=b, and b=c, then a=c, except where void and prohibited by law...

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    filename is string* not string
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Marxist-Trotskyist
    Join Date
    May 2005
    Location
    Many, many miles from the center of the Earth.
    Posts
    31
    Now I'm confused... I thought that you needed that to point to the data held by the variable passed to it...? Sorry, I natively program on Pascal and BASIC, kinda' new to C++. Thanks, by the way. *feels somewhat retarded*
    If a=b, and b=c, then a=c, except where void and prohibited by law...

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Now I'm confused
    See if this helps:
    Code:
    string filename = "myFile";
    
    string* ptr = &filename;
    cout<<ptr<<endl;  //006BFDDC (address in memory)
    cout<<*ptr<<endl;  //myFile
    
    *ptr = *ptr + ".txt";
    cout<<*ptr<<endl; //myFile.txt
    
    cout<<filename<<endl;  //myFile.txt
    Pointer variables store addresses, which all look something like this: 006BFDDC. Pointer variables do not store strings, ints, or doubles.
    Last edited by 7stud; 01-12-2007 at 03:31 AM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> string MOD_DIR = NULL;
    Don't assign NULL to a string. If you want the string to be empty, just don't initialize it, it will default to an empty string:
    Code:
    string MOD_DIR;

  8. #8
    Marxist-Trotskyist
    Join Date
    May 2005
    Location
    Many, many miles from the center of the Earth.
    Posts
    31
    TO 7stud:

    Ahh... I always get * and & confused. If I had passed a pointer, and wanted to refer to the data at the address held by the pointer, I would use &, right? Oh, and just removing the pointer declaration fixed it... I knew it was something obvious.

    TO Daved:

    Thanks for the tip. Just curious, what's the err in giving it the NULL value?
    If a=b, and b=c, then a=c, except where void and prohibited by law...

  9. #9
    Marxist-Trotskyist
    Join Date
    May 2005
    Location
    Many, many miles from the center of the Earth.
    Posts
    31
    P.S. Thanks to everyone for the replies, despite my utter blond-ness. '^.^
    If a=b, and b=c, then a=c, except where void and prohibited by law...

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    If I had passed a pointer, and wanted to refer to the data at the address held by the pointer, I would use &, right?
    No. See the example I posted:
    Code:
    string filename = "myFile";
    string* ptr = &filename;
    
    cout<<ptr<<endl;  //006BFDDC (address in memory)
    cout<<*ptr<<endl;  //myFile
    When you dereference a pointer with *, you get the value stored at that address. On the other hand, & is the address-of operator:

    &("and") ---> "address of"

    The second line of code uses the address-of operator to obtain the address of 'filename' and store it in the pointer variable:

    string* ptr = &filename;

    That line can be read as: "ptr equals the address-of filename".
    Last edited by 7stud; 01-12-2007 at 04:46 PM.

  11. #11
    Marxist-Trotskyist
    Join Date
    May 2005
    Location
    Many, many miles from the center of the Earth.
    Posts
    31
    Oh, ok, I see. When you dereference a pointer, you get the data at the address held by the pointer (done by using *). When you use &, you directly refer to the address, which is stored in the pointer... Sorry, never been good with pointers.
    If a=b, and b=c, then a=c, except where void and prohibited by law...

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >bool Load_ConfigFile(int config_Type, string *filename, string entry_Sect,
    int propmax)
    You probably want to pass filename as a reference:
    Code:
    bool Load_ConfigFile(int config_Type, string &filename, string entry_Sect, 
                         int propmax)
    You could use a pointer (as in your original code), but then you'd have to write:
    Code:
                *filename = MOD_DIR + *filename + ".ini";
    >Just curious, what's the err in giving it the NULL value?
    NULL is used to initialize pointers. This isn't a pointer:
    Code:
    string MOD_DIR = NULL;
    You could write:
    Code:
    string MOD_DIR = "";
    But as Daved said, this would be the same as:
    Code:
    string MOD_DIR;

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    However, "real" C++ programmers don't initialize pointers to NULL anyway, they use:

    string* ptr = 0;

  14. #14
    Marxist-Trotskyist
    Join Date
    May 2005
    Location
    Many, many miles from the center of the Earth.
    Posts
    31
    Thanks. I think I understand pointers more now... Specifically referencing and such. Curious, again, if you passed an array to a pointer declared in a function, and then called a member of an array using the pointer as the array name, would it call the array passed to the function initially? Like...

    Code:
    int array[10];
    
    void a_function(int *passed_array)
    {
        passed_array[5] = 1;
        return;
    }
    Would that put 1 at array[5]? Or does that not make any sense?
    If a=b, and b=c, then a=c, except where void and prohibited by law...

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    What happens when you try it?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21