Thread: RE: Classes Help

  1. #1
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    RE: Classes Help

    sry again, i am still learning classes :( folllowing is a new class than my previous post

    header file:
    Code:
    #ifndef __cDATE_H
    #define __cDATE_H
    
    #include <iostream.h>
    
    class cSTRING 
    {
    	private:
    		int nCmpValue;
    		char *_name;
    	public:
    		cSTRING(char *name, int returnVal);
    		cSTRING(cSTRING &rhs);
    		
    		cDATE(int,int);				
    				
    
    		~cSTRING();
    
    		char *getName()
    		{
    			return _name;
    		}
    		void two(char*,int);
    
    		cSTRING& operator=(const cSTRING& cStr);
    		cSTRING operator+(const cSTRING& cStr);
    		cSTRING& operator+=(const cSTRING& cStr);
    		cSTRING& operator==(const cSTRING& cStr);
    		cSTRING& operator!=(const cSTRING& cStr);
    };
    
    #endif
    CPP File:
    Code:
    #include "cDATE.h"
    #include <iostream.h>
    #include <string.h>
    
    cSTRING::cSTRING(char *name, int returnVal)
    {
    	nCmpValue = returnVal;
    	
    	_name = new char[strlen(name) + 1];
         //Allocates an character array object
        strcpy(_name, name);
    
    	cout << "cSTRING Default Constructor Called" << endl;
    }
    
    
    
    cSTRING& cSTRING::operator=(const cSTRING& other)
    {
    	cout << "cSTRING assignment op" << endl;
    	if (this!=&other)
    	{
    
    	}
    	return *this;
    }
    
    cSTRING cSTRING::operator+(const cSTRING& d)
    {
    	cSTRING d1;
    	cout << "inside +" << endl; 
    	return( d1 );
    
    }
    
    cSTRING::cSTRING(cSTRING &rhs)
    {
    	_nCmpValue = rhs.getId();
        _name = new char[strlen(rhs.getName()) + 1];
        strcpy(_name,rhs._name);
    }
    
    cSTRING::~cSTRING()
    { 
    	delete[] _name;
    	cout << "Destructor called" << endl; 
    };
    Driver file:
    Code:
    #include "cDATE.h"
    #include <iostream.h>
    
    //cSTRING doNothing1( cSTRING d );
    //void doNothing2( const cSTRING& d );
    
    int main(void)
    {
    	char mychar = 'h';
    	cout << "Hello called ";
    //	cSTRING d1(mychar);
    	cout << endl;
    
    	cSTRING programmer("John",22);
        cout << programmer.getName() << endl;
    
    	return (0);
    }
    this program is *suppose* to create a dynamic string, use operators to add two strings, numbers, and check if they are ==
    i am kinda stuck and have been using this wonderful tutorial @ about.com

    errors:
    Debug/console api.exe : fatal error LNK1120: 1 unresolved externals


    it builds but doesnt link i guess
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  2. #2
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    forgot to mention this error:

    cdate.obj : error LNK2001: unresolved external symbol "public: __thiscall cSTRING::cSTRING(void)" (??0cSTRING@@QAE@XZ)
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  3. #3
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    Only had a quick glance, but this is where I think your going wrong. The second you create your own constructor, the compiler will no longer provide you with the default constructor.
    Code:
    cSTRING cSTRING::operator+(const cSTRING& d)
    {
    	cSTRING d1;
    	cout << "inside +" << endl; 
    	return( d1 );
    
    }
    Your declaration of the object d1 has no arguments, and you do not have a constructor whose parameters match this call (the compiler is looking for a constructor cSTRING(void) ). Give yourself a default constructor and it should compile.

    [edit] There are a few other bugs (i.e. the operator+ method does nothing. But let us know when you can get this to compile. :)
    Last edited by dalek; 03-19-2004 at 05:18 AM.

  4. #4
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    You obviously forgot to implement a standart constructor for your class... (the code you posted isn't correct, because there is no declaration of cSTRING::cSTRING(void) ...)


    so just include theese lines in your implementation file:
    Code:
    cSTRING::cSTRING() : _name(0), nCmpValue(0)
    {
    }
    Also, don't forget to assign standart values for your member variables in your constructors...
    (i did it in the code above...)
    This is good for the health of your code

    P.S.:
    Why "cSTRING" for a classname and not "CString" ????
    it looks funny

  5. #5
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    thanks for help:

    1 correction:

    Code:
    cSTRING::cSTRING() : _name(0), nCmpValue(0)
    {
    }
    
    //but rather
    
    cString::cSTRING()
    {
    }
    dont know y, but i think cSTRING looks better than CString *more caps more ugly?*


    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  6. #6
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    try using : Acool_cStRiNg_loocA

  7. #7
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    it seems i am stuck again:
    when i call the
    Code:
    cSTRING::cSTRING(int num1, int num2)
    my prog crashes

    in my driver file i added the following line:

    Code:
    	cout << "Copying Numbers." << endl;
    	cSTRING oCopy(5,10);
    it copies the passed content into private variables but when it exists it, it crashes...
    where am i going wrong??
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    How do you define the constructor for cString taking two ints as parameters?

  9. #9
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    in Header:
    Code:
    cSTRING(int num1,int num2);
    in CPP
    Code:
    cSTRING::cSTRING(int num1,int num2)
    {
    	_num1 = num1;
    	_num2 = num2;
    
    	cout << endl;
    	cout << _num1 << endl << _num2 << endl;
    
    	cout << "Numbers are copied" << endl;
    }
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Are you sure it is crashing because of the constructor call?

    Comment out all code associated with the constructor and see what happens. If it doesn't crash without the constructor, but does with, then you'll need to submit more code, like say the header file code so we can see how _num1, and _num2 are declared.

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, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM