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