Classes

This is a discussion on Classes within the C++ Programming forums, part of the General Programming Boards category; Hey Im having a problem with passing arguments into a function from a class.This is my program with just prototypes ...

  1. #1
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151

    Classes

    Hey Im having a problem with passing arguments into a function from a class.This is my program with just prototypes and function calls. I am confused when to pass by pointer and by reference.

    Header file
    Code:
    #ifndef STRING_HPP
    #define STRING_HPP
    
    #include <iostream>
    
    using namespace std;
    class mystring
    {
        typedef struct STRING* String;
    
        struct STRING
        {
            int size;
            char *theString;
        };
    
    
    
    private:
    void validateString( mystring &theString );
    
    public:
    mystring()
    {
    	String->theString = NULL;
    	String.size =0;
    }
    mystring( char *init)
    {
    	newString( init );
    }
    
    ~mystring()
    {
    	destroyString( theString );
    }
    mystring&  destroyString( mystring &theString );
    mystring& newString( char* theString );
    void printString(mystring &theString);
    char charAt( mystring &theString, int index );
    mystring& operator+( mystring &string1, mystring &string2 );
    };
    
    
    
    #endif

    Function definitions
    Code:
    #include <iostream.h>
    
    #include <assert.h>
    #include "mystring.hpp"
    using namespace std;
    
    int stringCount = 0;
    
    mystring& mystring::newString( char& init )
    {
      /*Function definition */
    
      return( newString );
    }
    
     void mystring::validateString( mystring &theString )
    {
       /*Function definition */
    }
    
    mystring& mystring::destroyString( mystring &theString )
    {
       /*Function definition */
    
      return( theString );
    }
    
    void mystring::printString( mystring &theString )
    {
      validateString( theString );
    
      cout << theString->size << theString -> theString << endl );
    }
    
    char* mystring::charAt( mystring &theString, int index )
    {
      validateString( theString );
      assert( index >= 0 );
      assert( index < theString -> size );
    
      return( theString -> theString[index] );
    }
    
    mystring& mystring::operator+( mystring &string1, mystring &string2 )
    {
       /*Function definition */
      return( newString );
    }
    Especially when I am overloading the operator + for the purpose of concatenation, I get an error stating that 0 or 1 argument only has to be passed. Please help.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your operator+ when it is member of a class will only take one argument (the right-hand side part), because the left-hand side is defined by the current object (this) in your.

    So if you do:
    x = y + z;
    then it's the same as x = y.operator+(z).

    You can do your operators as free functions instead, which would allow you to have to arguments.

    --
    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.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If your operator + is a member function (which it is, since it is mystring:perator +), then the first argument is always "this" -- hence + should only take one (other) argument. In other words, choose one:
    Code:
    mystring& mystring::operator+(const mystring &string2);
    mystring& operator+(const mystring &string1, const mystring &string2);
    There's no need to pass anything by pointers that I can see. And you'll want to put const everywhere you don't expect that argument to change.

  4. #4
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    What is free functions?
    As I am not using the string class, I have to define a function with operator overloading. My function definitions such as mystring& operator+(const mystring &string1, const mystring &string2), is in a another file which is different from my class file.
    So when im calling the function isnt it supposed to be z = x + y;?

  5. #5
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    I think I got what your saying, thanks to tabstop.

    Also, my 1st constructor is giving problems.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Ron View Post
    I think I got what your saying, thanks to tabstop.

    Also, my 1st constructor is giving problems.
    String->size = 0. (Look at the line above!)

  7. #7
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    hmmm.. so another question, in the operator + when Im working with the 'this'. would the syntax be this.theString or this->theString?

  8. #8
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    im still getting the same error in the constructor for both theString and size
    error:expected primary-expression before '->' token

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    It seems to me that the first problem is that your class does not have any member variables. If I am reading it correctly, all you have done is define a private POD struct as an inner class of your mystring class. What you probably want to do is just declare two member variables:
    Code:
    class mystring
    {
    public:
        // ...
    private:
        int size;
        char* theString;
    };
    By the way, do not use using directives in header files except within a local scope.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    "this" is a pointer, so treat it as such.
    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. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 11:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21