What am I doing wrong?

This is a discussion on What am I doing wrong? within the C++ Programming forums, part of the General Programming Boards category; Firstly, my code: Code: #include <iostream> using namespace std; class text { char *str; public: void setText(char *s, bool memUsed); ...

  1. #1
    Jason Spaceman
    Guest

    Question What am I doing wrong?

    Firstly, my code:

    Code:
    #include <iostream>
    using namespace std;
    
    class text {
    	char *str;
    public:
    	void setText(char *s, bool memUsed);
    	//void setText(char *s, bool memUsed);
    	char* getText() { return str; }
    	int length() { return strlen(str); }
    	text(char *s = "Default Constructor") { setText(s, false); cout << "\nConstructing\n"; }
    	~text() { delete [] str; cout << "\nDestructing\n"; }
    	text(const text &s);
    	
    	char &letter(int let);
    };
    
    void text::setText(char *s, bool memUsed = true)
    {
    	if(memUsed == true)
    	{	
    		delete [] str;
    	}
    	int length = strlen(s) + 1;
    	str = new char[length];
    	if(!length)
    	{
    		cout << "Allocation Error";
    		exit(1);
    	}
    	strcpy(str, s);
    }
    
    text::text(const text &s)
    {
    	int length = strlen(s.str) + 1;
    	str = new char[length];
    	if(!length)
    	{
    		cout << "Allocation Error";
    		exit(1);
    	}
    	strcpy(str, s.str); 	
    	cout << "\nCopy Constructor\n";
    
    }
    
    char &text::letter(int let)
    {
    	if(let > 0 && let < length())
    	{	
    		return str[let];
    	}
    	return str[0];
    }
    
    
    int main()
    {
    	text one("jason spaceman");
    	text two;
    	text three = one;
    	text ob;
    
    
    	cout << one;
    	cout << two;
    	cout << three;
    	cout << endl;
    
    	one.letter(0) = 'J';
    
    	cout << one;
    	cout << three;
    	cout << ob;
    
    	return 0;
    }
    I have two problems, first when I try to compile the above code, with MS Visual C++, I get the following error:

    G:\CPRG6101\Lab8b.cpp(73) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class text' (or there is no acceptable conversion)
    It gives me an error for each of the "cout <<" parts in main().

    Secondly, I want the program to change the first letter of my name (jason spaceman) to 'Jason Spaceman' (from a lowercase j to an uppercase J) and then output it to the screen along with the length of the string. What do I need to add to main() in order to do this?

    Thanks for any help.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    You need to overload the operator<<() for your class....

    Code:
    #include <iostream>
    #include <string>
    
    class foobar{
    	std::string m_str;
    public:
    	foobar(const std::string& str):m_str(str){}
    	
    friend std::ostream& operator<<(std::ostream&,const foobar&);
    };
    
    std::ostream& operator<<(std::ostream& out,const foobar& f){
    	out << f.m_str;
    	return out;
    }
    
    int main(){
    
    	foobar f("Hello World");
    	
    	std::cout << f;
    }

  3. #3
    Jason Spaceman
    Guest
    Here's my updated code:

    Code:
    #include <iostream>
    using namespace std;
    
    class text {
    	char *str;
    public:
    	text(char *s = "Default Constructor") { setText(s, false); }  
    	text(const text &s);  
    	~text() { delete [] str; }  
    	void setText(char *s, bool memalloc);
    	char* getText() { return str; }
    	int length() { return strlen(str); }
    	char &letter(int let);  
    	friend ostream& operator<<(ostream &out,const text ob);
    	friend istream& operator>>(istream &in, text &ob);
    	text operator=(text obj);
    	text operator+(text ob);
    
    };
    
    void text::setText(char *s, bool memalloc = true)
    {
    	if(memalloc == true)
    	{	
    		delete [] str;
    	}
    	int length = strlen(s) + 1;
    	str = new char[length];
    	if(!length)
    	{
    		cout << "Allocation Error";
    		exit(1);
    	}
    	strcpy(str, s);
    }
    
    text::text(const text &s)
    {
    	int length = strlen(s.str) + 1;
    	str = new char[length];
    	if(!length)
    	{
    		cout << "Allocation Error";
    		exit(1);
    	}
    	strcpy(str, s.str); 	
    
    
    }
    
    char &text::letter(int let)
    {
    	if(let > 0 && let < length())
    	{	
    		return str[let];
    	}
    	return str[0];
    }
    
    ostream& operator<<(ostream &out, const text ob)
    {
    
    	out << "The String for this object is " << ob.str << endl; 
    	return out;
    }
    
    istream& operator>>(istream &in, text &ob)
    {
    	char charTemp[256];
    	cout << endl;
    	cout << "Enter string: ";
    	in >> charTemp;
    	ob.setText(charTemp);
    	return in;
    }
    
    int main()
    {
    	
    	text one("jason spaceman");
    	text two;
    	text three = one;
    	text ob;
    	cin >> ob;
    
    
    	cout << one;
    	cout << two;
    	cout << three;
    	cout << endl;
    
    	one.letter(0) = 'J';
    
    	cout << one;
    	cout << three;
    	cout << ob;
    
    	return 0;
    }
    When I output each of the three objects to the screen I also want to output their respective lengths (the length of each string that each object displays). But I'm not sure how to do this. What do I need to add to my cout << statements?

  4. #4
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    You already have a function which returns the length so you can just go :

    cout << one.length() ;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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