error 2065, error 2228 C++

ostream & operator<<(ostream & stream, String1 &sIn)
{
stream << sIn.str;
return stream;
}

When I compile this I get 2 Error messages which is down from the 12 I had 3 hours ago, but I have no idea where I am going wrong.

1. error C2065: 'str': undeclared identifier
2. error C2228:left of '.sIn' must have class/struct/union
Is String1 a class you create yourself or you are going to use the Standard C++ string class ? If C++ string class, I think to get the char * is to call .c_str().

Refer to http://www.cplusplus.com/reference/string/string/ for all the methods inside the Standard C++ string class.
It is a homemade string class Here is the rest of the code I have written:
#include<iostream>
using namespace std;


class String1
{
private:
char str[80];

public:
String1();
String1(char *);

void GetString(char *);
//concatenation
String1 operator+(String1 &);

//assignment
String1 operator=(String1 &);

friend ostream & operator<<(ostream & stream, String1 & sIn);


};
ostream & operator<<(ostream & stream, String1 &sIn)
{
stream <<str.sIn;
return stream;
}

String1 String1::operator+(String1 & sIn)
{
String1 temp(this->str);
strcat(temp.str, sIn.str);
}
String1 String1::operator =(String1 & sIn)
{
}
void String1::GetString(char * strBufPtr)
{
strcpy(strBufPtr, str);
}
String1::String1()
{
str[0] = 0;
}
String1::String1(char * strIn)
{
strcpy(str, strIn);
}

void main()
{
String1 S("Bob");
char buffer[20];
S.GetString(buffer);
cout << buffer;

String1 S1, S2("Bob"), S3("Jones");
S1 = S2;
//S1 = S2 + S3("Bob"), S3("Jones");
//String1 S1, S2("Bob");
cout << S1<< S1;
}
First post
ostream & operator<<(ostream & stream, String1 &sIn)
{
stream << sIn.str;
return stream;
}

Second post
ostream & operator<<(ostream & stream, String1 &sIn)
{
stream <<str.sIn;
return stream;
}

Did you get mixed up somewhere ? Is it sIn.str or str.sIn ?
It is str.sIn, sorry
1. error C2065: 'str': undeclared identifier
2. error C2228:left of '.sIn' must have class/struct/union

The error already point out your mistake.

You declare as String1& sIn so inside you should call it like sIn.str instead.
Then I get 2 new errors:
1.error C2143:syntax error:missing ',' before '.'
2.error C2039:'sIn' : is not a member of 'String1'

I tried to put a ',' before the '.' and I got more errors. I don't know how I got this lost.
I just try compiling using below and it is ok. Check you do not have a old version of below code and hence compilation fail.

1
2
3
4
5
ostream & operator<<(ostream & stream, String1 &sIn)
{
stream <<sIn.str;
return stream;
}



That did work thank you. I have just discovered a new problem though.

1
2
3
4
5
6
7
8
9
10
String1 String1::operator+(String1 & sIn)
{
String1 temp(this->str);
strcat(temp.str, sIn.str);
//Doesn't return a value
}
String1 String1::operator =(String1 & sIn)
{
// I think I erased this trying to do the rest.  doesn't return a value
}
this part
1
2
3
4
5
String1 String1::operator+(String1 & sIn)
{
String1 temp(this->str);
strcat(temp.str, sIn.str);
return temp; // was missing 


so now I am stuck with this problem:
1
2
3
4
String1 String1::operator =(String1 & sIn)
{
// I think I erased this trying to do the rest.  doesn't return a value
}
Actually for C++ operator overloading, we tend to return a pointer to current object so we can do nested calls on itself.

E.g
String1& String1::operator+(String1 & sIn)
{
String1 temp(this->str);
strcat(temp.str, sIn.str);
//Doesn't return a value
return *this;

}
String1& String1::operator =(String1 & sIn)
{
// I think I erased this trying to do the rest. doesn't return a value
return *this;
}
1
2
3
4
String1 String1::operator =(String1 & sIn)
{
      return *this
}


Now it compiles However my output is off. I am not sure why but I will figure it out. thanks for your help. You made me think and I realized I wrote that code on an earlier version.
String1& String1::operator =(String1 & sIn)
{
return *this
}

For efficiency sake, you should return reference to String1 instead of a new copy of String1.
Topic archived. No new replies allowed.