Thread: string type troubles

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    string type troubles

    I have been trying to get the following test program to at least compile and find out where I'm getting my memory addressing problems wrong in the main program. Here is my code:
    Code:
    #include <iostream>
    #include <windows.h>
    
    BOOL somefunc(const WCHAR* filepath,
    			  const wstring* Additions,
    			  DWORD AdditionsSize);
    
    int main()
    {
    	const wstring additions[] = {L"black.dll",
    		L"white.dll",
    		L"yellow.dll"};
    	BOOL ok = somefunc(L"E:\\somepath", additions, sizeof(additions)/sizeof(wstring));
    
    	return 0;
    }
    
    BOOL somefunc(const WCHAR* filepath,
    			  const wstring* Additions,
    			  DWORD AdditionsSize)
    {
    	for (DWORD i = 0; i < AdditionsSize; ++i)
    	{
    		wcout << Additions[i] << endl;
    	}
    
    	return TRUE;
    }
    However, I'm getting the following compiler error with regards to the types in Visual Studio 2005:
    Code:
    1>------ Build started: Project: ptrtoptr, Configuration: Debug Win32 ------
    1>Compiling...
    1>ptrtoptrmain.cpp
    1>e:\test\ptrtoptr\ptrtoptrmain.cpp(5) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>e:\test\ptrtoptr\ptrtoptrmain.cpp(5) : error C2146: syntax error : missing ',' before identifier 'Additions'
    1>e:\test\ptrtoptr\ptrtoptrmain.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>e:\test\ptrtoptr\ptrtoptrmain.cpp(10) : error C2146: syntax error : missing ';' before identifier 'additions'
    1>e:\test\ptrtoptr\ptrtoptrmain.cpp(10) : error C2065: 'additions' : undeclared identifier
    1>e:\test\ptrtoptr\ptrtoptrmain.cpp(10) : error C2059: syntax error : ']'
    1>e:\test\ptrtoptr\ptrtoptrmain.cpp(10) : error C2143: syntax error : missing ';' before '{'
    1>e:\test\ptrtoptr\ptrtoptrmain.cpp(12) : error C2143: syntax error : missing ';' before '}'
    1>e:\test\ptrtoptr\ptrtoptrmain.cpp(13) : error C2070: ''unknown-type'': illegal sizeof operand
    1>e:\test\ptrtoptr\ptrtoptrmain.cpp(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>e:\test\ptrtoptr\ptrtoptrmain.cpp(19) : error C2146: syntax error : missing ',' before identifier 'Additions'
    1>e:\test\ptrtoptr\ptrtoptrmain.cpp(22) : error C2065: 'AdditionsSize' : undeclared identifier
    1>e:\test\ptrtoptr\ptrtoptrmain.cpp(24) : error C2065: 'wcout' : undeclared identifier
    1>e:\test\ptrtoptr\ptrtoptrmain.cpp(24) : error C2065: 'Additions' : undeclared identifier
    1>e:\test\ptrtoptr\ptrtoptrmain.cpp(24) : error C2065: 'endl' : undeclared identifier
    1>Build log was saved at "file://e:\test\ptrtoptr\Debug\BuildLog.htm"
    1>ptrtoptr - 15 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Is there a header file that I have missed that could cause this to happen?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by stanlvw View Post
    Is there a header file that I have missed that could cause this to happen?
    Yes: #include <string>
    You also need to either prefix wstring, wcout & endl with std:: or add a using statement.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Thanks for the tip. I've actually found this out right after my post (guess my head wasn't thinking properly). However, I am now trying to perform pointer manipulation, which is pointing to an array like so:
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include <windows.h>
    using namespace std;
    
    BOOL somefunc(const WCHAR* filepath,
    			  const wstring* Additions,
    			  DWORD AdditionsSize);
    
    int main()
    {
    	const wstring newlist[] = {L"black.dll",
    		L"white.dll",
    		L"yellow.dll"};
    	BOOL ok = somefunc(L"E:\\somepath", newlist, sizeof(newlist)/sizeof(wstring));
    
    	return 0;
    }
    
    BOOL somefunc(const WCHAR* filepath,
    			  const wstring* Additions,
    			  DWORD AdditionsSize)
    {
    	wstring result;
    	wstring* someOtherPath = NULL;
    
    	for (DWORD i = 0; i < AdditionsSize; ++i)
    	{
    		someOtherPath[i] = filepath + Additions[i];
    		wcout << someOtherPath[i] << endl;
    	}
    
    	return TRUE;
    }
    However, I'm either getting a compiler error saying I can't add pointers together, or I'm getting a runtime error on the line where I'm attempting the addition. Is there an easy way to get around this?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    	wstring* someOtherPath = NULL;
    
    	for (DWORD i = 0; i < AdditionsSize; ++i)
    	{
    		someOtherPath[i] = filepath + Additions[i];
    What are you trying to do?
    Adding two pointers is illegal and your code wouldn't work right if it was allowed, because it isn't merging the two strings at all, but rather trying to add their address.
    Use std::string objects instead and pass by reference. Otherwise you're going to have to reply on C string copy functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I'm trying to concatenate the result of filepath and current entry pointed by Additions and store it in someOtherPath. When Elysia said "use std::string" objects and pass by reference, I don't quite understand what was being said. When I pass the array through the function, to my understanding, I'm already passing by reference. And wstring is a typedef of a string object.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by stanlvw View Post
    I'm trying to concatenate the result of filepath and current entry pointed by Additions and store it in someOtherPath.
    Pointers aren't magical, you know
    You must allocate storage first if you wish to add something into it.

    When Elysia said "use std::string" objects and pass by reference, I don't quite understand what was being said. When I pass the array through the function, to my understanding, I'm already passing by reference.
    Code:
    void foo1(int) { } // By value
    void foo2(int*) { } // By pointer
    void foo3(int&) { } // By reference
    You're passing the address to the string itself and not the actual object:

    Code:
    int main()
    {
        std::string mystr = "My string";
        const char* mystr2 = "My string";
        foo(mystr); // Passing object by reference (or value)
        foo(mystr2); // Passing address of string by pointer (by value, actually)
    }
    And wstring is a typedef of a string object.
    Yes, but you should probably make your new string of type std::wstring, as well, so you concatenate the strings there.
    Last edited by Elysia; 06-04-2008 at 12:09 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I've actually managed to get around the problem minimizing the use of an extra pointer now, and it seems to be working quite well without problems (at least right now). Thanks for your help.

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    There's something that I'm having trouble with in terms of cleaning up my code. I'm trying to define a macro as shown to be used in the concatenation process:
    Code:
    #define SLASH			TEXT("\\")
    and this is the concatenation that I tried to do here:
    Code:
    someOtherPath = filepath + SLASH + Additions[i];
    someOtherPath is wstring, filepath is const WCHAR*, and Additions[i] is wstring from having Additions passed in as const wstring*. I'm trying to concatenate slash in the code, but I can't think of anyway of doing so without obtaining a wstring copy of SLASH. Would appending the text be the way to go?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Either you do:
    Code:
    someOtherPath = filepath + std::wstring(SLASH) + Additions[i];
    Or
    Code:
    someOtherPath += filepath;
    someOtherPath += SLASH;
    someOtherPath += Additions[i];
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I believe for the + operator to work, the left-most variable being added (i.e. filepath) needs to be a std::wstring.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unless there's a free operator + which takes const wchar_t* and std::wstring.
    I don't know it this is the case, however.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I don't know enough about C++ to give a definite answer, but the following compiles on GCC. Only the commented-out line produced an error for me.
    Code:
    #include <string>
    
    int main() {
        std::string r, s = "s";
        const char *c = "c";
    
        r = s + c;
        r = c + s;
        r = s + c + s;
        r = c + s + c;
        r = s + c + c;
        //r = c + c + s;
    
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They all work fine, as well.
    The last line shouldn't work, as we know because the same problem exists in C (you can't add two pointers!).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Umm . . . what?

    You can add and subtract pointers in both C and C++.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        const char *start = "what";
        const char *end = strchr(start, 'a');
    
        printf("The 'a' in \"&#37;s\" is at index %i\n",
            start, (int)(end - start));
    
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What Elysia meant, but did not say, was that you cannot add pointers at all, and can only subtract pointers when they both point to the same array (or one past the end of the array).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM