•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 401,652 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,621 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 4888 | Replies: 1
![]() |
•
•
Join Date: Feb 2005
Posts: 11
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
#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 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;
}![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- need a bit of help with my homework(operator overloading) (C++)
- Operator Overloading Question (C++)
- Overloading assignment operator (C++)
- C++ Tic Tac Toe using classes & operator overloading (C++)
- program for finding factorial of a number using *operator overloading (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ prog =*(
- Next Thread: Double values with two decimal places


Linear Mode