[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

Making String function calls from within a Class

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
chima
Asked:
chima
  • 4
  • 3
  • 2
  • +1
1 Solution
 
chimaAuthor Commented:
I've tried other variations on the syntax, but I still have not nailed it down.
0
 
MattAACommented:
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
 
MattAACommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
mnashadkaCommented:
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
 
chimaAuthor Commented:
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
 
ambienceCommented:
>>     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
 
ambienceCommented:
oops i realized it had to be

MyString.append("abc");
0
 
chimaAuthor Commented:
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
 
ambienceCommented:
well, to give points you have to accept a comment as an answer.
0
 
chimaAuthor Commented:
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now