Compile Errors, Please Help

This is a discussion on Compile Errors, Please Help within the C++ Programming forums, part of the General Programming Boards category; Code: Hello, I am doing an exercise in my book and im suing the answer codes provided, and it still ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    9

    Unhappy Compile Errors, Please Help

    Code:
    Hello,
    I am doing an exercise in my book and im suing the answer codes provided, and it still does not work, here is the code:
    #include <stdafx.h>
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    using namespace std;
    
    class String {
    private:
    	char *ptr;
    public:
    	String();
    	String(char *s);
    	String(const String &src);
    	~String();
    
    	String& operator=(const String &src) {cpy(src.ptr); return *this;}
    	String& operator=(char *s) {cpy(s); return *this;}
    
    	String operator+(char *s);
    	int operator==(const String &other);
    	operator char*() {return ptr;}
    	operator int() {return atoi(ptr);}  //this is the convert to int function
    
    	void cat(char *s);
    	void cpy(char *s);
    };
    
    
    int main() {
    	String a, b, c;
    	a = "I ";
    	b = "am ";
    	c = "so ";
    	String d = a + b + c + "very happy!\n";
    	cout << d;
    	return 0;
    }
    
    
    //declaring String class member functions
    String::String() {
    	ptr = new char[1];
    	ptr[0] = '\0';
    }
    
    String::String(char *s) {
    	int n = strlen(s);
    	ptr = new char[n + 1];
    	strcpy(ptr, s);
    }
    
    String::String(const String &src) {
    	int n = strlen(src.ptr);
    	ptr = new char[n + 1];
    	strcpy(ptr, src.ptr);
    }
    
    String::~String() {
    	delete [] ptr;
    }
    
    int String::operator==(const String &other) {
    	return (strcmp(ptr, other.ptr) == 0);
    }
    
    String String::operator+(char *s) {
    	String new_str(ptr);
    	new_str.cat(s);
    	return new_str;
    }
    
    //cpy- copy string function
    void String::cpy(char *s) {
    	delete [] ptr;
    	int n = strlen(s);
    	ptr = new char[n + 1];
    	strcpy(ptr, s);
    }
    
    //cat- concatenation string function
    void String::cat(char *s) {
    	int n = strlen(ptr) + strlen(s);
    	char *p1 = new char[n + 1];
    	strcpy(p1, ptr);
    	strcat(p1, s);
    	delete [] ptr;
    	ptr = p1;
    }
    
    okay, so the exercies was to add a convert to int function which is marked above, the code compiled fine before i added it, but now i add it and it does not, please help.

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    I think you should have to overload assignment operator (=) as well.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    9

    Smile

    please specify an example if you can...

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Follow
    1
    2
    any of these two.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Errors, Please Help
    By jiahwa008 in forum C++ Programming
    Replies: 11
    Last Post: 03-11-2011, 05:23 AM
  2. Strange compile errors
    By csonx_p in forum C++ Programming
    Replies: 10
    Last Post: 07-28-2008, 12:41 PM
  3. compile once, compile twice ...error
    By Benzakhar in forum Windows Programming
    Replies: 6
    Last Post: 12-28-2003, 06:00 AM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM

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