I am now trying to add 2 strings together. But I dont understand where my compiler is pulling the reference to an int from. This is my code

#include <iostream>

using namespace std;

class String {
	char *str;			//pointer to character block
public:
	String();			//default constructor
	String (char *s);
	void setString (char *s);
	int stringLength();
	char *getString();
	operator +(String *s);
	operator =(String *s);
	
	
};
String::String()  {}


String::String(char *s)
{
	int length=strlen(s);		//length of string
	str=new char[length+1];		//increments length of string
	strcpy(str,s);				//copies s to str
	cout<<str<<'\n';
}

void String::setString(char *s)
{
	int length=strlen(s);		//length of string
	str=new char[length+1];		//increments length of string
	strcpy(str,s);				//copies s to str
	cout<<str<<'\n';
}



int String::stringLength()
{
	int length=strlen(str);;
	
return length;
}

  char *String::getString()
  {  
      
    return str;
    
  }


  String::operator +(String *s)
  {
 
	  String temp;
	  strcpy(temp.str,str);
	  strcat(str, str);

	  
	  
  return str;  
  }

  String::operator =(String *s)
  {
	  strcpy(str, str);

		  return *this;
  }
	  

int main()
{
	String s1="Hello World";
	String s2;

	s2.setString("THis is a string");
	cout<<"string 1 is "<<s1.stringLength()<<"characters long"<< endl;
	cout << "string 1 is " << s1.getString()<<'\n';
	cout << "string 2 is " << s2.getString()<< endl << endl; 
	cout << "assign string 2 to string 1" <<endl;
	s1.setString(s2);


return 0;
}

I am receiving errors
Compiling...
string.cpp
error C2440: 'return' : cannot convert from 'char *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
string.cpp(70) : error C2440: 'return' : cannot convert from 'class String' to 'int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
error C2664: 'setString' : cannot convert parameter 1 from 'class String' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

string.obj - 3 error(s), 0 warning(s)

The first error is the most annoying. I'm not even talking about an int:) THanks

Member Avatar for Siersan

Your compiler is assuming a function returns int if you don't specify a return value. That's not technically legal C++, but many compilers support it anyway. Here is your code with the necessary changes to make it better.

#include <cstring>
#include <iostream>

using namespace std;

class String {
  char *str;    // Pointer to character block
public:
  String();
  String(const char *s);
  String(const String& s);
  void setString(const char *s);
  void setString(const String& s);
  int stringLength() const;
  char *getString() const;
  String& operator=(const String& s);
};

String::String()
{
  str = 0;
}

String::String(const char *s)
{
  str = new char[strlen(s) + 1];  // Extra space for '\0'
  strcpy(str, s);
}

String::String(const String& s)
{
  str = new char[strlen(s.str) + 1];  // Extra space for '\0'
  strcpy(str, s.str);
}

void String::setString(const char *s)
{
  setString(String(s));
}

void String::setString(const String& s)
{
  *this = s;
}

int String::stringLength() const
{
  return strlen(str);
}

char *String::getString() const
{
  return str;
}

String& String::operator=(const String& s)
{
  char *temp = new char[strlen(s.str) + 1];
  strcpy(temp, s.str);
  delete [] str;
  str = temp;
  return *this;
}

String operator+(const String& a, const String& b)
{
  char *temp = new char[a.stringLength() + b.stringLength() + 1];
  strcpy(temp, a.getString());
  strcat(temp, b.getString());
  return String(temp);
}

int main()
{
  String s1 = "Hello World";
  String s2;

  s2.setString("This is a string");
  cout<<"string 1 is "<< s1.stringLength() <<" characters long"<<endl;
  cout<<"string 1 is "<< s1.getString() <<'\n';
  cout<<"string 2 is "<< s2.getString() << endl <<endl; 
  cout<<"assign string 2 to string 1"<<endl;
  s1.setString(s2);
  cout<<"string 1 is "<< s1.getString() <<endl;

  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.