?
Solved

Making String function calls from within a Class

Posted on 2003-02-25
10
Medium Priority
?
242 Views
Last Modified: 2011-09-20
What is the syntax that one should use?  For example:

//Dynamic String .cpp
//C++ file
//
#include <iostream>
#include <string>
//#include "DynamicString.h"
using namespace std;

class DynamicString{
     private:
         

     public:
     int stringSize;

     char *pStringBuffer;

      DynamicString(int);
      DynamicString( const DynamicString& MyString){};
               
     //  PROBLEM WITH THESE FUNCTIONS,i.e., syntax
     //  Please see main(){}
     String& String::append( const String &);
     String& String::insert( int pos, const String &);
           
     ~DynamicString();    
};

     DynamicString::DynamicString(int sSize): stringSize(sSize)
     {
     pStringBuffer= new char[stringSize];
                         cout<< "constructor" << endl;
     }


     DynamicString::~DynamicString(){
     delete []pStringBuffer;
                         cout<< "destructor" << endl;
     }

int main(){
     int buf=5;

     DynamicString MyString(5), YourString(5);
     
    cout << "Enter a sentence (use <line feed> as the delimiter):" << endl;
           
     cin.getline(MyString.pStringBuffer,buf,'\n');
                         
     // PROBLEM WITH THESE FUNCTION CALLS/SYNTAX
     MyString->pStringBuffer.append("abc");
     MyString->pStringBuffer.insert(2,"abc");

     cout<< "getline" << endl;
                     
     cout<< MyString.pStringBuffer << endl;
                     
          //Clean up

     return 0;
}
0
Comment
Question by:chima
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 

Author Comment

by:chima
ID: 8021560
I've tried other variations on the syntax, but I still have not nailed it down.
0
 
LVL 1

Expert Comment

by:MattAA
ID: 8021573
The STL string class has a lowercase s... in your functions you have it as upper case.  I would think that is the issue.

Hope this helps.
0
 
LVL 1

Expert Comment

by:MattAA
ID: 8021683
I just noticed that another thing that might be the problem.  Your function declarations indicate that the string should be passed by reference (String &) but you are passing a string literal.  In order to pass something by reference, there must be a variable for it to reference to -- even if it is const.

Hope this helps.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Expert Comment

by:mnashadka
ID: 8021797
It looks like you might be having problems where you put "String" but should be putting "DynamicString".  Maybe you changed the name at some point?
String& String::append( const String &);
String& String::insert( int pos, const String &);

should be:

DynamicString& DynamicString::append( const DynamicString &);
DynamicString& DynamicString::insert( int pos, const DynamicString &);

Also, I'm not really sure what you're doing with the MyString->pStringBuffer.insert, but I think you might want to be calling MyString.insert("abc"); and accessing pStringBuffer in your insert function.
0
 

Author Comment

by:chima
ID: 8022684
Thanks MattAA and mnashadka,
I'm new at this.
Because of the capital S in String, this gave the wrong intention.  
I did not realize this until MattAA pointed it out. My hope is to use the STL string (see below).  But I am learning that a function from another class can not be used in DynamicString.

basic_string& append(const E *s);
basic_string& append(const E *s, size_type n);
basic_string& append(const basic_string& str,
    size_type pos, size_type n);
basic_string& append(const basic_string& str);
basic_string& append(size_type n, E c);
basic_string& append(const_iterator first, const_iterator last);

Is there an easy way of appending within DynamicString?
 
0
 
LVL 22

Accepted Solution

by:
ambience earned 400 total points
ID: 8022778
>>     String& String::append( const String &);
    String& String::insert( int pos, const String &);

>> But I am learning that a function from another class can not be used in DynamicString.

unless String is a base class of DynamicString such syntax is not valid, normaly with base classes you bring the base class methods into scope for two reasons, for changing access rights (public, private etc) or when you overload a base class method you also bring the base versions into scope by explicilty making them visible in the scope (requires the use of 'using' keyword).

since you are new this might not be very simple for you to understand.

>> MyString->pStringBuffer.append("abc");

this is yet another mistake, pStringBuffer is char* and it does not define any method append, even if append was a member of DynamicString you would call it like

MyString->append("abc");

and not

MyString->pStringBuffer.append("");

>> Is there an easy way of appending within DynamicString?

i took liberty of making use of std::string and taking away pStringBuffer and size, just to illustrate the idea.

class DynamicString
{
private:
    std::string s;

public:
     DynamicString(int);
     DynamicString( const DynamicString& MyString){};
             
    const DynamicString& DynamicString::append( const std::string &ss)
    {
        s += ss;
        return *this;
    }

    const DynamicString& DynamicString::append( const DynamicString &ss)
    {
        s += ss.s;
        return *this;
    }

    const DynamicString& DynamicString::insert( int pos, const std::string &);
       
    ~DynamicString();    
};



0
 
LVL 22

Expert Comment

by:ambience
ID: 8022781
oops i realized it had to be

MyString.append("abc");
0
 

Author Comment

by:chima
ID: 8026263
Ambience,
Thank you. Your answer explains what I was trying to do. In my reading of C++, I understood that something like this was possible.  In my search, nothing gave me a complete understanding about using std:: or to use std::.
I tried correcting those other mistakes in the submitted code, yet I was unable to do so.  
I'm not sure how you get the points, but I would say that you earned them. cheers
0
 
LVL 22

Expert Comment

by:ambience
ID: 8054982
well, to give points you have to accept a comment as an answer.
0
 

Author Comment

by:chima
ID: 8068275
ambience,
Please pardon the delay.  I thought I had done this correctly.  All the while, I kept wondering where the "grade" was assigned.  Now I know.  Please continue monitoring my questions.  I do appreciate your help.
chima
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question