Thread: problem in mystring class

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    problem in mystring class

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    using namespace std;
    
    class mystring : public string
    {
    public:
    	mystring& lower_case(mystring& str=*this)
    	{
    		for (int i=0; i<str.size(); i++)
    		{
    			if (isupper(str[i]))
    			{
    				str[i] = tolower(str[i]);
    			}
    		}
    		return str;
    	}
    	
    	mystring& upper_case(mystring& str=*this)
    	{
    		for (int i=0; i<str.size(); i++)
    		{
    			if (islower(str[i]))
    			{
    				str[i] = toupper(str[i]);
    			}
    		}
    		return str;
    	}
    };
    
    int main()
    {
    	mystring str;
    	while (true)
    	{
    		cout << "String: ";
    		getline(cin, str, '\n');
    		if (str == "$") break;
    		cout << "Upper case: " << str.upper_case(str) << endl;
    		cout << "Lower case: " << str.lower_case(str) << endl;
    	}
    	return 0;
    }
    if i give the value of *this as a default argument, i get following error on MSVC .Net 2003:
    Code:
    C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\ostream(574) : wa
    rning C4530: C++ exception handler used, but unwind semantics are not enabled. S
    pecify /EHsc
    C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\istream(828) : wa
    rning C4530: C++ exception handler used, but unwind semantics are not enabled. S
    pecify /EHsc
    C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\istream(1064) : w
    arning C4530: C++ exception handler used, but unwind semantics are not enabled.
    Specify /EHsc
    case.cpp(9) : error C2355: 'this' : can only be referenced inside non-static mem
    ber functions
    case.cpp(21) : error C2355: 'this' : can only be referenced inside non-static me
    mber functions

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I'm sure default arguments are handled at the callers side, thus making "*this" being dereferenced outside the method where there is no class object.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't use "*this" before you are INSIDE the function.

    What you could do is to use overloading, like this:
    Code:
    	mystring& lower_case(mystring& str)
    	{
    		for (int i=0; i<str.size(); i++)
    		{
    			if (isupper(str[i]))
    			{
    				str[i] = tolower(str[i]);
    			}
    		}
    		return str;
    	}
    
    	mystring& lower_case()  // No argument - use "this"
    	{
                    return lower_case(*this);
    	}

    Edit: as the mystring& lower_case(mystring &str) doesn't touch "this", you could make it static.
    --
    Mats
    Last edited by matsp; 04-10-2008 at 05:45 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Magos View Post
    I'm sure default arguments are handled at the callers side, thus making "*this" being dereferenced outside the method where there is no class object.
    Spot on, and good explanation - I never explained why you can't use "this" as a default argument value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I also see a big, fat warning that says exception handling is disabled in project settings... This is on by default, so you must have disabled it. Hopefully for a reason. Hopefully a good one.
    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.

  6. #6
    Banned
    Join Date
    Nov 2007
    Posts
    678
    No i did not do anything about exceptions at all!
    I just compiled the program from command line like this:
    cl case.cpp
    I mostly use only command line, and i also don't understand what are those warnings about.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do not compile using command lines.
    Compile within the IDE. There's a reason the IDE can automatically compile for you and it's convenient and it's not error-prone.
    Simply press F7 (if you've select C++ 6 settings).

    Your current code will break and fold in so many ways because you aren't compiling correctly.
    Again, use the IDE.

    I'm really allergic to compiling stuff via the command line. It shouldn't be necessary.
    Sheesh. You don't even know how to use the compiler properly and yet you compile using command-line interface.
    Last edited by Elysia; 04-10-2008 at 05:28 AM.
    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.

  8. #8
    Banned
    Join Date
    Nov 2007
    Posts
    678
    that makes me ask for a little advice from you please help me here:

    - i want MSVC to complete my symbol names, show completions etc. for all the files that are needed in my project.
    what is the easiest way to do it?
    - how do i compile my project using MSVC if i need to run a few custom tools over my source files and other files added to project.
    also those tools produce some source files which need to be compiled. but they are generated by the tools.
    how i set the MSVC to use these newly created files and compile them as well.

    i tried to read about custom build steps, but was at lost. may be you can help me. by explaining in some easier steps.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    ctrl-space gives you completion of the stuff you are typing. ctrl-shift-space (I think) gives the current parameters for function calls [etc].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manav View Post
    - i want MSVC to complete my symbol names, show completions etc. for all the files that are needed in my project.
    what is the easiest way to do it?
    It's typically called IntelliSense. MSVc's intellisense is a little poor, but doable. If you type a method, it will automatically show the parameters for those functions. If you need to force it to show it, mats listen the shortcuts. Typically, I just bind it to Alt+q, which is easier to use.
    And you could also try Visual Assist X for Visual Studio. Very, very nice intellisense and other stuff plugin. Not free, but 30-days trial. And if you can reset the trial, all the better

    - how do i compile my project using MSVC if i need to run a few custom tools over my source files and other files added to project.
    also those tools produce some source files which need to be compiled. but they are generated by the tools.
    how i set the MSVC to use these newly created files and compile them as well.
    To this end, use the Custom Build Step in the project settings. Pre-build is executed before the compiler is called. Pre-link is called before the linker is called and post-build is called after the linker is finished.
    If you need to compile extra source files, try to manually enter extra arguments to the compiler under the project settings.
    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.

  11. #11
    Banned
    Join Date
    Nov 2007
    Posts
    678
    those features work if i use MSVC for writing window specific code.
    but i am writing code which does not use any windows api directly, i am using eg. Qt, boost and other portable
    networking libraries. i also have their sources. now MSVC can find the definitions/declarations, can show completions, can do a lot of things.
    how do i make it to do all those nice things for my Qt programs?

    and if any body have done Qt they know that Qt source files need to run qmake do a little magic with the source files.
    from command line its as simple as this:
    qmake -project
    qmake -makefile
    nmake
    how do i perform only these steps for my project build?
    and also how do i make MSVC to do intellisense kinda stuff with Qt API?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manav View Post
    those features work if i use MSVC for writing window specific code.
    but i am writing code which does not use any windows api directly, i am using eg. Qt, boost and other portable
    networking libraries. i also have their sources. now MSVC can find the definitions/declarations, can show completions, can do a lot of things.
    how do i make it to do all those nice things for my Qt programs?

    and also how do i make MSVC to do intellisense kinda stuff with Qt API?
    It will automatically parse and detect any headers you've included. So it should work with Qt, as well as your own source. You can also try deleting the intellisense database (the *.ncb file) in the project directory to see if it helps. No extra configuration should be necessary.
    You can also try Visual Assist X.

    and if any body have done Qt they know that Qt source files need to run qmake do a little magic with the source files.
    from command line its as simple as this:
    qmake -project
    qmake -makefile
    nmake
    how do i perform only these steps for my project build?
    I have no idea what those command lines do or how they interact with your code, I can only guess. But Qt sounds like a pain. It shouldn't have to rely on command lines to build the darn thing. Just ship a little Dll or some other form of binary for the different platforms or provide instructions on how to create one such for each platform.

    Qmake -project I suppose makes some changes to your project. That's fine. You can call that in the pre-build step.
    Qmake -makefile makes a makefile for... qt only or your project? Does it create some library you need to link to?
    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.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Isn't it normally advised against inheriting from standard containers (due to the lack of virtual destructor) and wouldn't that also apply to std::string?

    In this case, particularly since you apparently want to use the member function to provide the ability to convert other strings as well, wouldn't a much better approach be to provide a free function to_lower and to_upper for std::string (or download boost and use the ones that are already provided there)?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Banned
    Join Date
    Nov 2007
    Posts
    678
    so just including the headers and specifying the include path in settings will work? i will try and post later.

    about the qmake steps:
    well they just make everything ready so just doing an nmake will make my programs.
    how do i configure MSVC to just run nmake or use my makefile generated by qmake?

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh and now that you mention it...
    If the tolower/toupper functions are part of your class, they should perform the action on the object itself. When I call myobj.toupper(), I expect it to transform (or return a new object) with upper case.
    If you want it to be able to mainpulate another object, making it static.

    Quote Originally Posted by manav View Post
    so just including the headers and specifying the include path in settings will work? i will try and post later.

    about the qmake steps:
    well they just make everything ready so just doing an nmake will make my programs.
    how do i configure MSVC to just run nmake or use my makefile generated by qmake?
    You could call nmake in the pre-build stage, but I don't trust that makefile. I would rather find a solution that involves compiling Qt into a library and linking with that instead of messing with command lines and make files. It's only going to be a headache in the end.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  3. Problem with friend class declaration in a namespace
    By Angus in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2008, 01:29 PM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM