Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 06-27-2005, 08:08 PM   #1 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
using a string in a class

Thankies to Valmont for being so helpful in fixing the problem that has led me to this problem!

Now that I know how to use and create header files, have got everything linking correctly, and figured out this #ifndef stuff, I'd like to....use a string as a variable type in one of my classes.

So-oo, how do I do this? I've sort of been skirting my string issues for awhile now. I don't really understand anything about the 'using namespace std;' statement, which is the only way I've been getting strings to work. Now that I've got my class seperated into a .h and .cpp file, I don't know where I would put it, either.

I've tried putting it in various places and messing around alot but I keep getting lots of "'string' used as a type, but not defined as a type" errors.

Can someone guide me through using a string as a var type in a class? Thanks.
Aniviel833 is offline   Reply With Quote
Old 06-27-2005, 10:04 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
You'll need to include the header <string> wherever you declare a std::string object (variable).

About namespaces (like std) see this thread:
return types

After reading this, I want you (to test yourself) to write me a simple function swapping two std::string objects. You must do this in your own namespace. Post it. Even in case of a failure. Then we'll take a look at it together.
__________________
Valmont is offline   Reply With Quote
Old 06-28-2005, 08:08 PM   #3 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
First of all, a question about the original topic (to post the namespace thing in a sec...)

I can get strings to work just fine using std::string in any of my derived classes. However, if I try and use it in my base class, I get this 'undefined reference to MyClass::MyClass(bool)' error. Is there something different about the way this works with base classes, or shall I post the code?

Thanks,
-Stacy
Aniviel833 is offline   Reply With Quote
Old 06-28-2005, 08:21 PM   #4 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
Thanks for the explanation and the exercise. Here's what I've got:

Code:
#include <iostream>
#include <string>

namespace stacy
{

    //swap() swaps string1 and string2, then prints the new values.
    void swap(std::string string1, std::string string2)
        {
                std::string holder = "This will temporarily hold a string for me.";
        
                holder = string1;
                string1 = string2;
                string2 = holder;
                
                std::cout << "String 1 is now '" << string1 << "' and string 2 is '" << string2 << "'.";
        }

}



int main()
{
    stacy::swap("one", "two");
    std::cin.get();
}

This works fine, but I'm wondering about something: A while back, someone (who was doing Java; I don't know if this still applies...) was telling me that assigning an object to another (holder = string1) can cause problems because one object has two labels(?) At the end of swap(), holder and string2 are the same. Do I need to/should I do something about this?
Aniviel833 is offline   Reply With Quote
Old 06-28-2005, 10:26 PM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
You might be refering to this thread where you would parse by reference instead of making copies like your swap function does.
Which would alter your code to be:
Code:
#include <iostream>
#include <string>

namespace stacy
{
  //swap() swaps string1 and string2, then prints the new values.
  void swap(std::string &string1, std::string &string2)
  {
    std::string holder;
    holder = string1;
    string1 = string2;
    string2 = holder;
    std::cout << "String 1 is now '" << string1 
                  << "' and string 2 is '" << string2 << "'." 
                  << std::endl;
    }
}



int main()
{
  std::string one = "one";
  std::string two = "two";
  std::cout << "Befor swap: " << one << " " << two << std::endl;
  stacy::swap(one, two);
  std::cout << "After swap: " << one << " " << two << std::endl;
  std::cin.get();
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 06-29-2005, 01:19 AM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Yes, the pass by reference is important. However, the standard output does not belong in the function swap. Let the function do only what its name implies.

Anyway, here is how it looks when more people work at the same project in real life:
Code:
#include <iostream>
#include <string>

namespace stacy
{
  //swap() swaps two strings.
  void swap(std::string& string1, std::string& string2)
  {
    std::string holder;
    holder = string1;
    string1 = string2;
    string2 = holder;
  }
}

namespace Alex
{
  //swap() swaps two strings.
  void swap(std::string* string1, std::string* string2)
  {
    std::string holder;
    holder = *string1;
    *string1 = *string2;
    *string2 = holder;
  }
}



int main()
{
  std::string one("one"), two("two"), three("three");
  
  //Entering namespace scope.
  {
    using namespace stacy;
    swap(one, two);
  }
  //Exiting namespace scope
  
//---------------------------------//
  
  //Entering namespace scope.
  {
    using namespace Alex;
    swap(&two, &three);
  }
  //Exiting namespace scope
  
  std::cout << "After swap: " << one << " " << two <<" "<< three << std::endl;
  std::cin.get();
}
__________________
Valmont is offline   Reply With Quote
Old 06-29-2005, 03:45 AM   #7 (permalink)
Rashakil Fol
Registered User
 
Join Date: Jun 2005
Posts: 3
Rashakil Fol is on a distinguished road
Ironically, strings include their own swap method which is much more efficient -- it takes a constant time of operation instead of copying every character. For example:

Code:
std::string a = "Alpha";
std::string b = "Beta";
a.swap(b);
std::cout << a << ' ' << b << std::endl;
This prints Beta Alpha.
Rashakil Fol is offline   Reply With Quote
Old 06-29-2005, 07:03 AM   #8 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
Quote:
Ironically, strings include their own swap method
The object of this thread, was to learn the usage of namespaces and object oriented C++, by creating your own implementation of some standard provided features.
Quote:
which is much more efficient - it takes a constant time of operation instead of copying every character.
Then compare it to the version, where you parse by reference.. You'll soon notice they are the same.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 06-29-2005, 05:50 PM   #9 (permalink)
Rashakil Fol
Registered User
 
Join Date: Jun 2005
Posts: 3
Rashakil Fol is on a distinguished road
Quote:
Originally Posted by redhead
Then compare it to the version, where you parse by reference.. You'll soon notice they are the same.
Huh? Are you talking about this?
Code:
  void swap(std::string& string1, std::string& string2)
  {
    std::string holder;
    holder = string1;
    string1 = string2;
    string2 = holder;
  }
This is not the same as the string class's swap method. The string class's swap method simply swaps the objects' pointers that point to their dynamically allocated arrays, which means that no matter how large the strings are, the operation takes the same amount of time.

The code above copies characters around -- if string1 and string2 are fifty characters long, then fifty characters will get copied from string1 to holder, then fifty characters will get copied from string2 to string1, and then fifty characters will get copied from holder to string2. Also, holder will have to perform a memory allocation when it gets assigned from string1.

This is far more expensive than the swap method, which only has three pairs of pointers to swap, everytime, with no extra allocations in between.
Rashakil Fol is offline   Reply With Quote
Old 06-29-2005, 07:53 PM   #10 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Besides that, a destructor needs to be called for the temporary variable as well. The std::string has its own swap() method indeed. It is usually between 6 or 7 times as faster as the methods presented earlier.

Nevertheless, there is no "irony" involved in the kind of assignments presented earlier. Often the student needs to learn to develop algorithms at an early stage. Re-inventing the wheel is a common thing.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting objects arrays by object properties sde Java 28 08-05-2004 05:51 AM
From C to Java HighterDK Java 11 07-13-2004 07:15 PM


All times are GMT -8. The time now is 09:25 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting