Este si se ejecuta bien:
string.h
Código:
#ifndef STRING_H
#define STRING_H
#include <iostream>
using namespace std;
class Str
{
char string[100];
public:
Str(char *s = "\0")
{
strcpy(string, s);
}
~Str() { };
Str operator +(Str); // concatena 2 objetos
Str operator +(char[]); // concatena objeto con cadena
friend ostream& operator << (ostream&, const Str&);
friend istream& operator >> (istream&, Str &obj);
Str operator =(Str);
Str operator =(char[]);
};
#endif
string.cpp
Código:
#include "string.h"
#include <iostream>
Str Str::operator +(Str s)
{
Str tmp;
strcpy(tmp.string, string);
strcat(tmp.string, s.string);
return tmp;
}
Str Str::operator +(char s[])
{
Str tmp;
strcpy(tmp.string, string);
strcat(tmp.string, s);
return tmp;
}
Str Str::operator =(Str s)
{
strcpy(string, s.string);
return *this;
}
Str Str::operator =(char s[])
{
strcpy(string, s);
return *this;
}
ostream& operator << (ostream &f, const Str &obj)
{
f << obj.string << endl;
return f;
}
istream& operator >> (istream& f, Str &obj)
{
f >> obj.string;
return f;
}
main.cpp
Código:
#include "string.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
Str str1("CaraJodida "), str2("es groso "), str3;
str3 = str1 + str2;
str3 = str3 + ";D ";
cout << "Cadena concatenada: " << str3 << endl;
str3 = "cadena asignada a objeto";
cout << "Otra cadena: " << str3 << endl;
//cout << "Ingrese cadena : ";
//cin >> str3;
cout << str3;
system("PAUSE");
return 0;
}
Este codigo, en Dev-C++ no me da errores... tampoco en Visual Studio...
Para que no tengas que copiar y pegar todo... el chiste es que tu tenias:
Código:
friend istream& operator >> (istream&, const Str &obj);
Como declaracion, y como definicion tenias:
Código:
istream& operator >> (istream& f, const Str &obj)
{
f >> obj.string;
return f;
}
La cadena que va a recibir este operador no puede ser constante, asi que solo quitate el
const a
const Str &obj y te va a funcionar